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