~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_string.h

  • Committer: Brian Aker
  • Date: 2008-07-14 04:46:28 UTC
  • Revision ID: brian@tangent.org-20080714044628-mk3nt2rbaeqt8oe9
Removed oddball types in my_global.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#ifndef DRIZZLE_SERVER_SQL_STRING_H
17
 
#define DRIZZLE_SERVER_SQL_STRING_H
18
 
 
19
16
/* This file is originally from the mysql distribution. Coded by monty */
20
17
 
21
18
#ifdef USE_PRAGMA_INTERFACE
27
24
#endif
28
25
 
29
26
class String;
30
 
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
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, const CHARSET_INFO * const to_cs,
33
 
                        const char *from, uint32_t from_length,
34
 
                        const CHARSET_INFO * const from_cs, uint *errors);
35
 
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
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,
36
33
                               char *to, uint to_length,
37
 
                               const CHARSET_INFO * const from_cs,
 
34
                               CHARSET_INFO *from_cs,
38
35
                               const char *from, uint from_length,
39
36
                               uint nchars,
40
37
                               const char **well_formed_error_pos,
41
38
                               const char **cannot_convert_error_pos,
42
39
                               const char **from_end_pos);
43
 
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
 
40
size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
44
41
                                 char *dst, size_t dstlen,
45
42
                                 const char *src, size_t srclen);
46
43
 
47
44
class String
48
45
{
49
46
  char *Ptr;
50
 
  uint32_t str_length,Alloced_length;
 
47
  uint32 str_length,Alloced_length;
51
48
  bool alloced;
52
 
  const CHARSET_INFO *str_charset;
 
49
  CHARSET_INFO *str_charset;
53
50
public:
54
51
  String()
55
52
  { 
56
53
    Ptr=0; str_length=Alloced_length=0; alloced=0; 
57
54
    str_charset= &my_charset_bin; 
58
55
  }
59
 
  String(uint32_t length_arg)
 
56
  String(uint32 length_arg)
60
57
  { 
61
58
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
62
59
    str_charset= &my_charset_bin;
63
60
  }
64
 
  String(const char *str, const CHARSET_INFO * const cs)
 
61
  String(const char *str, CHARSET_INFO *cs)
65
62
  { 
66
63
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
67
64
    str_charset=cs;
68
65
  }
69
 
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
 
66
  String(const char *str,uint32 len, CHARSET_INFO *cs)
70
67
  { 
71
68
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
72
69
    str_charset=cs;
73
70
  }
74
 
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
 
71
  String(char *str,uint32 len, CHARSET_INFO *cs)
75
72
  { 
76
73
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
77
74
    str_charset=cs;
84
81
  }
85
82
  static void *operator new(size_t size, MEM_ROOT *mem_root)
86
83
  { return (void*) alloc_root(mem_root, (uint) size); }
87
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
88
 
                              size_t size __attribute__((unused)))
 
84
  static void operator delete(void *ptr_arg __attribute__((__unused__)),
 
85
                              size_t size __attribute__((__unused__)))
89
86
  { TRASH(ptr_arg, size); }
90
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
91
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
87
  static void operator delete(void *ptr_arg __attribute__((__unused__)),
 
88
                              MEM_ROOT *mem_root __attribute__((__unused__)))
92
89
  { /* never called */ }
93
90
  ~String() { free(); }
94
91
 
95
 
  inline void set_charset(const CHARSET_INFO * const charset_arg)
 
92
  inline void set_charset(CHARSET_INFO *charset_arg)
96
93
  { str_charset= charset_arg; }
97
 
  inline const CHARSET_INFO *charset() const { return str_charset; }
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 ; }
 
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 ; }
102
99
  inline bool is_empty() { return (str_length == 0); }
103
100
  inline void mark_as_const() { Alloced_length= 0;}
104
 
  inline char *ptr() { return Ptr; }
105
101
  inline const char *ptr() const { return Ptr; }
106
102
  inline char *c_ptr()
107
103
  {
124
120
    return Ptr;
125
121
  }
126
122
 
127
 
  void set(String &str,uint32_t offset,uint32_t arg_length)
 
123
  void set(String &str,uint32 offset,uint32 arg_length)
128
124
  {
129
125
    assert(&str != this);
130
126
    free();
135
131
      Alloced_length=0;
136
132
    str_charset=str.str_charset;
137
133
  }
138
 
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
134
  inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
139
135
  {
140
136
    free();
141
137
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
142
138
    str_charset=cs;
143
139
  }
144
 
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
140
  inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
145
141
  {
146
142
    free();
147
143
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
148
144
    str_charset=cs;
149
145
  }
150
 
  bool set_ascii(const char *str, uint32_t arg_length);
151
 
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
146
  bool set_ascii(const char *str, uint32 arg_length);
 
147
  inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
152
148
  {
153
149
    if (!alloced)
154
150
    {
156
152
    }
157
153
    str_charset=cs;
158
154
  }
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)
 
155
  bool set_int(int64_t num, bool unsigned_flag, CHARSET_INFO *cs);
 
156
  bool set(int64_t num, CHARSET_INFO *cs)
161
157
  { return set_int(num, false, cs); }
162
 
  bool set(uint64_t num, const CHARSET_INFO * const cs)
 
158
  bool set(uint64_t num, CHARSET_INFO *cs)
163
159
  { return set_int((int64_t)num, true, cs); }
164
 
  bool set_real(double num,uint decimals, const CHARSET_INFO * const cs);
 
160
  bool set_real(double num,uint decimals, CHARSET_INFO *cs);
165
161
 
166
162
  /*
167
163
    PMG 2004.11.12
201
197
      str_length=0;                             /* Safety */
202
198
    }
203
199
  }
204
 
  inline bool alloc(uint32_t arg_length)
 
200
  inline bool alloc(uint32 arg_length)
205
201
  {
206
202
    if (arg_length < Alloced_length)
207
203
      return 0;
208
204
    return real_alloc(arg_length);
209
205
  }
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
 
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
213
209
  {
214
210
    if (arg_length < Alloced_length)
215
211
    {
245
241
 
246
242
  bool copy();                                  // Alloc string if not alloced
247
243
  bool copy(const String &s);                   // Allocate new string
248
 
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);  // Allocate new string
249
 
  static bool needs_conversion(uint32_t arg_length,
250
 
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
251
 
                               uint32_t *offset);
252
 
  bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
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);
 
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);
257
253
  bool append(const String &s);
258
254
  bool append(const char *s);
259
 
  bool append(const char *s,uint32_t arg_length);
260
 
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const 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);
 
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);
268
264
  inline bool append(char chr)
269
265
  {
270
266
    if (str_length < Alloced_length)
279
275
    }
280
276
    return 0;
281
277
  }
282
 
  bool fill(uint32_t max_length,char fill);
283
 
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
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);
284
281
  friend int stringcmp(const String *a,const String *b);
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);
 
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);
288
285
 
289
 
  int reserve(uint32_t space_needed)
 
286
  int reserve(uint32 space_needed)
290
287
  {
291
288
    return realloc(str_length + space_needed);
292
289
  }
293
 
  int reserve(uint32_t space_needed, uint32_t grow_by);
 
290
  int reserve(uint32 space_needed, uint32 grow_by);
294
291
 
295
292
  /*
296
293
    The following append operations do NOT check alloced memory
301
298
  {
302
299
    Ptr[str_length++] = c;
303
300
  }
304
 
  void q_append(const uint32_t n)
 
301
  void q_append(const uint32 n)
305
302
  {
306
303
    int4store(Ptr + str_length, n);
307
304
    str_length += 4;
316
313
    float8store(Ptr + str_length, *d);
317
314
    str_length += 8;
318
315
  }
319
 
  void q_append(const char *data, uint32_t data_len)
 
316
  void q_append(const char *data, uint32 data_len)
320
317
  {
321
318
    memcpy(Ptr + str_length, data, data_len);
322
319
    str_length += data_len;
323
320
  }
324
321
 
325
 
  void write_at_position(int position, uint32_t value)
 
322
  void write_at_position(int position, uint32 value)
326
323
  {
327
324
    int4store(Ptr + position,value);
328
325
  }
329
326
 
330
 
  void qs_append(const char *str, uint32_t len);
 
327
  void qs_append(const char *str, uint32 len);
331
328
  void qs_append(double d);
332
329
  void qs_append(double *d);
333
330
  inline void qs_append(const char c)
340
337
 
341
338
  /* Inline (general) functions used by the protocol functions */
342
339
 
343
 
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
 
340
  inline char *prep_append(uint32 arg_length, uint32 step_alloc)
344
341
  {
345
 
    uint32_t new_length= arg_length + str_length;
 
342
    uint32 new_length= arg_length + str_length;
346
343
    if (new_length > Alloced_length)
347
344
    {
348
345
      if (realloc(new_length + step_alloc))
349
346
        return 0;
350
347
    }
351
 
    uint32_t old_length= str_length;
 
348
    uint32 old_length= str_length;
352
349
    str_length+= arg_length;
353
350
    return Ptr+ old_length;                     /* Area to use */
354
351
  }
355
352
 
356
 
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
 
353
  inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
357
354
  {
358
 
    uint32_t new_length= arg_length + str_length;
 
355
    uint32 new_length= arg_length + str_length;
359
356
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
360
 
      return true;
 
357
      return TRUE;
361
358
    memcpy(Ptr+str_length, s, arg_length);
362
359
    str_length+= arg_length;
363
 
    return false;
 
360
    return FALSE;
364
361
  }
365
362
  void print(String *print);
366
363
 
373
370
  }
374
371
};
375
372
 
376
 
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
 
373
static inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str, 
377
374
                                           char *end)
378
375
{
379
376
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
391
388
  return !(s1 == s2);
392
389
}
393
390
 
394
 
#endif /* DRIZZLE_SERVER_SQL_STRING_H */