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