~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Brian Aker
  • Date: 2010-04-05 23:46:43 UTC
  • Revision ID: brian@gaz-20100405234643-0he3xnj902rc70r8
Fixing tests to work with PBXT.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLE_SERVER_SQL_STRING_H
21
 
#define DRIZZLE_SERVER_SQL_STRING_H
 
20
#ifndef DRIZZLED_SQL_STRING_H
 
21
#define DRIZZLED_SQL_STRING_H
22
22
 
23
23
/* This file is originally from the mysql distribution. Coded by monty */
24
24
 
 
25
#include <drizzled/common.h>
 
26
#include <cassert>
 
27
#include <cstdlib>
 
28
#include <cstring>
 
29
#include <string>
25
30
 
26
31
#ifndef NOT_FIXED_DEC
27
32
#define NOT_FIXED_DEC                   (uint8_t)31
28
33
#endif
29
34
 
30
 
#include <drizzled/common.h>
31
 
#include <mysys/iocache.h>
32
 
#include <stdlib.h>
33
 
#include <string.h>
 
35
namespace drizzled
 
36
{
34
37
 
35
38
class String;
36
39
 
37
 
#if defined(__cplusplus)
38
 
extern "C" {
39
 
#endif
40
 
 
41
 
  int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
42
 
  int stringcmp(const String *a,const String *b);
43
 
  String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
44
 
  uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
45
 
                                   char *to, uint32_t to_length,
46
 
                                   const CHARSET_INFO * const from_cs,
47
 
                                   const char *from, uint32_t from_length,
48
 
                                   uint32_t nchars,
49
 
                                   const char **well_formed_error_pos,
50
 
                                   const char **cannot_convert_error_pos,
51
 
                                   const char **from_end_pos);
52
 
  size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
53
 
                                   char *dst, size_t dstlen,
54
 
                                   const char *src, size_t srclen);
55
 
 
56
 
#if defined(__cplusplus)
57
 
}
58
 
#endif
 
40
extern String my_empty_string;
 
41
extern const String my_null_string;
 
42
namespace memory { class Root; }
 
43
typedef struct charset_info_st CHARSET_INFO;
 
44
 
 
45
std::string String_to_std_string(String const& s);
 
46
String* set_String_from_std_string(String* s, std::string const& cs);
 
47
 
 
48
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
49
int stringcmp(const String *a,const String *b);
 
50
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
51
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
52
                                 char *to, uint32_t to_length,
 
53
                                 const CHARSET_INFO * const from_cs,
 
54
                                 const char *from, uint32_t from_length,
 
55
                                 uint32_t nchars,
 
56
                                 const char **well_formed_error_pos,
 
57
                                 const char **cannot_convert_error_pos,
 
58
                                 const char **from_end_pos);
 
59
 
59
60
 
60
61
class String
61
62
{
65
66
  const CHARSET_INFO *str_charset;
66
67
 
67
68
public:
68
 
  String()
69
 
  {
70
 
    Ptr=0; str_length=Alloced_length=0; alloced=0;
71
 
    str_charset= &my_charset_bin;
72
 
  }
73
 
  String(uint32_t length_arg)
74
 
  {
75
 
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
76
 
    str_charset= &my_charset_bin;
77
 
  }
78
 
  String(const char *str, const CHARSET_INFO * const cs)
79
 
  {
80
 
    Ptr=(char*) str; str_length=(uint32_t) strlen(str); Alloced_length=0; alloced=0;
81
 
    str_charset=cs;
82
 
  }
83
 
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
84
 
  {
85
 
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
86
 
    str_charset=cs;
87
 
  }
88
 
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
89
 
  {
90
 
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
91
 
    str_charset=cs;
92
 
  }
93
 
  String(const String &str)
94
 
  {
95
 
    Ptr=str.Ptr ; str_length=str.str_length ;
96
 
    Alloced_length=str.Alloced_length; alloced=0;
97
 
    str_charset=str.str_charset;
98
 
  }
99
 
  static void *operator new(size_t size, MEM_ROOT *mem_root)
100
 
  { return (void*) alloc_root(mem_root, (uint32_t) size); } 
 
69
  String();
 
70
  String(uint32_t length_arg);
 
71
  String(const char *str, const CHARSET_INFO * const cs);
 
72
  String(const char *str, uint32_t len, const CHARSET_INFO * const cs);
 
73
  String(char *str, uint32_t len, const CHARSET_INFO * const cs);
 
74
  String(const String &str);
 
75
 
 
76
  static void *operator new(size_t size, memory::Root *mem_root);
101
77
  static void operator delete(void *, size_t)
102
 
  { TRASH(ptr_arg, size); }
103
 
  static void operator delete(void *, MEM_ROOT *)
104
 
  { /* never called */ }
 
78
  { }
 
79
  static void operator delete(void *, memory::Root *)
 
80
  { }
105
81
  ~String();
106
82
 
107
83
  inline void set_charset(const CHARSET_INFO * const charset_arg)
121
97
      (void) realloc(str_length);
122
98
    else
123
99
      Ptr[str_length]= 0;
124
 
    
 
100
 
125
101
    return Ptr;
126
102
  }
127
103
  inline char *c_ptr_quick()
138
114
      (void) realloc(str_length);
139
115
    return Ptr;
140
116
  }
 
117
  inline char *c_str()
 
118
  {
 
119
    if (Ptr && str_length < Alloced_length)
 
120
      Ptr[str_length]=0;
 
121
    else
 
122
      (void) realloc(str_length);
 
123
    return Ptr;
 
124
  }
141
125
  void append_identifier(const char *name, uint32_t length);
142
126
 
143
127
  void set(String &str,uint32_t offset,uint32_t arg_length)
144
128
  {
145
129
    assert(&str != this);
146
130
    free();
147
 
    Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
 
131
    Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
148
132
    if (str.Alloced_length)
149
133
      Alloced_length=str.Alloced_length-offset;
150
134
    else
154
138
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
155
139
  {
156
140
    free();
157
 
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
 
141
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
158
142
    str_charset=cs;
159
143
  }
160
144
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
161
145
  {
162
146
    free();
163
 
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
 
147
    Ptr= const_cast<char*>(str);
 
148
    str_length=arg_length; Alloced_length=0 ; alloced=0;
164
149
    str_charset=cs;
165
150
  }
166
151
  bool set_ascii(const char *str, uint32_t arg_length);
168
153
  {
169
154
    if (!alloced)
170
155
    {
171
 
      Ptr=(char*) str; str_length=Alloced_length=arg_length;
 
156
      Ptr= str; str_length= Alloced_length= arg_length;
172
157
    }
173
 
    str_charset=cs;
 
158
    str_charset= cs;
174
159
  }
175
160
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
176
161
  bool set(int64_t num, const CHARSET_INFO * const cs)
177
162
  { return set_int(num, false, cs); }
178
163
  bool set(uint64_t num, const CHARSET_INFO * const cs)
179
 
  { return set_int((int64_t)num, true, cs); }
 
164
  { return set_int(static_cast<int64_t>(num), true, cs); }
180
165
  bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
181
166
 
182
167
  /*
230
215
    if (arg_length < Alloced_length)
231
216
    {
232
217
      char *new_ptr;
233
 
      if (!(new_ptr=(char*) ::realloc(Ptr,arg_length)))
 
218
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
234
219
      {
235
220
        Alloced_length = 0;
236
221
        real_alloc(arg_length);
309
294
    q_*** methods writes values of parameters itself
310
295
    qs_*** methods writes string representation of value
311
296
  */
312
 
  void q_append(const char c)
313
 
  {
314
 
    Ptr[str_length++] = c;
315
 
  }
316
 
  void q_append(const uint32_t n)
317
 
  {
318
 
    int4store(Ptr + str_length, n);
319
 
    str_length += 4;
320
 
  }
321
 
  void q_append(double d)
322
 
  {
323
 
    float8store(Ptr + str_length, d);
324
 
    str_length += 8;
325
 
  }
326
 
  void q_append(double *d)
327
 
  {
328
 
    float8store(Ptr + str_length, *d);
329
 
    str_length += 8;
330
 
  }
331
 
  void q_append(const char *data, uint32_t data_len)
332
 
  {
333
 
    memcpy(Ptr + str_length, data, data_len);
334
 
    str_length += data_len;
335
 
  }
336
 
 
337
 
  void write_at_position(int position, uint32_t value)
338
 
  {
339
 
    int4store(Ptr + position,value);
340
 
  }
 
297
  void q_append(const char c);
 
298
  void q_append(const uint32_t n);
 
299
  void q_append(double d);
 
300
  void q_append(double *d);
 
301
  void q_append(const char *data, uint32_t data_len);
 
302
  void write_at_position(int position, uint32_t value);
341
303
 
342
304
  /* Inline (general) functions used by the protocol functions */
343
305
 
374
336
  }
375
337
};
376
338
 
377
 
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
378
 
                                           char *end)
379
 
{
380
 
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
381
 
}
382
 
 
383
 
extern "C++" {
384
 
bool operator==(const String &s1, const String &s2);
385
 
bool operator!=(const String &s1, const String &s2);
386
 
}
387
 
 
388
 
#endif /* DRIZZLE_SERVER_SQL_STRING_H */
 
339
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
 
340
                             char *end);
 
341
 
 
342
} /* namespace drizzled */
 
343
 
 
344
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
 
345
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
 
346
 
 
347
 
 
348
#endif /* DRIZZLED_SQL_STRING_H */