1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#ifndef DRIZZLE_SERVER_SQL_STRING_H
21
#define DRIZZLE_SERVER_SQL_STRING_H
23
/* This file is originally from the mysql distribution. Coded by monty */
25
#ifdef USE_PRAGMA_INTERFACE
26
#pragma interface /* gcc class implementation */
30
#define NOT_FIXED_DEC 31
33
#include <libdrizzle/drizzle_com.h>
36
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
37
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
38
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
39
const char *from, uint32_t from_length,
40
const CHARSET_INFO * const from_cs, uint32_t *errors);
41
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
42
char *to, uint32_t to_length,
43
const CHARSET_INFO * const from_cs,
44
const char *from, uint32_t from_length,
46
const char **well_formed_error_pos,
47
const char **cannot_convert_error_pos,
48
const char **from_end_pos);
49
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
50
char *dst, size_t dstlen,
51
const char *src, size_t srclen);
56
uint32_t str_length,Alloced_length;
58
const CHARSET_INFO *str_charset;
62
Ptr=0; str_length=Alloced_length=0; alloced=0;
63
str_charset= &my_charset_bin;
65
String(uint32_t length_arg)
67
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
68
str_charset= &my_charset_bin;
70
String(const char *str, const CHARSET_INFO * const cs)
72
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
75
String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
77
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
80
String(char *str,uint32_t len, const CHARSET_INFO * const cs)
82
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
85
String(const String &str)
87
Ptr=str.Ptr ; str_length=str.str_length ;
88
Alloced_length=str.Alloced_length; alloced=0;
89
str_charset=str.str_charset;
91
static void *operator new(size_t size, MEM_ROOT *mem_root)
92
{ return (void*) alloc_root(mem_root, (uint) size); }
93
static void operator delete(void *ptr_arg __attribute__((unused)),
94
size_t size __attribute__((unused)))
95
{ TRASH(ptr_arg, size); }
96
static void operator delete(void *ptr_arg __attribute__((unused)),
97
MEM_ROOT *mem_root __attribute__((unused)))
98
{ /* never called */ }
101
inline void set_charset(const CHARSET_INFO * const charset_arg)
102
{ str_charset= charset_arg; }
103
inline const CHARSET_INFO *charset() const { return str_charset; }
104
inline uint32_t length() const { return str_length;}
105
inline uint32_t alloced_length() const { return Alloced_length;}
106
inline char& operator [] (uint32_t i) const { return Ptr[i]; }
107
inline void length(uint32_t len) { str_length=len ; }
108
inline bool is_empty() { return (str_length == 0); }
109
inline void mark_as_const() { Alloced_length= 0;}
110
inline char *ptr() { return Ptr; }
111
inline const char *ptr() const { return Ptr; }
114
if (!Ptr || Ptr[str_length]) /* Should be safe */
115
(void) realloc(str_length);
118
inline char *c_ptr_quick()
120
if (Ptr && str_length < Alloced_length)
124
inline char *c_ptr_safe()
126
if (Ptr && str_length < Alloced_length)
129
(void) realloc(str_length);
133
void set(String &str,uint32_t offset,uint32_t arg_length)
135
assert(&str != this);
137
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
138
if (str.Alloced_length)
139
Alloced_length=str.Alloced_length-offset;
142
str_charset=str.str_charset;
144
inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
147
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
150
inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
153
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
156
bool set_ascii(const char *str, uint32_t arg_length);
157
inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
161
Ptr=(char*) str; str_length=Alloced_length=arg_length;
165
bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
166
bool set(int64_t num, const CHARSET_INFO * const cs)
167
{ return set_int(num, false, cs); }
168
bool set(uint64_t num, const CHARSET_INFO * const cs)
169
{ return set_int((int64_t)num, true, cs); }
170
bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
174
This is a method that works the same as perl's "chop". It simply
175
drops the last character of a string. This is useful in the case
176
of the federated storage handler where I'm building a unknown
177
number, list of values and fields to be used in a sql insert
178
statement to be run on the remote server, and have a comma after each.
179
When the list is complete, I "chop" off the trailing comma
183
stringobj.append("VALUES ('foo', 'fi', 'fo',");
185
stringobj.append(")");
187
In this case, the value of string was:
189
VALUES ('foo', 'fi', 'fo',
190
VALUES ('foo', 'fi', 'fo'
191
VALUES ('foo', 'fi', 'fo')
196
Ptr[str_length--]= '\0';
207
str_length=0; /* Safety */
210
inline bool alloc(uint32_t arg_length)
212
if (arg_length < Alloced_length)
214
return real_alloc(arg_length);
216
bool real_alloc(uint32_t arg_length); // Empties old string
217
bool realloc(uint32_t arg_length);
218
inline void shrink(uint32_t arg_length) // Shrink buffer
220
if (arg_length < Alloced_length)
223
if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
226
real_alloc(arg_length);
231
Alloced_length=arg_length;
235
bool is_alloced() { return alloced; }
236
inline String& operator = (const String &s)
241
It is forbidden to do assignments like
242
some_string = substring_of_that_string
244
assert(!s.uses_buffer_owned_by(this));
246
Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
252
bool copy(); // Alloc string if not alloced
253
bool copy(const String &s); // Allocate new string
254
bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs); // Allocate new string
255
static bool needs_conversion(uint32_t arg_length,
256
const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
258
bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
259
const CHARSET_INFO * const cs);
260
bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
261
bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
262
const CHARSET_INFO * const csto, uint32_t *errors);
263
bool append(const String &s);
264
bool append(const char *s);
265
bool append(const char *s,uint32_t arg_length);
266
bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
267
bool append(IO_CACHE* file, uint32_t arg_length);
268
bool append_with_prefill(const char *s, uint32_t arg_length,
269
uint32_t full_length, char fill_char);
270
int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
271
int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
272
bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
273
bool replace(uint32_t offset,uint32_t arg_length,const String &to);
274
inline bool append(char chr)
276
if (str_length < Alloced_length)
278
Ptr[str_length++]=chr;
282
if (realloc(str_length+1))
284
Ptr[str_length++]=chr;
288
bool fill(uint32_t max_length,char fill);
289
friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
290
friend int stringcmp(const String *a,const String *b);
291
friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
293
int charpos(int i,uint32_t offset=0);
295
int reserve(uint32_t space_needed)
297
return realloc(str_length + space_needed);
299
int reserve(uint32_t space_needed, uint32_t grow_by);
302
The following append operations do NOT check alloced memory
303
q_*** methods writes values of parameters itself
304
qs_*** methods writes string representation of value
306
void q_append(const char c)
308
Ptr[str_length++] = c;
310
void q_append(const uint32_t n)
312
int4store(Ptr + str_length, n);
315
void q_append(double d)
317
float8store(Ptr + str_length, d);
320
void q_append(double *d)
322
float8store(Ptr + str_length, *d);
325
void q_append(const char *data, uint32_t data_len)
327
memcpy(Ptr + str_length, data, data_len);
328
str_length += data_len;
331
void write_at_position(int position, uint32_t value)
333
int4store(Ptr + position,value);
336
void qs_append(const char *str, uint32_t len);
337
void qs_append(double d);
338
void qs_append(double *d);
339
inline void qs_append(const char c)
344
void qs_append(int i);
345
void qs_append(uint32_t i);
347
/* Inline (general) functions used by the protocol functions */
349
inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
351
uint32_t new_length= arg_length + str_length;
352
if (new_length > Alloced_length)
354
if (realloc(new_length + step_alloc))
357
uint32_t old_length= str_length;
358
str_length+= arg_length;
359
return Ptr+ old_length; /* Area to use */
362
inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
364
uint32_t new_length= arg_length + str_length;
365
if (new_length > Alloced_length && realloc(new_length + step_alloc))
367
memcpy(Ptr+str_length, s, arg_length);
368
str_length+= arg_length;
371
void print(String *print);
373
/* Swap two string objects. Efficient way to exchange data without memcpy. */
374
void swap(String &s);
376
inline bool uses_buffer_owned_by(const String *s) const
378
return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
382
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
385
return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
389
bool operator==(const String &s1, const String &s2)
391
return stringcmp(&s1,&s2) == 0;
395
bool operator!=(const String &s1, const String &s2)
400
#endif /* DRIZZLE_SERVER_SQL_STRING_H */