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 |
/*
|
|
17 |
Code for handling strings with can grow dynamicly.
|
|
18 |
Copyright Monty Program KB.
|
|
19 |
By monty.
|
|
20 |
*/
|
|
21 |
||
22 |
#include "mysys_priv.h" |
|
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
23 |
#include <mystrings/m_string.h> |
1
by brian
clean slate |
24 |
|
146
by Brian Aker
my_bool cleanup. |
25 |
bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str, |
1
by brian
clean slate |
26 |
size_t init_alloc, size_t alloc_increment) |
27 |
{
|
|
28 |
uint length; |
|
29 |
||
30 |
if (!alloc_increment) |
|
31 |
alloc_increment=128; |
|
32 |
length=1; |
|
33 |
if (init_str && (length= strlen(init_str)+1) < init_alloc) |
|
34 |
init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment; |
|
35 |
if (!init_alloc) |
|
36 |
init_alloc=alloc_increment; |
|
37 |
||
38 |
if (!(str->str=(char*) my_malloc(init_alloc,MYF(MY_WME)))) |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
39 |
return(true); |
1
by brian
clean slate |
40 |
str->length=length-1; |
41 |
if (init_str) |
|
42 |
memcpy(str->str,init_str,length); |
|
43 |
str->max_length=init_alloc; |
|
44 |
str->alloc_increment=alloc_increment; |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
45 |
return(false); |
1
by brian
clean slate |
46 |
}
|
47 |
||
48 |
||
146
by Brian Aker
my_bool cleanup. |
49 |
bool dynstr_set(DYNAMIC_STRING *str, const char *init_str) |
1
by brian
clean slate |
50 |
{
|
51 |
uint length=0; |
|
52 |
||
53 |
if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length) |
|
54 |
{
|
|
55 |
str->max_length=((length+str->alloc_increment-1)/str->alloc_increment)* |
|
56 |
str->alloc_increment; |
|
57 |
if (!str->max_length) |
|
58 |
str->max_length=str->alloc_increment; |
|
59 |
if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME)))) |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
60 |
return(true); |
1
by brian
clean slate |
61 |
}
|
62 |
if (init_str) |
|
63 |
{
|
|
64 |
str->length=length-1; |
|
65 |
memcpy(str->str,init_str,length); |
|
66 |
}
|
|
67 |
else
|
|
68 |
str->length=0; |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
69 |
return(false); |
1
by brian
clean slate |
70 |
}
|
71 |
||
72 |
||
146
by Brian Aker
my_bool cleanup. |
73 |
bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size) |
1
by brian
clean slate |
74 |
{
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
75 |
if (!additional_size) return(false); |
1
by brian
clean slate |
76 |
if (str->length + additional_size > str->max_length) |
77 |
{
|
|
78 |
str->max_length=((str->length + additional_size+str->alloc_increment-1)/ |
|
79 |
str->alloc_increment)*str->alloc_increment; |
|
80 |
if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME)))) |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
81 |
return(true); |
1
by brian
clean slate |
82 |
}
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
83 |
return(false); |
1
by brian
clean slate |
84 |
}
|
85 |
||
86 |
||
146
by Brian Aker
my_bool cleanup. |
87 |
bool dynstr_append(DYNAMIC_STRING *str, const char *append) |
1
by brian
clean slate |
88 |
{
|
89 |
return dynstr_append_mem(str,append,(uint) strlen(append)); |
|
90 |
}
|
|
91 |
||
92 |
||
146
by Brian Aker
my_bool cleanup. |
93 |
bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append, |
1
by brian
clean slate |
94 |
size_t length) |
95 |
{
|
|
96 |
char *new_ptr; |
|
97 |
if (str->length+length >= str->max_length) |
|
98 |
{
|
|
99 |
uint new_length=(str->length+length+str->alloc_increment)/ |
|
100 |
str->alloc_increment; |
|
101 |
new_length*=str->alloc_increment; |
|
102 |
if (!(new_ptr=(char*) my_realloc(str->str,new_length,MYF(MY_WME)))) |
|
163
by Brian Aker
Merge Monty's code. |
103 |
return true; |
1
by brian
clean slate |
104 |
str->str=new_ptr; |
105 |
str->max_length=new_length; |
|
106 |
}
|
|
107 |
memcpy(str->str + str->length,append,length); |
|
108 |
str->length+=length; |
|
109 |
str->str[str->length]=0; /* Safety for C programs */ |
|
163
by Brian Aker
Merge Monty's code. |
110 |
return false; |
1
by brian
clean slate |
111 |
}
|
112 |
||
113 |
||
146
by Brian Aker
my_bool cleanup. |
114 |
bool dynstr_trunc(DYNAMIC_STRING *str, size_t n) |
1
by brian
clean slate |
115 |
{
|
116 |
str->length-=n; |
|
117 |
str->str[str->length]= '\0'; |
|
163
by Brian Aker
Merge Monty's code. |
118 |
return false; |
1
by brian
clean slate |
119 |
}
|
120 |
||
121 |
/*
|
|
122 |
Concatenates any number of strings, escapes any OS quote in the result then
|
|
123 |
surround the whole affair in another set of quotes which is finally appended
|
|
124 |
to specified DYNAMIC_STRING. This function is especially useful when
|
|
125 |
building strings to be executed with the system() function.
|
|
126 |
||
127 |
@param str Dynamic String which will have addtional strings appended.
|
|
128 |
@param append String to be appended.
|
|
129 |
@param ... Optional. Additional string(s) to be appended.
|
|
130 |
||
131 |
@note The final argument in the list must be NullS even if no additional
|
|
132 |
options are passed.
|
|
133 |
||
134 |
@return True = Success.
|
|
135 |
*/
|
|
136 |
||
146
by Brian Aker
my_bool cleanup. |
137 |
bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) |
1
by brian
clean slate |
138 |
{
|
139 |
const char *quote_str= "\'"; |
|
140 |
const uint quote_len= 1; |
|
163
by Brian Aker
Merge Monty's code. |
141 |
bool ret= true; |
1
by brian
clean slate |
142 |
va_list dirty_text; |
143 |
||
144 |
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */ |
|
145 |
va_start(dirty_text, append); |
|
146 |
while (append != NullS) |
|
147 |
{
|
|
148 |
const char *cur_pos= append; |
|
149 |
const char *next_pos= cur_pos; |
|
150 |
||
151 |
/* Search for quote in each string and replace with escaped quote */
|
|
152 |
while(*(next_pos= strcend(cur_pos, quote_str[0])) != '\0') |
|
153 |
{
|
|
154 |
ret&= dynstr_append_mem(str, cur_pos, next_pos - cur_pos); |
|
155 |
ret&= dynstr_append_mem(str ,"\\", 1); |
|
156 |
ret&= dynstr_append_mem(str, quote_str, quote_len); |
|
157 |
cur_pos= next_pos + 1; |
|
158 |
}
|
|
159 |
ret&= dynstr_append_mem(str, cur_pos, next_pos - cur_pos); |
|
160 |
append= va_arg(dirty_text, char *); |
|
161 |
}
|
|
162 |
va_end(dirty_text); |
|
163 |
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */ |
|
164 |
||
165 |
return ret; |
|
166 |
}
|
|
167 |
||
168 |
||
169 |
void dynstr_free(DYNAMIC_STRING *str) |
|
170 |
{
|
|
171 |
if (str->str) |
|
172 |
{
|
|
173 |
my_free(str->str,MYF(MY_WME)); |
|
174 |
str->str=0; |
|
175 |
}
|
|
176 |
}
|