~drizzle-trunk/drizzle/development

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