~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;
30
int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
205 by Brian Aker
uint32 -> uin32_t
31
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
32
uint32_t copy_and_convert(char *to, uint32_t to_length, CHARSET_INFO *to_cs,
33
			const char *from, uint32_t from_length,
1 by brian
clean slate
34
			CHARSET_INFO *from_cs, uint *errors);
205 by Brian Aker
uint32 -> uin32_t
35
uint32_t well_formed_copy_nchars(CHARSET_INFO *to_cs,
1 by brian
clean slate
36
                               char *to, uint to_length,
37
                               CHARSET_INFO *from_cs,
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);
43
size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
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;
52
  CHARSET_INFO *str_charset;
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
  }
64
  String(const char *str, CHARSET_INFO *cs)
65
  { 
66
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
67
    str_charset=cs;
68
  }
205 by Brian Aker
uint32 -> uin32_t
69
  String(const char *str,uint32_t len, CHARSET_INFO *cs)
1 by brian
clean slate
70
  { 
71
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
72
    str_charset=cs;
73
  }
205 by Brian Aker
uint32 -> uin32_t
74
  String(char *str,uint32_t len, CHARSET_INFO *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
95
  inline void set_charset(CHARSET_INFO *charset_arg)
96
  { str_charset= charset_arg; }
97
  inline 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
  }
205 by Brian Aker
uint32 -> uin32_t
138
  inline void set(char *str,uint32_t arg_length, CHARSET_INFO *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
  }
205 by Brian Aker
uint32 -> uin32_t
144
  inline void set(const char *str,uint32_t arg_length, CHARSET_INFO *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);
151
  inline void set_quick(char *str,uint32_t arg_length, CHARSET_INFO *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
  }
152 by Brian Aker
longlong replacement
159
  bool set_int(int64_t num, bool unsigned_flag, CHARSET_INFO *cs);
160
  bool set(int64_t num, CHARSET_INFO *cs)
1 by brian
clean slate
161
  { return set_int(num, false, cs); }
151 by Brian Aker
Ulonglong to uint64_t
162
  bool set(uint64_t num, CHARSET_INFO *cs)
152 by Brian Aker
longlong replacement
163
  { return set_int((int64_t)num, true, cs); }
1 by brian
clean slate
164
  bool set_real(double num,uint decimals, CHARSET_INFO *cs);
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
205 by Brian Aker
uint32 -> uin32_t
248
  bool copy(const char *s,uint32_t arg_length, CHARSET_INFO *cs);	// Allocate new string
249
  static bool needs_conversion(uint32_t arg_length,
1 by brian
clean slate
250
  			       CHARSET_INFO *cs_from, CHARSET_INFO *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,
1 by brian
clean slate
253
		    CHARSET_INFO *cs);
205 by Brian Aker
uint32 -> uin32_t
254
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, CHARSET_INFO *cs);
255
  bool copy(const char*s,uint32_t arg_length, CHARSET_INFO *csfrom,
1 by brian
clean slate
256
	    CHARSET_INFO *csto, uint *errors);
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);
260
  bool append(const char *s,uint32_t arg_length, CHARSET_INFO *cs);
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);
1 by brian
clean slate
283
  void strip_sp();
284
  friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
285
  friend int stringcmp(const String *a,const String *b);
205 by Brian Aker
uint32 -> uin32_t
286
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
287
  uint32_t numchars();
288
  int charpos(int i,uint32_t offset=0);
1 by brian
clean slate
289
205 by Brian Aker
uint32 -> uin32_t
290
  int reserve(uint32_t space_needed)
1 by brian
clean slate
291
  {
292
    return realloc(str_length + space_needed);
293
  }
205 by Brian Aker
uint32 -> uin32_t
294
  int reserve(uint32_t space_needed, uint32_t grow_by);
1 by brian
clean slate
295
296
  /*
297
    The following append operations do NOT check alloced memory
298
    q_*** methods writes values of parameters itself
299
    qs_*** methods writes string representation of value
300
  */
301
  void q_append(const char c)
302
  {
303
    Ptr[str_length++] = c;
304
  }
205 by Brian Aker
uint32 -> uin32_t
305
  void q_append(const uint32_t n)
1 by brian
clean slate
306
  {
307
    int4store(Ptr + str_length, n);
308
    str_length += 4;
309
  }
310
  void q_append(double d)
311
  {
312
    float8store(Ptr + str_length, d);
313
    str_length += 8;
314
  }
315
  void q_append(double *d)
316
  {
317
    float8store(Ptr + str_length, *d);
318
    str_length += 8;
319
  }
205 by Brian Aker
uint32 -> uin32_t
320
  void q_append(const char *data, uint32_t data_len)
1 by brian
clean slate
321
  {
322
    memcpy(Ptr + str_length, data, data_len);
323
    str_length += data_len;
324
  }
325
205 by Brian Aker
uint32 -> uin32_t
326
  void write_at_position(int position, uint32_t value)
1 by brian
clean slate
327
  {
328
    int4store(Ptr + position,value);
329
  }
330
205 by Brian Aker
uint32 -> uin32_t
331
  void qs_append(const char *str, uint32_t len);
1 by brian
clean slate
332
  void qs_append(double d);
333
  void qs_append(double *d);
334
  inline void qs_append(const char c)
335
  {
336
     Ptr[str_length]= c;
337
     str_length++;
338
  }
339
  void qs_append(int i);
340
  void qs_append(uint i);
341
342
  /* Inline (general) functions used by the protocol functions */
343
205 by Brian Aker
uint32 -> uin32_t
344
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
345
  {
205 by Brian Aker
uint32 -> uin32_t
346
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
347
    if (new_length > Alloced_length)
348
    {
349
      if (realloc(new_length + step_alloc))
350
        return 0;
351
    }
205 by Brian Aker
uint32 -> uin32_t
352
    uint32_t old_length= str_length;
1 by brian
clean slate
353
    str_length+= arg_length;
354
    return Ptr+ old_length;			/* Area to use */
355
  }
356
205 by Brian Aker
uint32 -> uin32_t
357
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
358
  {
205 by Brian Aker
uint32 -> uin32_t
359
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
360
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
361
      return true;
1 by brian
clean slate
362
    memcpy(Ptr+str_length, s, arg_length);
363
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
364
    return false;
1 by brian
clean slate
365
  }
366
  void print(String *print);
367
368
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
369
  void swap(String &s);
370
371
  inline bool uses_buffer_owned_by(const String *s) const
372
  {
373
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
374
  }
375
};
376
377
static inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str, 
378
                                           char *end)
379
{
380
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
381
}
382
383
inline
384
bool operator==(const String &s1, const String &s2)
385
{
386
  return stringcmp(&s1,&s2) == 0;
387
}
388
389
inline
390
bool operator!=(const String &s1, const String &s2)
391
{
392
  return !(s1 == s2);
393
}
394
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
395
#endif /* DRIZZLE_SERVER_SQL_STRING_H */