~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);
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
66
  String(const char *str, const charset_info_st * const cs);
67
  String(const char *str, size_t len, const charset_info_st * const cs);
68
  String(char *str, size_t len, const charset_info_st * const cs);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
69
  String(const String &str);
70
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
71
  static void *operator new(size_t size, memory::Root *mem_root);
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
72
  static void operator delete(void *, size_t)
73
  { }
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
74
  static void operator delete(void *, memory::Root *)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
75
  { }
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
76
  ~String();
1 by brian
clean slate
77
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
78
  inline void set_charset(const charset_info_st * const charset_arg)
1 by brian
clean slate
79
  { str_charset= charset_arg; }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
80
  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).
81
  inline size_t length() const { return str_length;}
82
  inline size_t alloced_length() const { return Alloced_length;}
83
  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()
84
  inline void length(size_t len) { str_length=len; }
85
  inline bool empty() const { return str_length == 0; }
86
  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().
87
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
88
  inline const char *ptr() const { return Ptr; }
89
  inline char *c_ptr()
90
  {
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
91
    if (str_length == Alloced_length)
2318.7.11 by Olaf van der Spek
Use String::c_str() instead of c_ptr_safe()
92
      realloc(str_length);
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
93
    else
94
      Ptr[str_length]= 0;
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
95
1 by brian
clean slate
96
    return Ptr;
97
  }
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
98
  inline const char* begin() const
99
  {
100
    return Ptr;
101
  }
102
  inline const char* end() const
103
  {
104
    return begin() + size();
105
  }
106
  inline const char* data() const
107
  {
108
    return Ptr;
109
  }
110
  inline size_t size() const
111
  {
112
    return length();
113
  }
114
  inline const char* c_str()
2318.7.11 by Olaf van der Spek
Use String::c_str() instead of c_ptr_safe()
115
  {
116
    if (Ptr && str_length < Alloced_length)
117
      Ptr[str_length]=0;
118
    else
119
      realloc(str_length);
1273.13.38 by Brian Aker
Add in new show work.
120
    return Ptr;
121
  }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
122
  void append_identifier(const char *name, size_t length);
2318.8.1 by Olaf van der Spek
Add data_ref.h
123
  void append_identifier(str_ref);
1 by brian
clean slate
124
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
125
  void set(String &str,size_t offset,size_t arg_length)
1 by brian
clean slate
126
  {
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
127
    assert(&str != this);
1 by brian
clean slate
128
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
129
    Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
1 by brian
clean slate
130
    if (str.Alloced_length)
131
      Alloced_length=str.Alloced_length-offset;
132
    else
133
      Alloced_length=0;
134
    str_charset=str.str_charset;
135
  }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
136
  inline void set(char *str,size_t arg_length, const charset_info_st * const cs)
1 by brian
clean slate
137
  {
138
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
139
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
1 by brian
clean slate
140
    str_charset=cs;
141
  }
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
142
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
143
  inline void set(const char *str,size_t arg_length, const charset_info_st * const cs)
1 by brian
clean slate
144
  {
145
    free();
1241.3.4 by Trond Norbye
Cleanup: use C++ style casting, init-list and virtual destructors
146
    Ptr= const_cast<char*>(str);
147
    str_length=arg_length; Alloced_length=0 ; alloced=0;
1 by brian
clean slate
148
    str_charset=cs;
149
  }
2281.6.1 by Olaf van der Spek
Return void
150
  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.
151
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
152
  inline void set_quick(char *str,size_t arg_length, const charset_info_st * 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
  }
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
160
2297.1.5 by Olaf van der Spek
Return void
161
  void set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs);
162
  void set(int64_t num, const charset_info_st * const cs)
163
  { set_int(num, false, cs); }
164
  void set(uint64_t num, const charset_info_st * const cs)
165
  { set_int(static_cast<int64_t>(num), true, cs); }
2281.6.1 by Olaf van der Spek
Return void
166
  void set_real(double num,size_t decimals, const charset_info_st* cs);
1 by brian
clean slate
167
168
  /*
169
    PMG 2004.11.12
170
    This is a method that works the same as perl's "chop". It simply
171
    drops the last character of a string. This is useful in the case
172
    of the federated storage handler where I'm building a unknown
173
    number, list of values and fields to be used in a sql insert
174
    statement to be run on the remote server, and have a comma after each.
175
    When the list is complete, I "chop" off the trailing comma
176
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
177
    ex.
178
      String stringobj;
1 by brian
clean slate
179
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
180
      stringobj.chop();
181
      stringobj.append(")");
182
183
    In this case, the value of string was:
184
185
    VALUES ('foo', 'fi', 'fo',
186
    VALUES ('foo', 'fi', 'fo'
187
    VALUES ('foo', 'fi', 'fo')
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
188
1 by brian
clean slate
189
  */
190
  inline void chop()
191
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
192
    Ptr[str_length--]= '\0';
1 by brian
clean slate
193
  }
194
195
  inline void free()
196
  {
197
    if (alloced)
198
    {
199
      alloced=0;
200
      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.
201
      ::free(Ptr);
1 by brian
clean slate
202
      Ptr=0;
203
      str_length=0;				/* Safety */
204
    }
205
  }
2275.2.12 by Olaf van der Spek
Return void
206
  inline void alloc(size_t arg_length)
1 by brian
clean slate
207
  {
2275.2.12 by Olaf van der Spek
Return void
208
    if (arg_length >= Alloced_length)
209
      real_alloc(arg_length);
1 by brian
clean slate
210
  }
2275.2.12 by Olaf van der Spek
Return void
211
  void real_alloc(size_t arg_length);			// Empties old string
2281.6.3 by Olaf van der Spek
Return void
212
  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).
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
  }
2281.6.3 by Olaf van der Spek
Return void
230
  bool is_alloced() { return alloced; } const
1 by brian
clean slate
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
2281.4.12 by Olaf van der Spek
Return void
247
  void copy();					// Alloc string if not alloced
2281.4.11 by Olaf van der Spek
Merge ~olafvdspek/drizzle/bool
248
  void copy(const String&);			// Allocate new string
249
  void copy(const std::string&, const charset_info_st*);	// Allocate new string
250
  void copy(const char*, size_t, const charset_info_st*); // Allocate new string
2318.7.8 by Olaf van der Spek
Remove unused param
251
  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
252
  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
253
  void copy(const char*s,size_t arg_length, const charset_info_st& csto);
2385.1.4 by Olaf van der Spek
Refactor setTableName
254
  void append(const char*);
255
  void append(const char*, size_t);
2385.1.3 by Olaf van der Spek
Refactor getTableName
256
  void append(str_ref);
2318.7.5 by Olaf van der Spek
Remove unused param
257
  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).
258
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
259
  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
260
  void replace(size_t offset,size_t arg_length,const char *to,size_t length);
261
  void replace(size_t offset,size_t arg_length,const String &to);
262
263
  inline void append(char chr)
1 by brian
clean slate
264
  {
265
    if (str_length < Alloced_length)
266
    {
267
      Ptr[str_length++]=chr;
268
    }
269
    else
270
    {
2281.4.13 by Olaf van der Spek
Return void
271
      realloc(str_length+1);
1 by brian
clean slate
272
      Ptr[str_length++]=chr;
273
    }
274
  }
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
275
  friend int sortcmp(const String *a,const String *b, const charset_info_st * const cs);
1 by brian
clean slate
276
  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).
277
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
2385.2.6 by Olaf van der Spek
Refactor
278
  size_t numchars() const;
279
  int charpos(int i, size_t offset= 0) const;
1 by brian
clean slate
280
2281.6.3 by Olaf van der Spek
Return void
281
  void reserve(size_t space_needed)
1 by brian
clean slate
282
  {
2281.6.3 by Olaf van der Spek
Return void
283
    realloc(str_length + space_needed);
1 by brian
clean slate
284
  }
2281.6.3 by Olaf van der Spek
Return void
285
  void reserve(size_t space_needed, size_t grow_by);
1 by brian
clean slate
286
2281.4.16 by Olaf van der Spek
Return void
287
  inline void append(const char *s, size_t arg_length, size_t step_alloc)
1 by brian
clean slate
288
  {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
289
    size_t new_length= arg_length + str_length;
2281.4.16 by Olaf van der Spek
Return void
290
    if (new_length > Alloced_length)
291
			realloc(new_length + step_alloc);
1 by brian
clean slate
292
    memcpy(Ptr+str_length, s, arg_length);
293
    str_length+= arg_length;
294
  }
2281.4.16 by Olaf van der Spek
Return void
295
2318.7.8 by Olaf van der Spek
Remove unused param
296
  void print(String&) const;
1 by brian
clean slate
297
298
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
299
  void swap(String &s);
300
301
  inline bool uses_buffer_owned_by(const String *s) const
302
  {
303
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
304
  }
305
};
306
2318.7.6 by Olaf van der Spek
Remove unused declarations
307
bool check_if_only_end_space(const charset_info_st* const, char *str, char *end);
1 by brian
clean slate
308
2318.7.6 by Olaf van der Spek
Remove unused declarations
309
std::ostream& operator<<(std::ostream&, const String&);
2023.2.1 by Brian Aker
Merge in BOOL type.
310
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
311
} /* namespace drizzled */
312
313
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
314
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);