1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
17
Code for handling strings with can grow dynamicly.
18
Copyright Monty Program KB.
22
#include "mysys_priv.h"
25
my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
26
size_t init_alloc, size_t alloc_increment)
29
DBUG_ENTER("init_dynamic_string");
34
if (init_str && (length= strlen(init_str)+1) < init_alloc)
35
init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment;
37
init_alloc=alloc_increment;
39
if (!(str->str=(char*) my_malloc(init_alloc,MYF(MY_WME))))
43
memcpy(str->str,init_str,length);
44
str->max_length=init_alloc;
45
str->alloc_increment=alloc_increment;
50
my_bool dynstr_set(DYNAMIC_STRING *str, const char *init_str)
53
DBUG_ENTER("dynstr_set");
55
if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length)
57
str->max_length=((length+str->alloc_increment-1)/str->alloc_increment)*
60
str->max_length=str->alloc_increment;
61
if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME))))
67
memcpy(str->str,init_str,length);
75
my_bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size)
77
DBUG_ENTER("dynstr_realloc");
79
if (!additional_size) DBUG_RETURN(FALSE);
80
if (str->length + additional_size > str->max_length)
82
str->max_length=((str->length + additional_size+str->alloc_increment-1)/
83
str->alloc_increment)*str->alloc_increment;
84
if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME))))
91
my_bool dynstr_append(DYNAMIC_STRING *str, const char *append)
93
return dynstr_append_mem(str,append,(uint) strlen(append));
97
my_bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,
101
if (str->length+length >= str->max_length)
103
uint new_length=(str->length+length+str->alloc_increment)/
104
str->alloc_increment;
105
new_length*=str->alloc_increment;
106
if (!(new_ptr=(char*) my_realloc(str->str,new_length,MYF(MY_WME))))
109
str->max_length=new_length;
111
memcpy(str->str + str->length,append,length);
113
str->str[str->length]=0; /* Safety for C programs */
118
my_bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
121
str->str[str->length]= '\0';
126
Concatenates any number of strings, escapes any OS quote in the result then
127
surround the whole affair in another set of quotes which is finally appended
128
to specified DYNAMIC_STRING. This function is especially useful when
129
building strings to be executed with the system() function.
131
@param str Dynamic String which will have addtional strings appended.
132
@param append String to be appended.
133
@param ... Optional. Additional string(s) to be appended.
135
@note The final argument in the list must be NullS even if no additional
138
@return True = Success.
141
my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
143
const char *quote_str= "\'";
144
const uint quote_len= 1;
148
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
149
va_start(dirty_text, append);
150
while (append != NullS)
152
const char *cur_pos= append;
153
const char *next_pos= cur_pos;
155
/* Search for quote in each string and replace with escaped quote */
156
while(*(next_pos= strcend(cur_pos, quote_str[0])) != '\0')
158
ret&= dynstr_append_mem(str, cur_pos, next_pos - cur_pos);
159
ret&= dynstr_append_mem(str ,"\\", 1);
160
ret&= dynstr_append_mem(str, quote_str, quote_len);
161
cur_pos= next_pos + 1;
163
ret&= dynstr_append_mem(str, cur_pos, next_pos - cur_pos);
164
append= va_arg(dirty_text, char *);
167
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */
173
void dynstr_free(DYNAMIC_STRING *str)
177
my_free(str->str,MYF(MY_WME));