1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* This file is originally from the mysql distribution. Coded by monty */
|
|
17 |
||
18 |
#ifdef USE_PRAGMA_INTERFACE
|
|
19 |
#pragma interface /* gcc class implementation */ |
|
20 |
#endif
|
|
21 |
||
22 |
#ifndef NOT_FIXED_DEC
|
|
23 |
#define NOT_FIXED_DEC 31
|
|
24 |
#endif
|
|
25 |
||
26 |
class String; |
|
27 |
int sortcmp(const String *a,const String *b, CHARSET_INFO *cs); |
|
28 |
String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); |
|
29 |
uint32 copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs, |
|
30 |
const char *from, uint32 from_length, |
|
31 |
CHARSET_INFO *from_cs, uint *errors); |
|
32 |
||
33 |
class String |
|
34 |
{
|
|
35 |
char *Ptr; |
|
36 |
uint32 str_length,Alloced_length; |
|
37 |
bool alloced; |
|
38 |
CHARSET_INFO *str_charset; |
|
39 |
public: |
|
40 |
String() |
|
41 |
{
|
|
42 |
Ptr=0; str_length=Alloced_length=0; alloced=0; |
|
43 |
str_charset= &my_charset_bin; |
|
44 |
}
|
|
45 |
String(uint32 length_arg) |
|
46 |
{
|
|
47 |
alloced=0; Alloced_length=0; (void) real_alloc(length_arg); |
|
48 |
str_charset= &my_charset_bin; |
|
49 |
}
|
|
50 |
String(const char *str, CHARSET_INFO *cs) |
|
51 |
{
|
|
52 |
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0; |
|
53 |
str_charset=cs; |
|
54 |
}
|
|
55 |
String(const char *str,uint32 len, CHARSET_INFO *cs) |
|
56 |
{
|
|
57 |
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0; |
|
58 |
str_charset=cs; |
|
59 |
}
|
|
60 |
String(char *str,uint32 len, CHARSET_INFO *cs) |
|
61 |
{
|
|
62 |
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0; |
|
63 |
str_charset=cs; |
|
64 |
}
|
|
65 |
String(const String &str) |
|
66 |
{
|
|
67 |
Ptr=str.Ptr ; str_length=str.str_length ; |
|
68 |
Alloced_length=str.Alloced_length; alloced=0; |
|
69 |
str_charset=str.str_charset; |
|
70 |
}
|
|
71 |
static void *operator new(size_t size, MEM_ROOT *mem_root) |
|
72 |
{ return (void*) alloc_root(mem_root, (uint) size); } |
|
73 |
static void operator delete(void *ptr_arg,size_t size) |
|
74 |
{ TRASH(ptr_arg, size); } |
|
75 |
static void operator delete(void *ptr_arg, MEM_ROOT *mem_root) |
|
76 |
{ /* never called */ } |
|
77 |
~String() { free(); } |
|
78 |
||
79 |
inline void set_charset(CHARSET_INFO *charset_arg) |
|
80 |
{ str_charset= charset_arg; } |
|
81 |
inline CHARSET_INFO *charset() const { return str_charset; } |
|
82 |
inline uint32 length() const { return str_length;} |
|
83 |
inline uint32 alloced_length() const { return Alloced_length;} |
|
84 |
inline char& operator [] (uint32 i) const { return Ptr[i]; } |
|
85 |
inline void length(uint32 len) { str_length=len ; } |
|
86 |
inline bool is_empty() { return (str_length == 0); } |
|
87 |
inline void mark_as_const() { Alloced_length= 0;} |
|
88 |
inline const char *ptr() const { return Ptr; } |
|
89 |
inline char *c_ptr() |
|
90 |
{
|
|
91 |
if (!Ptr || Ptr[str_length]) /* Should be safe */ |
|
92 |
(void) realloc(str_length); |
|
93 |
return Ptr; |
|
94 |
}
|
|
95 |
inline char *c_ptr_quick() |
|
96 |
{
|
|
97 |
if (Ptr && str_length < Alloced_length) |
|
98 |
Ptr[str_length]=0; |
|
99 |
return Ptr; |
|
100 |
}
|
|
101 |
inline char *c_ptr_safe() |
|
102 |
{
|
|
103 |
if (Ptr && str_length < Alloced_length) |
|
104 |
Ptr[str_length]=0; |
|
105 |
else
|
|
106 |
(void) realloc(str_length); |
|
107 |
return Ptr; |
|
108 |
}
|
|
109 |
||
110 |
void set(String &str,uint32 offset,uint32 arg_length) |
|
111 |
{
|
|
112 |
DBUG_ASSERT(&str != this); |
|
113 |
free(); |
|
114 |
Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0; |
|
115 |
if (str.Alloced_length) |
|
116 |
Alloced_length=str.Alloced_length-offset; |
|
117 |
else
|
|
118 |
Alloced_length=0; |
|
119 |
str_charset=str.str_charset; |
|
120 |
}
|
|
121 |
inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs) |
|
122 |
{
|
|
123 |
free(); |
|
124 |
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0; |
|
125 |
str_charset=cs; |
|
126 |
}
|
|
127 |
inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs) |
|
128 |
{
|
|
129 |
free(); |
|
130 |
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0; |
|
131 |
str_charset=cs; |
|
132 |
}
|
|
133 |
bool set_ascii(const char *str, uint32 arg_length); |
|
134 |
inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs) |
|
135 |
{
|
|
136 |
if (!alloced) |
|
137 |
{
|
|
138 |
Ptr=(char*) str; str_length=Alloced_length=arg_length; |
|
139 |
}
|
|
140 |
str_charset=cs; |
|
141 |
}
|
|
142 |
bool set(longlong num, CHARSET_INFO *cs); |
|
143 |
bool set(ulonglong num, CHARSET_INFO *cs); |
|
144 |
bool set(double num,uint decimals, CHARSET_INFO *cs); |
|
145 |
||
146 |
/*
|
|
147 |
PMG 2004.11.12
|
|
148 |
This is a method that works the same as perl's "chop". It simply
|
|
149 |
drops the last character of a string. This is useful in the case
|
|
150 |
of the federated storage handler where I'm building a unknown
|
|
151 |
number, list of values and fields to be used in a sql insert
|
|
152 |
statement to be run on the remote server, and have a comma after each.
|
|
153 |
When the list is complete, I "chop" off the trailing comma
|
|
154 |
||
155 |
ex.
|
|
156 |
String stringobj;
|
|
157 |
stringobj.append("VALUES ('foo', 'fi', 'fo',");
|
|
158 |
stringobj.chop();
|
|
159 |
stringobj.append(")");
|
|
160 |
||
161 |
In this case, the value of string was:
|
|
162 |
||
163 |
VALUES ('foo', 'fi', 'fo',
|
|
164 |
VALUES ('foo', 'fi', 'fo'
|
|
165 |
VALUES ('foo', 'fi', 'fo')
|
|
166 |
|
|
167 |
*/
|
|
168 |
inline void chop() |
|
169 |
{
|
|
170 |
Ptr[str_length--]= '\0'; |
|
171 |
}
|
|
172 |
||
173 |
inline void free() |
|
174 |
{
|
|
175 |
if (alloced) |
|
176 |
{
|
|
177 |
alloced=0; |
|
178 |
Alloced_length=0; |
|
179 |
my_free(Ptr,MYF(0)); |
|
180 |
Ptr=0; |
|
181 |
str_length=0; /* Safety */ |
|
182 |
}
|
|
183 |
}
|
|
184 |
inline bool alloc(uint32 arg_length) |
|
185 |
{
|
|
186 |
if (arg_length < Alloced_length) |
|
187 |
return 0; |
|
188 |
return real_alloc(arg_length); |
|
189 |
}
|
|
190 |
bool real_alloc(uint32 arg_length); // Empties old string |
|
191 |
bool realloc(uint32 arg_length); |
|
192 |
inline void shrink(uint32 arg_length) // Shrink buffer |
|
193 |
{
|
|
194 |
if (arg_length < Alloced_length) |
|
195 |
{
|
|
196 |
char *new_ptr; |
|
197 |
if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0)))) |
|
198 |
{
|
|
199 |
Alloced_length = 0; |
|
200 |
real_alloc(arg_length); |
|
201 |
}
|
|
202 |
else
|
|
203 |
{
|
|
204 |
Ptr=new_ptr; |
|
205 |
Alloced_length=arg_length; |
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
bool is_alloced() { return alloced; } |
|
210 |
inline String& operator = (const String &s) |
|
211 |
{
|
|
212 |
if (&s != this) |
|
213 |
{
|
|
214 |
/*
|
|
215 |
It is forbidden to do assignments like
|
|
216 |
some_string = substring_of_that_string
|
|
217 |
*/
|
|
218 |
DBUG_ASSERT(!s.uses_buffer_owned_by(this)); |
|
219 |
free(); |
|
220 |
Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length; |
|
221 |
alloced=0; |
|
222 |
}
|
|
223 |
return *this; |
|
224 |
}
|
|
225 |
||
226 |
bool copy(); // Alloc string if not alloced |
|
227 |
bool copy(const String &s); // Allocate new string |
|
228 |
bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string |
|
229 |
static bool needs_conversion(uint32 arg_length, |
|
230 |
CHARSET_INFO *cs_from, CHARSET_INFO *cs_to, |
|
231 |
uint32 *offset); |
|
232 |
bool copy_aligned(const char *s, uint32 arg_length, uint32 offset, |
|
233 |
CHARSET_INFO *cs); |
|
234 |
bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs); |
|
235 |
bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom, |
|
236 |
CHARSET_INFO *csto, uint *errors); |
|
237 |
bool append(const String &s); |
|
238 |
bool append(const char *s); |
|
239 |
bool append(const char *s,uint32 arg_length); |
|
240 |
bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs); |
|
241 |
bool append(IO_CACHE* file, uint32 arg_length); |
|
242 |
bool append_with_prefill(const char *s, uint32 arg_length, |
|
243 |
uint32 full_length, char fill_char); |
|
244 |
int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 |
|
245 |
int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 |
|
246 |
bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length); |
|
247 |
bool replace(uint32 offset,uint32 arg_length,const String &to); |
|
248 |
inline bool append(char chr) |
|
249 |
{
|
|
250 |
if (str_length < Alloced_length) |
|
251 |
{
|
|
252 |
Ptr[str_length++]=chr; |
|
253 |
}
|
|
254 |
else
|
|
255 |
{
|
|
256 |
if (realloc(str_length+1)) |
|
257 |
return 1; |
|
258 |
Ptr[str_length++]=chr; |
|
259 |
}
|
|
260 |
return 0; |
|
261 |
}
|
|
262 |
bool fill(uint32 max_length,char fill); |
|
263 |
void strip_sp(); |
|
264 |
friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs); |
|
265 |
friend int stringcmp(const String *a,const String *b); |
|
266 |
friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); |
|
267 |
uint32 numchars(); |
|
268 |
int charpos(int i,uint32 offset=0); |
|
269 |
||
270 |
int reserve(uint32 space_needed) |
|
271 |
{
|
|
272 |
return realloc(str_length + space_needed); |
|
273 |
}
|
|
274 |
int reserve(uint32 space_needed, uint32 grow_by); |
|
275 |
||
276 |
/*
|
|
277 |
The following append operations do NOT check alloced memory
|
|
278 |
q_*** methods writes values of parameters itself
|
|
279 |
qs_*** methods writes string representation of value
|
|
280 |
*/
|
|
281 |
void q_append(const char c) |
|
282 |
{
|
|
283 |
Ptr[str_length++] = c; |
|
284 |
}
|
|
285 |
void q_append(const uint32 n) |
|
286 |
{
|
|
287 |
int4store(Ptr + str_length, n); |
|
288 |
str_length += 4; |
|
289 |
}
|
|
290 |
void q_append(double d) |
|
291 |
{
|
|
292 |
float8store(Ptr + str_length, d); |
|
293 |
str_length += 8; |
|
294 |
}
|
|
295 |
void q_append(double *d) |
|
296 |
{
|
|
297 |
float8store(Ptr + str_length, *d); |
|
298 |
str_length += 8; |
|
299 |
}
|
|
300 |
void q_append(const char *data, uint32 data_len) |
|
301 |
{
|
|
302 |
memcpy(Ptr + str_length, data, data_len); |
|
303 |
str_length += data_len; |
|
304 |
}
|
|
305 |
||
306 |
void write_at_position(int position, uint32 value) |
|
307 |
{
|
|
308 |
int4store(Ptr + position,value); |
|
309 |
}
|
|
310 |
||
311 |
void qs_append(const char *str, uint32 len); |
|
312 |
void qs_append(double d); |
|
313 |
void qs_append(double *d); |
|
314 |
inline void qs_append(const char c) |
|
315 |
{
|
|
316 |
Ptr[str_length]= c; |
|
317 |
str_length++; |
|
318 |
}
|
|
319 |
void qs_append(int i); |
|
320 |
void qs_append(uint i); |
|
321 |
||
322 |
/* Inline (general) functions used by the protocol functions */
|
|
323 |
||
324 |
inline char *prep_append(uint32 arg_length, uint32 step_alloc) |
|
325 |
{
|
|
326 |
uint32 new_length= arg_length + str_length; |
|
327 |
if (new_length > Alloced_length) |
|
328 |
{
|
|
329 |
if (realloc(new_length + step_alloc)) |
|
330 |
return 0; |
|
331 |
}
|
|
332 |
uint32 old_length= str_length; |
|
333 |
str_length+= arg_length; |
|
334 |
return Ptr+ old_length; /* Area to use */ |
|
335 |
}
|
|
336 |
||
337 |
inline bool append(const char *s, uint32 arg_length, uint32 step_alloc) |
|
338 |
{
|
|
339 |
uint32 new_length= arg_length + str_length; |
|
340 |
if (new_length > Alloced_length && realloc(new_length + step_alloc)) |
|
341 |
return TRUE; |
|
342 |
memcpy(Ptr+str_length, s, arg_length); |
|
343 |
str_length+= arg_length; |
|
344 |
return FALSE; |
|
345 |
}
|
|
346 |
void print(String *print); |
|
347 |
||
348 |
/* Swap two string objects. Efficient way to exchange data without memcpy. */
|
|
349 |
void swap(String &s); |
|
350 |
||
351 |
inline bool uses_buffer_owned_by(const String *s) const |
|
352 |
{
|
|
353 |
return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length); |
|
354 |
}
|
|
355 |
};
|