~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
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
21
1 by brian
clean slate
22
/* This file is originally from the mysql distribution. Coded by monty */
23
543 by Monty Taylor
Renamed drizzle_common again. Removed sql_common. (empty)
24
#include <drizzled/common.h>
2318.8.1 by Olaf van der Spek
Add data_ref.h
25
#include <drizzled/util/data_ref.h>
2154.2.3 by Brian Aker
Update headers (more removal of session).
26
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
27
#include <cassert>
28
#include <cstdlib>
29
#include <cstring>
1271.2.4 by Tim Penhey
Add some standard converstion functions.
30
#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...
31
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
32
#include <drizzled/visibility.h>
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
33
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
34
#ifndef NOT_FIXED_DEC
35
#define NOT_FIXED_DEC			(uint8_t)31
36
#endif
37
2252.1.17 by Olaf van der Spek
Common fwd
38
namespace drizzled {
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
39
2119.4.3 by Monty Taylor
We want to add visibility markers into the headers... not take them away
40
extern DRIZZLED_API String my_empty_string;
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
41
extern const String my_null_string;
42
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
43
int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
44
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).
45
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
46
size_t well_formed_copy_nchars(const charset_info_st * const to_cs,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
47
                                 char *to, size_t to_length,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
48
                                 const charset_info_st * const from_cs,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
49
                                 const char *from, size_t from_length,
50
                                 size_t nchars,
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
51
                                 const char **well_formed_error_pos,
52
                                 const char **cannot_convert_error_pos,
53
                                 const char **from_end_pos);
54
1 by brian
clean slate
55
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
56
class DRIZZLED_API String
1 by brian
clean slate
57
{
511.2.4 by Monty Taylor
Fixed warnings before we got to sql_yacc.
58
  char *Ptr;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
59
  size_t str_length,Alloced_length;
1 by brian
clean slate
60
  bool alloced;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
61
  const charset_info_st *str_charset;
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
62
1 by brian
clean slate
63
public:
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
64
  String();
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
65
  String(size_t length_arg);
2440.2.1 by Olaf van der Spek
Use str_ref
66
  String(const char*, const charset_info_st*);
67
  String(const char*, size_t, const charset_info_st*);
68
  String(char *str, size_t len, const charset_info_st*);
69
  String(str_ref, const charset_info_st*);
70
  String(const String&);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
71
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
72
  static void *operator new(size_t size, memory::Root *mem_root);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
73
  static void operator delete(void *, size_t)
74
  { }
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
75
  static void operator delete(void *, memory::Root *)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
76
  { }
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
77
  ~String();
1 by brian
clean slate
78
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
79
  inline void set_charset(const charset_info_st * const charset_arg)
1 by brian
clean slate
80
  { str_charset= charset_arg; }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
81
  inline const charset_info_st *charset() const { return str_charset; }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
82
  inline size_t length() const { return str_length;}
83
  inline size_t alloced_length() const { return Alloced_length;}
84
  inline char& operator [] (size_t i) const { return Ptr[i]; }
2318.7.11 by Olaf van der Spek
Use String::c_str() instead of c_ptr_safe()
85
  inline void length(size_t len) { str_length=len; }
86
  inline bool empty() const { return str_length == 0; }
87
  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().
88
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
89
  inline const char *ptr() const { return Ptr; }
90
  inline char *c_ptr()
91
  {
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
92
    if (str_length == Alloced_length)
2318.7.11 by Olaf van der Spek
Use String::c_str() instead of c_ptr_safe()
93
      realloc(str_length);
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
94
    else
95
      Ptr[str_length]= 0;
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
96
1 by brian
clean slate
97
    return Ptr;
98
  }
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
99
  inline const char* begin() const
100
  {
101
    return Ptr;
102
  }
103
  inline const char* end() const
104
  {
105
    return begin() + size();
106
  }
107
  inline const char* data() const
108
  {
109
    return Ptr;
110
  }
111
  inline size_t size() const
112
  {
113
    return length();
114
  }
115
  inline const char* c_str()
2318.7.11 by Olaf van der Spek
Use String::c_str() instead of c_ptr_safe()
116
  {
117
    if (Ptr && str_length < Alloced_length)
118
      Ptr[str_length]=0;
119
    else
120
      realloc(str_length);
1273.13.38 by Brian Aker
Add in new show work.
121
    return Ptr;
122
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
123
  void append_identifier(const char *name, size_t length);
2318.8.1 by Olaf van der Spek
Add data_ref.h
124
  void append_identifier(str_ref);
1 by brian
clean slate
125
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
126
  void set(String &str,size_t offset,size_t arg_length)
1 by brian
clean slate
127
  {
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
128
    assert(&str != this);
1 by brian
clean slate
129
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
130
    Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
1 by brian
clean slate
131
    if (str.Alloced_length)
132
      Alloced_length=str.Alloced_length-offset;
133
    else
134
      Alloced_length=0;
135
    str_charset=str.str_charset;
136
  }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
137
  inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
1 by brian
clean slate
138
  {
139
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
140
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
1 by brian
clean slate
141
    str_charset=cs;
142
  }
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
143
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
144
  inline void set(const char *str,size_t arg_length, const charset_info_st * 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
  }
2281.6.1 by Olaf van der Spek
Return void
151
  void set_ascii(const char *str, size_t arg_length);
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
152
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
153
  inline void set_quick(char *str,size_t arg_length, const charset_info_st * const cs)
1 by brian
clean slate
154
  {
155
    if (!alloced)
156
    {
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
157
      Ptr= str; str_length= Alloced_length= arg_length;
1 by brian
clean slate
158
    }
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
159
    str_charset= cs;
1 by brian
clean slate
160
  }
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
161
2297.1.5 by Olaf van der Spek
Return void
162
  void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
163
  void set(int64_t num, const charset_info_st * const cs)
164
  { set_int(num, false, cs); }
165
  void set(uint64_t num, const charset_info_st * const cs)
166
  { set_int(static_cast<int64_t>(num), true, cs); }
2281.6.1 by Olaf van der Spek
Return void
167
  void set_real(double num,size_t decimals, const charset_info_st* cs);
1 by brian
clean slate
168
169
  /*
170
    PMG 2004.11.12
171
    This is a method that works the same as perl's "chop". It simply
172
    drops the last character of a string. This is useful in the case
173
    of the federated storage handler where I'm building a unknown
174
    number, list of values and fields to be used in a sql insert
175
    statement to be run on the remote server, and have a comma after each.
176
    When the list is complete, I "chop" off the trailing comma
177
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
178
    ex.
179
      String stringobj;
1 by brian
clean slate
180
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
181
      stringobj.chop();
182
      stringobj.append(")");
183
184
    In this case, the value of string was:
185
186
    VALUES ('foo', 'fi', 'fo',
187
    VALUES ('foo', 'fi', 'fo'
188
    VALUES ('foo', 'fi', 'fo')
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
189
1 by brian
clean slate
190
  */
191
  inline void chop()
192
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
193
    Ptr[str_length--]= '\0';
1 by brian
clean slate
194
  }
195
196
  inline void free()
197
  {
198
    if (alloced)
199
    {
200
      alloced=0;
201
      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.
202
      ::free(Ptr);
1 by brian
clean slate
203
      Ptr=0;
204
      str_length=0;				/* Safety */
205
    }
206
  }
2275.2.12 by Olaf van der Spek
Return void
207
  inline void alloc(size_t arg_length)
1 by brian
clean slate
208
  {
2275.2.12 by Olaf van der Spek
Return void
209
    if (arg_length >= Alloced_length)
210
      real_alloc(arg_length);
1 by brian
clean slate
211
  }
2275.2.12 by Olaf van der Spek
Return void
212
  void real_alloc(size_t arg_length);			// Empties old string
2281.6.3 by Olaf van der Spek
Return void
213
  void realloc(size_t arg_length);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
214
  inline void shrink(size_t arg_length)		// Shrink buffer
1 by brian
clean slate
215
  {
216
    if (arg_length < Alloced_length)
217
    {
218
      char *new_ptr;
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
219
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
1 by brian
clean slate
220
      {
2023.2.7 by Brian Aker
Create an Bool primtive type for item.
221
        Alloced_length = 0;
222
        real_alloc(arg_length);
1 by brian
clean slate
223
      }
224
      else
225
      {
2023.2.7 by Brian Aker
Create an Bool primtive type for item.
226
        Ptr=new_ptr;
227
        Alloced_length=arg_length;
1 by brian
clean slate
228
      }
229
    }
230
  }
2281.6.3 by Olaf van der Spek
Return void
231
  bool is_alloced() { return alloced; } const
1 by brian
clean slate
232
  inline String& operator = (const String &s)
233
  {
234
    if (&s != this)
235
    {
236
      /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
237
        It is forbidden to do assignments like
1 by brian
clean slate
238
        some_string = substring_of_that_string
239
       */
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
240
      assert(!s.uses_buffer_owned_by(this));
1 by brian
clean slate
241
      free();
242
      Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
243
      alloced=0;
244
    }
245
    return *this;
246
  }
247
2281.4.12 by Olaf van der Spek
Return void
248
  void copy();					// Alloc string if not alloced
2281.4.11 by Olaf van der Spek
Merge ~olafvdspek/drizzle/bool
249
  void copy(const String&);			// Allocate new string
250
  void copy(const std::string&, const charset_info_st*);	// Allocate new string
251
  void copy(const char*, size_t, const charset_info_st*); // Allocate new string
2318.7.8 by Olaf van der Spek
Remove unused param
252
  static bool needs_conversion(size_t arg_length, const charset_info_st* cs_from, const charset_info_st* cs_to);
2281.4.16 by Olaf van der Spek
Return void
253
  void set_or_copy_aligned(const char *s, size_t arg_length, const charset_info_st*);
2281.6.2 by Olaf van der Spek
Return void
254
  void copy(const char*s,size_t arg_length, const charset_info_st& csto);
2385.1.4 by Olaf van der Spek
Refactor setTableName
255
  void append(const char*);
256
  void append(const char*, size_t);
2385.1.3 by Olaf van der Spek
Refactor getTableName
257
  void append(str_ref);
2318.7.5 by Olaf van der Spek
Remove unused param
258
  void append_with_prefill(const char *s, size_t arg_length, size_t full_length, char fill_char);
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
259
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
260
  int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
2281.4.13 by Olaf van der Spek
Return void
261
  void replace(size_t offset,size_t arg_length,const char *to,size_t length);
262
  void replace(size_t offset,size_t arg_length,const String &to);
263
264
  inline void append(char chr)
1 by brian
clean slate
265
  {
266
    if (str_length < Alloced_length)
267
    {
268
      Ptr[str_length++]=chr;
269
    }
270
    else
271
    {
2281.4.13 by Olaf van der Spek
Return void
272
      realloc(str_length+1);
1 by brian
clean slate
273
      Ptr[str_length++]=chr;
274
    }
275
  }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
276
  friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
1 by brian
clean slate
277
  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).
278
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
2385.2.6 by Olaf van der Spek
Refactor
279
  size_t numchars() const;
280
  int charpos(int i, size_t offset= 0) const;
1 by brian
clean slate
281
2281.6.3 by Olaf van der Spek
Return void
282
  void reserve(size_t space_needed)
1 by brian
clean slate
283
  {
2281.6.3 by Olaf van der Spek
Return void
284
    realloc(str_length + space_needed);
1 by brian
clean slate
285
  }
2281.6.3 by Olaf van der Spek
Return void
286
  void reserve(size_t space_needed, size_t grow_by);
1 by brian
clean slate
287
2281.4.16 by Olaf van der Spek
Return void
288
  inline void append(const char *s, size_t arg_length, size_t step_alloc)
1 by brian
clean slate
289
  {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
290
    size_t new_length= arg_length + str_length;
2281.4.16 by Olaf van der Spek
Return void
291
    if (new_length > Alloced_length)
292
			realloc(new_length + step_alloc);
1 by brian
clean slate
293
    memcpy(Ptr+str_length, s, arg_length);
294
    str_length+= arg_length;
295
  }
2281.4.16 by Olaf van der Spek
Return void
296
2318.7.8 by Olaf van der Spek
Remove unused param
297
  void print(String&) const;
1 by brian
clean slate
298
299
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
300
  void swap(String &s);
301
302
  inline bool uses_buffer_owned_by(const String *s) const
303
  {
304
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
305
  }
306
};
307
2318.7.6 by Olaf van der Spek
Remove unused declarations
308
bool check_if_only_end_space(const charset_info_st* const, char *str, char *end);
1 by brian
clean slate
309
2318.7.6 by Olaf van der Spek
Remove unused declarations
310
std::ostream& operator<<(std::ostream&, const String&);
2023.2.1 by Brian Aker
Merge in BOOL type.
311
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
312
} /* namespace drizzled */
313
314
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
315
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);