~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef DRIZZLE_SERVER_SQL_STRING_H
 
21
#define DRIZZLE_SERVER_SQL_STRING_H
15
22
 
16
23
/* This file is originally from the mysql distribution. Coded by monty */
17
24
 
18
 
#ifdef USE_PRAGMA_INTERFACE
19
 
#pragma interface                       /* gcc class implementation */
20
 
#endif
21
25
 
22
26
#ifndef NOT_FIXED_DEC
23
27
#define NOT_FIXED_DEC                   31
24
28
#endif
25
29
 
 
30
#include <libdrizzle/drizzle_com.h>
 
31
#include <mysys/iocache.h>
 
32
 
26
33
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,
 
34
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
35
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
36
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
 
37
                        const char *from, uint32_t from_length,
 
38
                        const CHARSET_INFO * const from_cs, uint32_t *errors);
 
39
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
40
                               char *to, uint32_t to_length,
 
41
                               const CHARSET_INFO * const from_cs,
 
42
                               const char *from, uint32_t from_length,
 
43
                               uint32_t nchars,
37
44
                               const char **well_formed_error_pos,
38
45
                               const char **cannot_convert_error_pos,
39
46
                               const char **from_end_pos);
40
 
size_t my_copy_with_hex_escaping(CHARSET_INFO *cs,
 
47
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
41
48
                                 char *dst, size_t dstlen,
42
49
                                 const char *src, size_t srclen);
43
50
 
44
51
class String
45
52
{
46
 
  char *Ptr;
47
 
  uint32 str_length,Alloced_length;
 
53
  const char *Ptr;
 
54
  uint32_t str_length,Alloced_length;
48
55
  bool alloced;
49
 
  CHARSET_INFO *str_charset;
 
56
  const CHARSET_INFO *str_charset;
50
57
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
 
  }
 
58
  String() :
 
59
    Ptr(NULL), str_length(0), Alloced_length(0),
 
60
    alloced(false), str_charset(&my_charset_bin)
 
61
  {}
 
62
 
 
63
  String(uint32_t length_arg) :
 
64
    Ptr(NULL), str_length(0), Alloced_length(0),
 
65
    alloced(false), str_charset(&my_charset_bin)
 
66
  {
 
67
    (void) real_alloc(length_arg);
 
68
  }
 
69
 
 
70
  String(const char *str, const CHARSET_INFO * const cs) :
 
71
    Ptr(str), str_length(0), Alloced_length(0),
 
72
    alloced(false), str_charset(cs)
 
73
  {
 
74
    str_length= strlen(str);
 
75
  }
 
76
 
 
77
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs) :
 
78
    str_length(len), Alloced_length(0), alloced(false),
 
79
    Ptr(str), str_charset(cs)
 
80
  {
 
81
  }
 
82
 
76
83
  String(const String &str)
77
 
  { 
 
84
    str_length(str.Ptr), Alloced_length(0), alloced(false),
 
85
    Ptr(str), str_charset(cs)
 
86
  {
78
87
    Ptr=str.Ptr ; str_length=str.str_length ;
79
 
    Alloced_length=str.Alloced_length; alloced=0; 
 
88
    Alloced_length=str.Alloced_length; alloced=0;
80
89
    str_charset=str.str_charset;
81
90
  }
82
91
  static void *operator new(size_t size, MEM_ROOT *mem_root)
83
92
  { return (void*) alloc_root(mem_root, (uint) size); }
84
 
  static void operator delete(void *ptr_arg __attribute__((__unused__)),
85
 
                              size_t size __attribute__((__unused__)))
 
93
  static void operator delete(void *ptr_arg __attribute__((unused)),
 
94
                              size_t size __attribute__((unused)))
86
95
  { TRASH(ptr_arg, size); }
87
 
  static void operator delete(void *ptr_arg __attribute__((__unused__)),
88
 
                              MEM_ROOT *mem_root __attribute__((__unused__)))
 
96
  static void operator delete(void *ptr_arg __attribute__((unused)),
 
97
                              MEM_ROOT *mem_root __attribute__((unused)))
89
98
  { /* never called */ }
90
99
  ~String() { free(); }
91
100
 
92
 
  inline void set_charset(CHARSET_INFO *charset_arg)
 
101
  inline void set_charset(const CHARSET_INFO * const charset_arg)
93
102
  { 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 ; }
 
103
  inline const CHARSET_INFO *charset() const { return str_charset; }
 
104
  inline uint32_t length() const { return str_length;}
 
105
  inline uint32_t alloced_length() const { return Alloced_length;}
 
106
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
 
107
  inline void length(uint32_t len) { str_length=len ; }
99
108
  inline bool is_empty() { return (str_length == 0); }
100
109
  inline void mark_as_const() { Alloced_length= 0;}
 
110
  inline char *ptr() { return Ptr; }
101
111
  inline const char *ptr() const { return Ptr; }
102
112
  inline char *c_ptr()
103
113
  {
120
130
    return Ptr;
121
131
  }
122
132
 
123
 
  void set(String &str,uint32 offset,uint32 arg_length)
 
133
  void set(String &str,uint32_t offset,uint32_t arg_length)
124
134
  {
125
135
    assert(&str != this);
126
136
    free();
131
141
      Alloced_length=0;
132
142
    str_charset=str.str_charset;
133
143
  }
134
 
  inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
 
144
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
135
145
  {
136
146
    free();
137
147
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
138
148
    str_charset=cs;
139
149
  }
140
 
  inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
 
150
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
141
151
  {
142
152
    free();
143
153
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
144
154
    str_charset=cs;
145
155
  }
146
 
  bool set_ascii(const char *str, uint32 arg_length);
147
 
  inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
 
156
  bool set_ascii(const char *str, uint32_t arg_length);
 
157
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
148
158
  {
149
159
    if (!alloced)
150
160
    {
152
162
    }
153
163
    str_charset=cs;
154
164
  }
155
 
  bool set_int(int64_t num, bool unsigned_flag, CHARSET_INFO *cs);
156
 
  bool set(int64_t num, CHARSET_INFO *cs)
 
165
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
 
166
  bool set(int64_t num, const CHARSET_INFO * const cs)
157
167
  { return set_int(num, false, cs); }
158
 
  bool set(uint64_t num, CHARSET_INFO *cs)
 
168
  bool set(uint64_t num, const CHARSET_INFO * const cs)
159
169
  { return set_int((int64_t)num, true, cs); }
160
 
  bool set_real(double num,uint decimals, CHARSET_INFO *cs);
 
170
  bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
161
171
 
162
172
  /*
163
173
    PMG 2004.11.12
192
202
    {
193
203
      alloced=0;
194
204
      Alloced_length=0;
195
 
      my_free(Ptr,MYF(0));
 
205
      ::free(Ptr);
196
206
      Ptr=0;
197
207
      str_length=0;                             /* Safety */
198
208
    }
199
209
  }
200
 
  inline bool alloc(uint32 arg_length)
 
210
  inline bool alloc(uint32_t arg_length)
201
211
  {
202
212
    if (arg_length < Alloced_length)
203
213
      return 0;
204
214
    return real_alloc(arg_length);
205
215
  }
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
 
216
  bool real_alloc(uint32_t arg_length);                 // Empties old string
 
217
  bool realloc(uint32_t arg_length);
 
218
  inline void shrink(uint32_t arg_length)               // Shrink buffer
209
219
  {
210
220
    if (arg_length < Alloced_length)
211
221
    {
241
251
 
242
252
  bool copy();                                  // Alloc string if not alloced
243
253
  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);
 
254
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);  // Allocate new string
 
255
  static bool needs_conversion(uint32_t arg_length,
 
256
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
 
257
                               uint32_t *offset);
 
258
  bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
 
259
                    const CHARSET_INFO * const cs);
 
260
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
 
261
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
 
262
            const CHARSET_INFO * const csto, uint32_t *errors);
253
263
  bool append(const String &s);
254
264
  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);
 
265
  bool append(const char *s,uint32_t arg_length);
 
266
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
 
267
  bool append(IO_CACHE* file, uint32_t arg_length);
 
268
  bool append_with_prefill(const char *s, uint32_t arg_length, 
 
269
                           uint32_t full_length, char fill_char);
 
270
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
271
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
272
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
 
273
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
264
274
  inline bool append(char chr)
265
275
  {
266
276
    if (str_length < Alloced_length)
275
285
    }
276
286
    return 0;
277
287
  }
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);
 
288
  bool fill(uint32_t max_length,char fill);
 
289
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
281
290
  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);
 
291
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
292
  uint32_t numchars();
 
293
  int charpos(int i,uint32_t offset=0);
285
294
 
286
 
  int reserve(uint32 space_needed)
 
295
  int reserve(uint32_t space_needed)
287
296
  {
288
297
    return realloc(str_length + space_needed);
289
298
  }
290
 
  int reserve(uint32 space_needed, uint32 grow_by);
 
299
  int reserve(uint32_t space_needed, uint32_t grow_by);
291
300
 
292
301
  /*
293
302
    The following append operations do NOT check alloced memory
298
307
  {
299
308
    Ptr[str_length++] = c;
300
309
  }
301
 
  void q_append(const uint32 n)
 
310
  void q_append(const uint32_t n)
302
311
  {
303
312
    int4store(Ptr + str_length, n);
304
313
    str_length += 4;
313
322
    float8store(Ptr + str_length, *d);
314
323
    str_length += 8;
315
324
  }
316
 
  void q_append(const char *data, uint32 data_len)
 
325
  void q_append(const char *data, uint32_t data_len)
317
326
  {
318
327
    memcpy(Ptr + str_length, data, data_len);
319
328
    str_length += data_len;
320
329
  }
321
330
 
322
 
  void write_at_position(int position, uint32 value)
 
331
  void write_at_position(int position, uint32_t value)
323
332
  {
324
333
    int4store(Ptr + position,value);
325
334
  }
326
335
 
327
 
  void qs_append(const char *str, uint32 len);
 
336
  void qs_append(const char *str, uint32_t len);
328
337
  void qs_append(double d);
329
338
  void qs_append(double *d);
330
339
  inline void qs_append(const char c)
333
342
     str_length++;
334
343
  }
335
344
  void qs_append(int i);
336
 
  void qs_append(uint i);
 
345
  void qs_append(uint32_t i);
337
346
 
338
347
  /* Inline (general) functions used by the protocol functions */
339
348
 
340
 
  inline char *prep_append(uint32 arg_length, uint32 step_alloc)
 
349
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
341
350
  {
342
 
    uint32 new_length= arg_length + str_length;
 
351
    uint32_t new_length= arg_length + str_length;
343
352
    if (new_length > Alloced_length)
344
353
    {
345
354
      if (realloc(new_length + step_alloc))
346
355
        return 0;
347
356
    }
348
 
    uint32 old_length= str_length;
 
357
    uint32_t old_length= str_length;
349
358
    str_length+= arg_length;
350
359
    return Ptr+ old_length;                     /* Area to use */
351
360
  }
352
361
 
353
 
  inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
 
362
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
354
363
  {
355
 
    uint32 new_length= arg_length + str_length;
 
364
    uint32_t new_length= arg_length + str_length;
356
365
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
357
366
      return true;
358
367
    memcpy(Ptr+str_length, s, arg_length);
370
379
  }
371
380
};
372
381
 
373
 
static inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str, 
 
382
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
374
383
                                           char *end)
375
384
{
376
385
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
388
397
  return !(s1 == s2);
389
398
}
390
399
 
 
400
#endif /* DRIZZLE_SERVER_SQL_STRING_H */