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