~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: lbieber
  • Date: 2010-10-06 16:34:16 UTC
  • mfrom: (1816.1.3 build)
  • Revision ID: lbieber@orisndriz08-20101006163416-ea0sl59qgpglk21y
Merge Monty - Change the requirement from either libinnodb to libhaildb. Also, tied it to version 2.2
Merge Andrew - fix bug 650935: remove --compress from all clients
Merge Andrew - fix bug 653471: Add -A to drizzle client
Merge Travis - 621861 = To change C structs to C++ classes in Drizzle

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
23
23
/* This file is originally from the mysql distribution. Coded by monty */
24
24
 
25
25
#include <drizzled/common.h>
26
 
 
27
26
#include <cassert>
28
27
#include <cstdlib>
29
28
#include <cstring>
30
29
#include <string>
31
30
 
32
 
#include "drizzled/visibility.h"
33
 
 
34
31
#ifndef NOT_FIXED_DEC
35
32
#define NOT_FIXED_DEC                   (uint8_t)31
36
33
#endif
40
37
 
41
38
class String;
42
39
 
43
 
extern DRIZZLED_API String my_empty_string;
 
40
extern String my_empty_string;
44
41
extern const String my_null_string;
45
42
namespace memory { class Root; }
46
43
typedef struct charset_info_st CHARSET_INFO;
47
44
 
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);
 
45
std::string String_to_std_string(String const& s);
 
46
String* set_String_from_std_string(String* s, std::string const& cs);
50
47
 
51
48
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
52
49
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,
 
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,
56
53
                                 const CHARSET_INFO * const from_cs,
57
 
                                 const char *from, size_t from_length,
58
 
                                 size_t nchars,
 
54
                                 const char *from, uint32_t from_length,
 
55
                                 uint32_t nchars,
59
56
                                 const char **well_formed_error_pos,
60
57
                                 const char **cannot_convert_error_pos,
61
58
                                 const char **from_end_pos);
62
59
 
63
60
 
64
 
class DRIZZLED_API String
 
61
class String
65
62
{
66
63
  char *Ptr;
67
 
  size_t str_length,Alloced_length;
 
64
  uint32_t str_length,Alloced_length;
68
65
  bool alloced;
69
66
  const CHARSET_INFO *str_charset;
70
67
 
71
68
public:
72
69
  String();
73
 
  String(size_t length_arg);
 
70
  String(uint32_t length_arg);
74
71
  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);
 
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);
77
74
  String(const String &str);
78
75
 
79
76
  static void *operator new(size_t size, memory::Root *mem_root);
86
83
  inline void set_charset(const CHARSET_INFO * const charset_arg)
87
84
  { str_charset= charset_arg; }
88
85
  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 ; }
 
86
  inline uint32_t length() const { return str_length;}
 
87
  inline uint32_t alloced_length() const { return Alloced_length;}
 
88
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
 
89
  inline void length(uint32_t len) { str_length=len ; }
93
90
  inline bool is_empty() { return (str_length == 0); }
94
91
  inline void mark_as_const() { Alloced_length= 0;}
95
92
  inline char *ptr() { return Ptr; }
125
122
      (void) realloc(str_length);
126
123
    return Ptr;
127
124
  }
128
 
  void append_identifier(const char *name, size_t length);
 
125
  void append_identifier(const char *name, uint32_t length);
129
126
 
130
 
  void set(String &str,size_t offset,size_t arg_length)
 
127
  void set(String &str,uint32_t offset,uint32_t arg_length)
131
128
  {
132
129
    assert(&str != this);
133
130
    free();
138
135
      Alloced_length=0;
139
136
    str_charset=str.str_charset;
140
137
  }
141
 
  inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
138
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
142
139
  {
143
140
    free();
144
141
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
145
142
    str_charset=cs;
146
143
  }
147
 
  inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
144
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
148
145
  {
149
146
    free();
150
147
    Ptr= const_cast<char*>(str);
151
148
    str_length=arg_length; Alloced_length=0 ; alloced=0;
152
149
    str_charset=cs;
153
150
  }
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)
 
151
  bool set_ascii(const char *str, uint32_t arg_length);
 
152
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
156
153
  {
157
154
    if (!alloced)
158
155
    {
165
162
  { return set_int(num, false, cs); }
166
163
  bool set(uint64_t num, const CHARSET_INFO * const cs)
167
164
  { return set_int(static_cast<int64_t>(num), true, cs); }
168
 
  bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
 
165
  bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
169
166
 
170
167
  /*
171
168
    PMG 2004.11.12
205
202
      str_length=0;                             /* Safety */
206
203
    }
207
204
  }
208
 
  inline bool alloc(size_t arg_length)
 
205
  inline bool alloc(uint32_t arg_length)
209
206
  {
210
207
    if (arg_length < Alloced_length)
211
208
      return 0;
212
209
    return real_alloc(arg_length);
213
210
  }
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
 
211
  bool real_alloc(uint32_t arg_length);                 // Empties old string
 
212
  bool realloc(uint32_t arg_length);
 
213
  inline void shrink(uint32_t arg_length)               // Shrink buffer
217
214
  {
218
215
    if (arg_length < Alloced_length)
219
216
    {
220
217
      char *new_ptr;
221
218
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
222
219
      {
223
 
        Alloced_length = 0;
224
 
        real_alloc(arg_length);
 
220
        Alloced_length = 0;
 
221
        real_alloc(arg_length);
225
222
      }
226
223
      else
227
224
      {
228
 
        Ptr=new_ptr;
229
 
        Alloced_length=arg_length;
 
225
        Ptr=new_ptr;
 
226
        Alloced_length=arg_length;
230
227
      }
231
228
    }
232
229
  }
249
246
 
250
247
  bool copy();                                  // Alloc string if not alloced
251
248
  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,
 
249
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);  // Allocate new string
 
250
  static bool needs_conversion(uint32_t arg_length,
255
251
                               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);
 
252
                               uint32_t *offset);
 
253
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
 
254
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
 
255
            const CHARSET_INFO * const csto, uint32_t *errors);
260
256
  bool append(const String &s);
261
257
  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);
 
258
  bool append(const char *s,uint32_t arg_length);
 
259
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
 
260
  bool append_with_prefill(const char *s, uint32_t arg_length,
 
261
                           uint32_t full_length, char fill_char);
 
262
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
263
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
264
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
 
265
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
270
266
  inline bool append(char chr)
271
267
  {
272
268
    if (str_length < Alloced_length)
276
272
    else
277
273
    {
278
274
      if (realloc(str_length+1))
279
 
        return 1;
 
275
        return 1;
280
276
      Ptr[str_length++]=chr;
281
277
    }
282
278
    return 0;
283
279
  }
284
280
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
285
281
  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);
 
282
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
283
  uint32_t numchars();
 
284
  int charpos(int i,uint32_t offset=0);
289
285
 
290
 
  int reserve(size_t space_needed)
 
286
  int reserve(uint32_t space_needed)
291
287
  {
292
288
    return realloc(str_length + space_needed);
293
289
  }
294
 
  int reserve(size_t space_needed, size_t grow_by);
 
290
  int reserve(uint32_t space_needed, uint32_t grow_by);
295
291
 
296
292
  /*
297
293
    The following append operations do NOT check alloced memory
299
295
    qs_*** methods writes string representation of value
300
296
  */
301
297
  void q_append(const char c);
302
 
  void q_append(const size_t n);
 
298
  void q_append(const uint32_t n);
303
299
  void q_append(double d);
304
300
  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);
 
301
  void q_append(const char *data, uint32_t data_len);
 
302
  void write_at_position(int position, uint32_t value);
307
303
 
308
304
  /* Inline (general) functions used by the protocol functions */
309
305
 
310
 
  inline char *prep_append(size_t arg_length, size_t step_alloc)
 
306
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
311
307
  {
312
 
    size_t new_length= arg_length + str_length;
 
308
    uint32_t new_length= arg_length + str_length;
313
309
    if (new_length > Alloced_length)
314
310
    {
315
311
      if (realloc(new_length + step_alloc))
316
312
        return 0;
317
313
    }
318
 
    size_t old_length= str_length;
 
314
    uint32_t old_length= str_length;
319
315
    str_length+= arg_length;
320
316
    return Ptr+ old_length;                     /* Area to use */
321
317
  }
322
318
 
323
 
  inline bool append(const char *s, size_t arg_length, size_t step_alloc)
 
319
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
324
320
  {
325
 
    size_t new_length= arg_length + str_length;
 
321
    uint32_t new_length= arg_length + str_length;
326
322
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
327
323
      return true;
328
324
    memcpy(Ptr+str_length, s, arg_length);
343
339
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
344
340
                             char *end);
345
341
 
346
 
std::ostream& operator<<(std::ostream& output, const String &str);
347
 
 
348
342
} /* namespace drizzled */
349
343
 
350
344
bool operator==(const drizzled::String &s1, const drizzled::String &s2);