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