~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
 
23
23
/* This file is originally from the mysql distribution. Coded by monty */
24
24
 
 
25
 
 
26
#ifndef NOT_FIXED_DEC
 
27
#define NOT_FIXED_DEC                   (uint8_t)31
 
28
#endif
 
29
 
25
30
#include <drizzled/common.h>
26
 
 
27
31
#include <cassert>
28
32
#include <cstdlib>
29
33
#include <cstring>
30
 
#include <string>
31
 
 
32
 
#include <drizzled/visibility.h>
33
 
 
34
 
#ifndef NOT_FIXED_DEC
35
 
#define NOT_FIXED_DEC                   (uint8_t)31
36
 
#endif
37
 
 
38
 
namespace drizzled
39
 
{
40
34
 
41
35
class String;
42
36
 
43
 
extern DRIZZLED_API String my_empty_string;
 
37
extern String my_empty_string;
44
38
extern const String my_null_string;
45
 
namespace memory { class Root; }
 
39
namespace drizzled { namespace memory { class Root; } }
46
40
typedef struct charset_info_st CHARSET_INFO;
47
41
 
48
 
DRIZZLED_API std::string String_to_std_string(String const& s);
49
 
DRIZZLED_API String* set_String_from_std_string(String* s, std::string const& cs);
50
 
 
51
 
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
52
 
int stringcmp(const String *a,const String *b);
53
 
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
54
 
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
55
 
                                 char *to, size_t to_length,
56
 
                                 const CHARSET_INFO * const from_cs,
57
 
                                 const char *from, size_t from_length,
58
 
                                 size_t nchars,
59
 
                                 const char **well_formed_error_pos,
60
 
                                 const char **cannot_convert_error_pos,
61
 
                                 const char **from_end_pos);
62
 
 
63
 
 
64
 
class DRIZZLED_API String
 
42
#if defined(__cplusplus)
 
43
extern "C" {
 
44
#endif
 
45
 
 
46
  int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
47
  int stringcmp(const String *a,const String *b);
 
48
  String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
49
  uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
50
                                   char *to, uint32_t to_length,
 
51
                                   const CHARSET_INFO * const from_cs,
 
52
                                   const char *from, uint32_t from_length,
 
53
                                   uint32_t nchars,
 
54
                                   const char **well_formed_error_pos,
 
55
                                   const char **cannot_convert_error_pos,
 
56
                                   const char **from_end_pos);
 
57
 
 
58
#if defined(__cplusplus)
 
59
}
 
60
#endif
 
61
 
 
62
class String
65
63
{
66
64
  char *Ptr;
67
 
  size_t str_length,Alloced_length;
 
65
  uint32_t str_length,Alloced_length;
68
66
  bool alloced;
69
67
  const CHARSET_INFO *str_charset;
70
68
 
71
69
public:
72
70
  String();
73
 
  String(size_t length_arg);
 
71
  String(uint32_t length_arg);
74
72
  String(const char *str, const CHARSET_INFO * const cs);
75
 
  String(const char *str, size_t len, const CHARSET_INFO * const cs);
76
 
  String(char *str, size_t len, const CHARSET_INFO * const cs);
 
73
  String(const char *str, uint32_t len, const CHARSET_INFO * const cs);
 
74
  String(char *str, uint32_t len, const CHARSET_INFO * const cs);
77
75
  String(const String &str);
78
76
 
79
 
  static void *operator new(size_t size, memory::Root *mem_root);
 
77
  static void *operator new(size_t size, drizzled::memory::Root *mem_root);
80
78
  static void operator delete(void *, size_t)
81
79
  { }
82
 
  static void operator delete(void *, memory::Root *)
 
80
  static void operator delete(void *, drizzled::memory::Root *)
83
81
  { }
84
82
  ~String();
85
83
 
86
84
  inline void set_charset(const CHARSET_INFO * const charset_arg)
87
85
  { str_charset= charset_arg; }
88
86
  inline const CHARSET_INFO *charset() const { return str_charset; }
89
 
  inline size_t length() const { return str_length;}
90
 
  inline size_t alloced_length() const { return Alloced_length;}
91
 
  inline char& operator [] (size_t i) const { return Ptr[i]; }
92
 
  inline void length(size_t len) { str_length=len ; }
 
87
  inline uint32_t length() const { return str_length;}
 
88
  inline uint32_t alloced_length() const { return Alloced_length;}
 
89
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
 
90
  inline void length(uint32_t len) { str_length=len ; }
93
91
  inline bool is_empty() { return (str_length == 0); }
94
92
  inline void mark_as_const() { Alloced_length= 0;}
95
93
  inline char *ptr() { return Ptr; }
117
115
      (void) realloc(str_length);
118
116
    return Ptr;
119
117
  }
120
 
  inline char *c_str()
121
 
  {
122
 
    if (Ptr && str_length < Alloced_length)
123
 
      Ptr[str_length]=0;
124
 
    else
125
 
      (void) realloc(str_length);
126
 
    return Ptr;
127
 
  }
128
 
  void append_identifier(const char *name, size_t length);
 
118
  void append_identifier(const char *name, uint32_t length);
129
119
 
130
 
  void set(String &str,size_t offset,size_t arg_length)
 
120
  void set(String &str,uint32_t offset,uint32_t arg_length)
131
121
  {
132
122
    assert(&str != this);
133
123
    free();
138
128
      Alloced_length=0;
139
129
    str_charset=str.str_charset;
140
130
  }
141
 
  inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
131
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
142
132
  {
143
133
    free();
144
134
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
145
135
    str_charset=cs;
146
136
  }
147
 
  inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
137
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
148
138
  {
149
139
    free();
150
140
    Ptr= const_cast<char*>(str);
151
141
    str_length=arg_length; Alloced_length=0 ; alloced=0;
152
142
    str_charset=cs;
153
143
  }
154
 
  bool set_ascii(const char *str, size_t arg_length);
155
 
  inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
144
  bool set_ascii(const char *str, uint32_t arg_length);
 
145
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
156
146
  {
157
147
    if (!alloced)
158
148
    {
165
155
  { return set_int(num, false, cs); }
166
156
  bool set(uint64_t num, const CHARSET_INFO * const cs)
167
157
  { return set_int(static_cast<int64_t>(num), true, cs); }
168
 
  bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
 
158
  bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
169
159
 
170
160
  /*
171
161
    PMG 2004.11.12
205
195
      str_length=0;                             /* Safety */
206
196
    }
207
197
  }
208
 
  inline bool alloc(size_t arg_length)
 
198
  inline bool alloc(uint32_t arg_length)
209
199
  {
210
200
    if (arg_length < Alloced_length)
211
201
      return 0;
212
202
    return real_alloc(arg_length);
213
203
  }
214
 
  bool real_alloc(size_t arg_length);                   // Empties old string
215
 
  bool realloc(size_t arg_length);
216
 
  inline void shrink(size_t arg_length)         // Shrink buffer
 
204
  bool real_alloc(uint32_t arg_length);                 // Empties old string
 
205
  bool realloc(uint32_t arg_length);
 
206
  inline void shrink(uint32_t arg_length)               // Shrink buffer
217
207
  {
218
208
    if (arg_length < Alloced_length)
219
209
    {
220
210
      char *new_ptr;
221
211
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
222
212
      {
223
 
        Alloced_length = 0;
224
 
        real_alloc(arg_length);
 
213
        Alloced_length = 0;
 
214
        real_alloc(arg_length);
225
215
      }
226
216
      else
227
217
      {
228
 
        Ptr=new_ptr;
229
 
        Alloced_length=arg_length;
 
218
        Ptr=new_ptr;
 
219
        Alloced_length=arg_length;
230
220
      }
231
221
    }
232
222
  }
249
239
 
250
240
  bool copy();                                  // Alloc string if not alloced
251
241
  bool copy(const String &s);                   // Allocate new string
252
 
  bool copy(const std::string&, const CHARSET_INFO * const cs); // Allocate new string
253
 
  bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs);    // Allocate new string
254
 
  static bool needs_conversion(size_t arg_length,
 
242
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);  // Allocate new string
 
243
  static bool needs_conversion(uint32_t arg_length,
255
244
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
256
 
                               size_t *offset);
257
 
  bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
258
 
  bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
259
 
            const CHARSET_INFO * const csto, size_t *errors);
 
245
                               uint32_t *offset);
 
246
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
 
247
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
 
248
            const CHARSET_INFO * const csto, uint32_t *errors);
260
249
  bool append(const String &s);
261
250
  bool append(const char *s);
262
 
  bool append(const char *s,size_t arg_length);
263
 
  bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
264
 
  bool append_with_prefill(const char *s, size_t arg_length,
265
 
                           size_t full_length, char fill_char);
266
 
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
267
 
  int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
268
 
  bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
269
 
  bool replace(size_t offset,size_t arg_length,const String &to);
 
251
  bool append(const char *s,uint32_t arg_length);
 
252
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
 
253
  bool append_with_prefill(const char *s, uint32_t arg_length,
 
254
                           uint32_t full_length, char fill_char);
 
255
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
256
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
257
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
 
258
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
270
259
  inline bool append(char chr)
271
260
  {
272
261
    if (str_length < Alloced_length)
276
265
    else
277
266
    {
278
267
      if (realloc(str_length+1))
279
 
        return 1;
 
268
        return 1;
280
269
      Ptr[str_length++]=chr;
281
270
    }
282
271
    return 0;
283
272
  }
284
273
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
285
274
  friend int stringcmp(const String *a,const String *b);
286
 
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
287
 
  size_t numchars();
288
 
  int charpos(int i,size_t offset=0);
 
275
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
276
  uint32_t numchars();
 
277
  int charpos(int i,uint32_t offset=0);
289
278
 
290
 
  int reserve(size_t space_needed)
 
279
  int reserve(uint32_t space_needed)
291
280
  {
292
281
    return realloc(str_length + space_needed);
293
282
  }
294
 
  int reserve(size_t space_needed, size_t grow_by);
 
283
  int reserve(uint32_t space_needed, uint32_t grow_by);
295
284
 
296
285
  /*
297
286
    The following append operations do NOT check alloced memory
298
287
    q_*** methods writes values of parameters itself
299
288
    qs_*** methods writes string representation of value
300
289
  */
301
 
  void q_append(const char c);
302
 
  void q_append(const size_t n);
303
 
  void q_append(double d);
304
 
  void q_append(double *d);
305
 
  void q_append(const char *data, size_t data_len);
306
 
  void write_at_position(int position, size_t value);
 
290
  void q_append(const char c)
 
291
  {
 
292
    Ptr[str_length++] = c;
 
293
  }
 
294
  void q_append(const uint32_t n)
 
295
  {
 
296
    int4store(Ptr + str_length, n);
 
297
    str_length += 4;
 
298
  }
 
299
  void q_append(double d)
 
300
  {
 
301
    float8store(Ptr + str_length, d);
 
302
    str_length += 8;
 
303
  }
 
304
  void q_append(double *d)
 
305
  {
 
306
    float8store(Ptr + str_length, *d);
 
307
    str_length += 8;
 
308
  }
 
309
  void q_append(const char *data, uint32_t data_len)
 
310
  {
 
311
    memcpy(Ptr + str_length, data, data_len);
 
312
    str_length += data_len;
 
313
  }
 
314
 
 
315
  void write_at_position(int position, uint32_t value)
 
316
  {
 
317
    int4store(Ptr + position,value);
 
318
  }
307
319
 
308
320
  /* Inline (general) functions used by the protocol functions */
309
321
 
310
 
  inline char *prep_append(size_t arg_length, size_t step_alloc)
 
322
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
311
323
  {
312
 
    size_t new_length= arg_length + str_length;
 
324
    uint32_t new_length= arg_length + str_length;
313
325
    if (new_length > Alloced_length)
314
326
    {
315
327
      if (realloc(new_length + step_alloc))
316
328
        return 0;
317
329
    }
318
 
    size_t old_length= str_length;
 
330
    uint32_t old_length= str_length;
319
331
    str_length+= arg_length;
320
332
    return Ptr+ old_length;                     /* Area to use */
321
333
  }
322
334
 
323
 
  inline bool append(const char *s, size_t arg_length, size_t step_alloc)
 
335
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
324
336
  {
325
 
    size_t new_length= arg_length + str_length;
 
337
    uint32_t new_length= arg_length + str_length;
326
338
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
327
339
      return true;
328
340
    memcpy(Ptr+str_length, s, arg_length);
343
355
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
344
356
                             char *end);
345
357
 
346
 
std::ostream& operator<<(std::ostream& output, const String &str);
347
 
 
348
 
} /* namespace drizzled */
349
 
 
350
 
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
351
 
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
352
 
 
 
358
extern "C++" {
 
359
bool operator==(const String &s1, const String &s2);
 
360
bool operator!=(const String &s1, const String &s2);
 
361
}
353
362
 
354
363
#endif /* DRIZZLED_SQL_STRING_H */