~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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
 */
1 by brian
clean slate
19
1122.2.10 by Monty Taylor
Fixed all of the include guards.
20
#ifndef DRIZZLED_SQL_STRING_H
21
#define DRIZZLED_SQL_STRING_H
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
22
1 by brian
clean slate
23
/* This file is originally from the mysql distribution. Coded by monty */
24
543 by Monty Taylor
Renamed drizzle_common again. Removed sql_common. (empty)
25
#include <drizzled/common.h>
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
26
#include <cassert>
27
#include <cstdlib>
28
#include <cstring>
1271.2.4 by Tim Penhey
Add some standard converstion functions.
29
#include <string>
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
30
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
31
#ifndef NOT_FIXED_DEC
32
#define NOT_FIXED_DEC			(uint8_t)31
33
#endif
34
35
namespace drizzled
36
{
37
1 by brian
clean slate
38
class String;
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
39
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
40
extern String my_empty_string;
41
extern const String my_null_string;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
42
namespace memory { class Root; }
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
43
typedef struct charset_info_st CHARSET_INFO;
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
44
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
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);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
50
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
51
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
52
                                 char *to, size_t to_length,
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
53
                                 const CHARSET_INFO * const from_cs,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
54
                                 const char *from, size_t from_length,
55
                                 size_t nchars,
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
56
                                 const char **well_formed_error_pos,
57
                                 const char **cannot_convert_error_pos,
58
                                 const char **from_end_pos);
59
1 by brian
clean slate
60
61
class String
62
{
511.2.4 by Monty Taylor
Fixed warnings before we got to sql_yacc.
63
  char *Ptr;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
64
  size_t str_length,Alloced_length;
1 by brian
clean slate
65
  bool alloced;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
66
  const CHARSET_INFO *str_charset;
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
67
1 by brian
clean slate
68
public:
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
69
  String();
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
70
  String(size_t length_arg);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
71
  String(const char *str, const CHARSET_INFO * const cs);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
72
  String(const char *str, size_t len, const CHARSET_INFO * const cs);
73
  String(char *str, size_t len, const CHARSET_INFO * const cs);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
74
  String(const String &str);
75
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
76
  static void *operator new(size_t size, memory::Root *mem_root);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
77
  static void operator delete(void *, size_t)
78
  { }
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
79
  static void operator delete(void *, memory::Root *)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
80
  { }
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
81
  ~String();
1 by brian
clean slate
82
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
83
  inline void set_charset(const CHARSET_INFO * const charset_arg)
1 by brian
clean slate
84
  { str_charset= charset_arg; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
85
  inline const CHARSET_INFO *charset() const { return str_charset; }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
86
  inline size_t length() const { return str_length;}
87
  inline size_t alloced_length() const { return Alloced_length;}
88
  inline char& operator [] (size_t i) const { return Ptr[i]; }
89
  inline void length(size_t len) { str_length=len ; }
1 by brian
clean slate
90
  inline bool is_empty() { return (str_length == 0); }
91
  inline void mark_as_const() { Alloced_length= 0;}
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
92
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
93
  inline const char *ptr() const { return Ptr; }
94
  inline char *c_ptr()
95
  {
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
96
    if (str_length == Alloced_length)
1 by brian
clean slate
97
      (void) realloc(str_length);
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
98
    else
99
      Ptr[str_length]= 0;
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
100
1 by brian
clean slate
101
    return Ptr;
102
  }
103
  inline char *c_ptr_quick()
104
  {
105
    if (Ptr && str_length < Alloced_length)
106
      Ptr[str_length]=0;
107
    return Ptr;
108
  }
109
  inline char *c_ptr_safe()
110
  {
111
    if (Ptr && str_length < Alloced_length)
112
      Ptr[str_length]=0;
113
    else
114
      (void) realloc(str_length);
115
    return Ptr;
116
  }
1273.13.38 by Brian Aker
Add in new show work.
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
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
125
  void append_identifier(const char *name, size_t length);
1 by brian
clean slate
126
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
127
  void set(String &str,size_t offset,size_t arg_length)
1 by brian
clean slate
128
  {
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
129
    assert(&str != this);
1 by brian
clean slate
130
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
131
    Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
1 by brian
clean slate
132
    if (str.Alloced_length)
133
      Alloced_length=str.Alloced_length-offset;
134
    else
135
      Alloced_length=0;
136
    str_charset=str.str_charset;
137
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
138
  inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
139
  {
140
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
141
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
1 by brian
clean slate
142
    str_charset=cs;
143
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
144
  inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
145
  {
146
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
147
    Ptr= const_cast<char*>(str);
148
    str_length=arg_length; Alloced_length=0 ; alloced=0;
1 by brian
clean slate
149
    str_charset=cs;
150
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
151
  bool set_ascii(const char *str, size_t arg_length);
152
  inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
153
  {
154
    if (!alloced)
155
    {
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
156
      Ptr= str; str_length= Alloced_length= arg_length;
1 by brian
clean slate
157
    }
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
158
    str_charset= cs;
1 by brian
clean slate
159
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
160
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
161
  bool set(int64_t num, const CHARSET_INFO * const cs)
1 by brian
clean slate
162
  { return set_int(num, false, cs); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
163
  bool set(uint64_t num, const CHARSET_INFO * const cs)
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
164
  { return set_int(static_cast<int64_t>(num), true, cs); }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
165
  bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
1 by brian
clean slate
166
167
  /*
168
    PMG 2004.11.12
169
    This is a method that works the same as perl's "chop". It simply
170
    drops the last character of a string. This is useful in the case
171
    of the federated storage handler where I'm building a unknown
172
    number, list of values and fields to be used in a sql insert
173
    statement to be run on the remote server, and have a comma after each.
174
    When the list is complete, I "chop" off the trailing comma
175
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
176
    ex.
177
      String stringobj;
1 by brian
clean slate
178
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
179
      stringobj.chop();
180
      stringobj.append(")");
181
182
    In this case, the value of string was:
183
184
    VALUES ('foo', 'fi', 'fo',
185
    VALUES ('foo', 'fi', 'fo'
186
    VALUES ('foo', 'fi', 'fo')
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
187
1 by brian
clean slate
188
  */
189
  inline void chop()
190
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
191
    Ptr[str_length--]= '\0';
1 by brian
clean slate
192
  }
193
194
  inline void free()
195
  {
196
    if (alloced)
197
    {
198
      alloced=0;
199
      Alloced_length=0;
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
200
      ::free(Ptr);
1 by brian
clean slate
201
      Ptr=0;
202
      str_length=0;				/* Safety */
203
    }
204
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
205
  inline bool alloc(size_t arg_length)
1 by brian
clean slate
206
  {
207
    if (arg_length < Alloced_length)
208
      return 0;
209
    return real_alloc(arg_length);
210
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
211
  bool real_alloc(size_t arg_length);			// Empties old string
212
  bool realloc(size_t arg_length);
213
  inline void shrink(size_t arg_length)		// Shrink buffer
1 by brian
clean slate
214
  {
215
    if (arg_length < Alloced_length)
216
    {
217
      char *new_ptr;
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
218
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
1 by brian
clean slate
219
      {
2023.2.7 by Brian Aker
Create an Bool primtive type for item.
220
        Alloced_length = 0;
221
        real_alloc(arg_length);
1 by brian
clean slate
222
      }
223
      else
224
      {
2023.2.7 by Brian Aker
Create an Bool primtive type for item.
225
        Ptr=new_ptr;
226
        Alloced_length=arg_length;
1 by brian
clean slate
227
      }
228
    }
229
  }
230
  bool is_alloced() { return alloced; }
231
  inline String& operator = (const String &s)
232
  {
233
    if (&s != this)
234
    {
235
      /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
236
        It is forbidden to do assignments like
1 by brian
clean slate
237
        some_string = substring_of_that_string
238
       */
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
239
      assert(!s.uses_buffer_owned_by(this));
1 by brian
clean slate
240
      free();
241
      Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
242
      alloced=0;
243
    }
244
    return *this;
245
  }
246
247
  bool copy();					// Alloc string if not alloced
248
  bool copy(const String &s);			// Allocate new string
2039.6.4 by Brian Aker
Merge in local_instance change.
249
  bool copy(const std::string&, const CHARSET_INFO * const cs);	// Allocate new string
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
250
  bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs);	// Allocate new string
251
  static bool needs_conversion(size_t arg_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
252
  			       const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
253
			       size_t *offset);
254
  bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
255
  bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
2039.6.4 by Brian Aker
Merge in local_instance change.
256
            const CHARSET_INFO * const csto, size_t *errors);
1 by brian
clean slate
257
  bool append(const String &s);
258
  bool append(const char *s);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
259
  bool append(const char *s,size_t arg_length);
260
  bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
261
  bool append_with_prefill(const char *s, size_t arg_length,
262
			   size_t full_length, char fill_char);
263
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
264
  int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
265
  bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
266
  bool replace(size_t offset,size_t arg_length,const String &to);
1 by brian
clean slate
267
  inline bool append(char chr)
268
  {
269
    if (str_length < Alloced_length)
270
    {
271
      Ptr[str_length++]=chr;
272
    }
273
    else
274
    {
275
      if (realloc(str_length+1))
2023.2.7 by Brian Aker
Create an Bool primtive type for item.
276
        return 1;
1 by brian
clean slate
277
      Ptr[str_length++]=chr;
278
    }
279
    return 0;
280
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
281
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
1 by brian
clean slate
282
  friend int stringcmp(const String *a,const String *b);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
283
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
284
  size_t numchars();
285
  int charpos(int i,size_t offset=0);
1 by brian
clean slate
286
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
287
  int reserve(size_t space_needed)
1 by brian
clean slate
288
  {
289
    return realloc(str_length + space_needed);
290
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
291
  int reserve(size_t space_needed, size_t grow_by);
1 by brian
clean slate
292
293
  /*
294
    The following append operations do NOT check alloced memory
295
    q_*** methods writes values of parameters itself
296
    qs_*** methods writes string representation of value
297
  */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
298
  void q_append(const char c);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
299
  void q_append(const size_t n);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
300
  void q_append(double d);
301
  void q_append(double *d);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
302
  void q_append(const char *data, size_t data_len);
303
  void write_at_position(int position, size_t value);
1 by brian
clean slate
304
305
  /* Inline (general) functions used by the protocol functions */
306
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
307
  inline char *prep_append(size_t arg_length, size_t step_alloc)
1 by brian
clean slate
308
  {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
309
    size_t new_length= arg_length + str_length;
1 by brian
clean slate
310
    if (new_length > Alloced_length)
311
    {
312
      if (realloc(new_length + step_alloc))
313
        return 0;
314
    }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
315
    size_t old_length= str_length;
1 by brian
clean slate
316
    str_length+= arg_length;
317
    return Ptr+ old_length;			/* Area to use */
318
  }
319
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
320
  inline bool append(const char *s, size_t arg_length, size_t step_alloc)
1 by brian
clean slate
321
  {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
322
    size_t new_length= arg_length + str_length;
1 by brian
clean slate
323
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
324
      return true;
1 by brian
clean slate
325
    memcpy(Ptr+str_length, s, arg_length);
326
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
327
    return false;
1 by brian
clean slate
328
  }
329
  void print(String *print);
330
331
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
332
  void swap(String &s);
333
334
  inline bool uses_buffer_owned_by(const String *s) const
335
  {
336
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
337
  }
338
};
339
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
340
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
341
                             char *end);
1 by brian
clean slate
342
2023.2.1 by Brian Aker
Merge in BOOL type.
343
std::ostream& operator<<(std::ostream& output, const String &str);
344
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
345
} /* namespace drizzled */
346
347
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
348
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
349
1 by brian
clean slate
350
1122.2.10 by Monty Taylor
Fixed all of the include guards.
351
#endif /* DRIZZLED_SQL_STRING_H */