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"
23
#include <mystrings/m_string.h>
25
bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
26
size_t init_alloc, size_t alloc_increment)
33
if (init_str && (length= strlen(init_str)+1) < init_alloc)
34
init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment;
36
init_alloc=alloc_increment;
38
if (!(str->str=(char*) my_malloc(init_alloc,MYF(MY_WME))))
42
memcpy(str->str,init_str,length);
43
str->max_length=init_alloc;
44
str->alloc_increment=alloc_increment;
49
bool dynstr_set(DYNAMIC_STRING *str, const char *init_str)
53
if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length)
55
str->max_length=((length+str->alloc_increment-1)/str->alloc_increment)*
58
str->max_length=str->alloc_increment;
59
if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME))))
65
memcpy(str->str,init_str,length);
73
bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size)
75
if (!additional_size) return(false);
76
if (str->length + additional_size > str->max_length)
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))))
87
bool dynstr_append(DYNAMIC_STRING *str, const char *append)
89
return dynstr_append_mem(str,append,(uint) strlen(append));
93
bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,
97
if (str->length+length >= str->max_length)
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))))
105
str->max_length=new_length;
107
memcpy(str->str + str->length,append,length);
109
str->str[str->length]=0; /* Safety for C programs */
114
bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
117
str->str[str->length]= '\0';
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.
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.
131
@note The final argument in the list must be NullS even if no additional
134
@return True = Success.
137
bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
139
const char *quote_str= "\'";
140
const uint quote_len= 1;
144
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
145
va_start(dirty_text, append);
146
while (append != NullS)
148
const char *cur_pos= append;
149
const char *next_pos= cur_pos;
151
/* Search for quote in each string and replace with escaped quote */
152
while((next_pos= strrchr(cur_pos, quote_str[0])) != NULL)
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;
159
ret&= dynstr_append_mem(str, cur_pos, next_pos - cur_pos);
160
append= va_arg(dirty_text, char *);
163
ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */
169
void dynstr_free(DYNAMIC_STRING *str)
173
my_free(str->str,MYF(MY_WME));