~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
1 by brian
clean slate
19
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
20
#ifndef DRIZZLE_SERVER_SQL_STRING_H
21
#define DRIZZLE_SERVER_SQL_STRING_H
22
1 by brian
clean slate
23
/* This file is originally from the mysql distribution. Coded by monty */
24
25
#ifdef USE_PRAGMA_INTERFACE
26
#pragma interface			/* gcc class implementation */
27
#endif
28
29
#ifndef NOT_FIXED_DEC
30
#define NOT_FIXED_DEC			31
31
#endif
32
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
33
#include <libdrizzle/drizzle_com.h>
34
1 by brian
clean slate
35
class String;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
36
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
205 by Brian Aker
uint32 -> uin32_t
37
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
38
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
205 by Brian Aker
uint32 -> uin32_t
39
			const char *from, uint32_t from_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
40
			const CHARSET_INFO * const from_cs, uint *errors);
41
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
1 by brian
clean slate
42
                               char *to, uint to_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
43
                               const CHARSET_INFO * const from_cs,
1 by brian
clean slate
44
                               const char *from, uint from_length,
45
                               uint nchars,
46
                               const char **well_formed_error_pos,
47
                               const char **cannot_convert_error_pos,
48
                               const char **from_end_pos);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
49
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
1 by brian
clean slate
50
                                 char *dst, size_t dstlen,
51
                                 const char *src, size_t srclen);
52
53
class String
54
{
55
  char *Ptr;
205 by Brian Aker
uint32 -> uin32_t
56
  uint32_t str_length,Alloced_length;
1 by brian
clean slate
57
  bool alloced;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
58
  const CHARSET_INFO *str_charset;
1 by brian
clean slate
59
public:
60
  String()
61
  { 
62
    Ptr=0; str_length=Alloced_length=0; alloced=0; 
63
    str_charset= &my_charset_bin; 
64
  }
205 by Brian Aker
uint32 -> uin32_t
65
  String(uint32_t length_arg)
1 by brian
clean slate
66
  { 
67
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
68
    str_charset= &my_charset_bin;
69
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
70
  String(const char *str, const CHARSET_INFO * const cs)
1 by brian
clean slate
71
  { 
72
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
73
    str_charset=cs;
74
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
75
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
1 by brian
clean slate
76
  { 
77
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
78
    str_charset=cs;
79
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
80
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
1 by brian
clean slate
81
  { 
82
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
83
    str_charset=cs;
84
  }
85
  String(const String &str)
86
  { 
87
    Ptr=str.Ptr ; str_length=str.str_length ;
88
    Alloced_length=str.Alloced_length; alloced=0; 
89
    str_charset=str.str_charset;
90
  }
91
  static void *operator new(size_t size, MEM_ROOT *mem_root)
92
  { return (void*) alloc_root(mem_root, (uint) size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
93
  static void operator delete(void *ptr_arg __attribute__((unused)),
94
                              size_t size __attribute__((unused)))
1 by brian
clean slate
95
  { TRASH(ptr_arg, size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
96
  static void operator delete(void *ptr_arg __attribute__((unused)),
97
                              MEM_ROOT *mem_root __attribute__((unused)))
1 by brian
clean slate
98
  { /* never called */ }
99
  ~String() { free(); }
100
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
101
  inline void set_charset(const CHARSET_INFO * const charset_arg)
1 by brian
clean slate
102
  { str_charset= charset_arg; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
103
  inline const CHARSET_INFO *charset() const { return str_charset; }
205 by Brian Aker
uint32 -> uin32_t
104
  inline uint32_t length() const { return str_length;}
105
  inline uint32_t alloced_length() const { return Alloced_length;}
106
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
107
  inline void length(uint32_t len) { str_length=len ; }
1 by brian
clean slate
108
  inline bool is_empty() { return (str_length == 0); }
109
  inline void mark_as_const() { Alloced_length= 0;}
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
110
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
111
  inline const char *ptr() const { return Ptr; }
112
  inline char *c_ptr()
113
  {
114
    if (!Ptr || Ptr[str_length])		/* Should be safe */
115
      (void) realloc(str_length);
116
    return Ptr;
117
  }
118
  inline char *c_ptr_quick()
119
  {
120
    if (Ptr && str_length < Alloced_length)
121
      Ptr[str_length]=0;
122
    return Ptr;
123
  }
124
  inline char *c_ptr_safe()
125
  {
126
    if (Ptr && str_length < Alloced_length)
127
      Ptr[str_length]=0;
128
    else
129
      (void) realloc(str_length);
130
    return Ptr;
131
  }
132
205 by Brian Aker
uint32 -> uin32_t
133
  void set(String &str,uint32_t offset,uint32_t arg_length)
1 by brian
clean slate
134
  {
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
135
    assert(&str != this);
1 by brian
clean slate
136
    free();
137
    Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
138
    if (str.Alloced_length)
139
      Alloced_length=str.Alloced_length-offset;
140
    else
141
      Alloced_length=0;
142
    str_charset=str.str_charset;
143
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
144
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
145
  {
146
    free();
147
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
148
    str_charset=cs;
149
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
150
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
151
  {
152
    free();
153
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
154
    str_charset=cs;
155
  }
205 by Brian Aker
uint32 -> uin32_t
156
  bool set_ascii(const char *str, uint32_t arg_length);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
157
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
158
  {
159
    if (!alloced)
160
    {
161
      Ptr=(char*) str; str_length=Alloced_length=arg_length;
162
    }
163
    str_charset=cs;
164
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
165
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
166
  bool set(int64_t num, const CHARSET_INFO * const cs)
1 by brian
clean slate
167
  { return set_int(num, false, cs); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
168
  bool set(uint64_t num, const CHARSET_INFO * const cs)
152 by Brian Aker
longlong replacement
169
  { return set_int((int64_t)num, true, cs); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
170
  bool set_real(double num,uint decimals, const CHARSET_INFO * const cs);
1 by brian
clean slate
171
172
  /*
173
    PMG 2004.11.12
174
    This is a method that works the same as perl's "chop". It simply
175
    drops the last character of a string. This is useful in the case
176
    of the federated storage handler where I'm building a unknown
177
    number, list of values and fields to be used in a sql insert
178
    statement to be run on the remote server, and have a comma after each.
179
    When the list is complete, I "chop" off the trailing comma
180
181
    ex. 
182
      String stringobj; 
183
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
184
      stringobj.chop();
185
      stringobj.append(")");
186
187
    In this case, the value of string was:
188
189
    VALUES ('foo', 'fi', 'fo',
190
    VALUES ('foo', 'fi', 'fo'
191
    VALUES ('foo', 'fi', 'fo')
192
      
193
  */
194
  inline void chop()
195
  {
196
    Ptr[str_length--]= '\0'; 
197
  }
198
199
  inline void free()
200
  {
201
    if (alloced)
202
    {
203
      alloced=0;
204
      Alloced_length=0;
205
      my_free(Ptr,MYF(0));
206
      Ptr=0;
207
      str_length=0;				/* Safety */
208
    }
209
  }
205 by Brian Aker
uint32 -> uin32_t
210
  inline bool alloc(uint32_t arg_length)
1 by brian
clean slate
211
  {
212
    if (arg_length < Alloced_length)
213
      return 0;
214
    return real_alloc(arg_length);
215
  }
205 by Brian Aker
uint32 -> uin32_t
216
  bool real_alloc(uint32_t arg_length);			// Empties old string
217
  bool realloc(uint32_t arg_length);
218
  inline void shrink(uint32_t arg_length)		// Shrink buffer
1 by brian
clean slate
219
  {
220
    if (arg_length < Alloced_length)
221
    {
222
      char *new_ptr;
223
      if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
224
      {
225
	Alloced_length = 0;
226
	real_alloc(arg_length);
227
      }
228
      else
229
      {
230
	Ptr=new_ptr;
231
	Alloced_length=arg_length;
232
      }
233
    }
234
  }
235
  bool is_alloced() { return alloced; }
236
  inline String& operator = (const String &s)
237
  {
238
    if (&s != this)
239
    {
240
      /*
241
        It is forbidden to do assignments like 
242
        some_string = substring_of_that_string
243
       */
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
244
      assert(!s.uses_buffer_owned_by(this));
1 by brian
clean slate
245
      free();
246
      Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
247
      alloced=0;
248
    }
249
    return *this;
250
  }
251
252
  bool copy();					// Alloc string if not alloced
253
  bool copy(const String &s);			// Allocate new string
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
254
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);	// Allocate new string
205 by Brian Aker
uint32 -> uin32_t
255
  static bool needs_conversion(uint32_t arg_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
256
  			       const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
205 by Brian Aker
uint32 -> uin32_t
257
			       uint32_t *offset);
258
  bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
259
		    const CHARSET_INFO * const cs);
260
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
261
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
262
	    const CHARSET_INFO * const csto, uint *errors);
1 by brian
clean slate
263
  bool append(const String &s);
264
  bool append(const char *s);
205 by Brian Aker
uint32 -> uin32_t
265
  bool append(const char *s,uint32_t arg_length);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
266
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
205 by Brian Aker
uint32 -> uin32_t
267
  bool append(IO_CACHE* file, uint32_t arg_length);
268
  bool append_with_prefill(const char *s, uint32_t arg_length, 
269
			   uint32_t full_length, char fill_char);
270
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
271
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
272
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
273
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
1 by brian
clean slate
274
  inline bool append(char chr)
275
  {
276
    if (str_length < Alloced_length)
277
    {
278
      Ptr[str_length++]=chr;
279
    }
280
    else
281
    {
282
      if (realloc(str_length+1))
283
	return 1;
284
      Ptr[str_length++]=chr;
285
    }
286
    return 0;
287
  }
205 by Brian Aker
uint32 -> uin32_t
288
  bool fill(uint32_t max_length,char fill);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
289
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
1 by brian
clean slate
290
  friend int stringcmp(const String *a,const String *b);
205 by Brian Aker
uint32 -> uin32_t
291
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
292
  uint32_t numchars();
293
  int charpos(int i,uint32_t offset=0);
1 by brian
clean slate
294
205 by Brian Aker
uint32 -> uin32_t
295
  int reserve(uint32_t space_needed)
1 by brian
clean slate
296
  {
297
    return realloc(str_length + space_needed);
298
  }
205 by Brian Aker
uint32 -> uin32_t
299
  int reserve(uint32_t space_needed, uint32_t grow_by);
1 by brian
clean slate
300
301
  /*
302
    The following append operations do NOT check alloced memory
303
    q_*** methods writes values of parameters itself
304
    qs_*** methods writes string representation of value
305
  */
306
  void q_append(const char c)
307
  {
308
    Ptr[str_length++] = c;
309
  }
205 by Brian Aker
uint32 -> uin32_t
310
  void q_append(const uint32_t n)
1 by brian
clean slate
311
  {
312
    int4store(Ptr + str_length, n);
313
    str_length += 4;
314
  }
315
  void q_append(double d)
316
  {
317
    float8store(Ptr + str_length, d);
318
    str_length += 8;
319
  }
320
  void q_append(double *d)
321
  {
322
    float8store(Ptr + str_length, *d);
323
    str_length += 8;
324
  }
205 by Brian Aker
uint32 -> uin32_t
325
  void q_append(const char *data, uint32_t data_len)
1 by brian
clean slate
326
  {
327
    memcpy(Ptr + str_length, data, data_len);
328
    str_length += data_len;
329
  }
330
205 by Brian Aker
uint32 -> uin32_t
331
  void write_at_position(int position, uint32_t value)
1 by brian
clean slate
332
  {
333
    int4store(Ptr + position,value);
334
  }
335
205 by Brian Aker
uint32 -> uin32_t
336
  void qs_append(const char *str, uint32_t len);
1 by brian
clean slate
337
  void qs_append(double d);
338
  void qs_append(double *d);
339
  inline void qs_append(const char c)
340
  {
341
     Ptr[str_length]= c;
342
     str_length++;
343
  }
344
  void qs_append(int i);
345
  void qs_append(uint i);
346
347
  /* Inline (general) functions used by the protocol functions */
348
205 by Brian Aker
uint32 -> uin32_t
349
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
350
  {
205 by Brian Aker
uint32 -> uin32_t
351
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
352
    if (new_length > Alloced_length)
353
    {
354
      if (realloc(new_length + step_alloc))
355
        return 0;
356
    }
205 by Brian Aker
uint32 -> uin32_t
357
    uint32_t old_length= str_length;
1 by brian
clean slate
358
    str_length+= arg_length;
359
    return Ptr+ old_length;			/* Area to use */
360
  }
361
205 by Brian Aker
uint32 -> uin32_t
362
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
363
  {
205 by Brian Aker
uint32 -> uin32_t
364
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
365
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
366
      return true;
1 by brian
clean slate
367
    memcpy(Ptr+str_length, s, arg_length);
368
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
369
    return false;
1 by brian
clean slate
370
  }
371
  void print(String *print);
372
373
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
374
  void swap(String &s);
375
376
  inline bool uses_buffer_owned_by(const String *s) const
377
  {
378
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
379
  }
380
};
381
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
382
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
1 by brian
clean slate
383
                                           char *end)
384
{
385
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
386
}
387
388
inline
389
bool operator==(const String &s1, const String &s2)
390
{
391
  return stringcmp(&s1,&s2) == 0;
392
}
393
394
inline
395
bool operator!=(const String &s1, const String &s2)
396
{
397
  return !(s1 == s2);
398
}
399
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
400
#endif /* DRIZZLE_SERVER_SQL_STRING_H */