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;
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))))
93 return dynstr_append_mem(str,append,(uint) strlen(append));
101 if (str->length+length >= str->max_length)
103 size_t 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;
121 str->str[str->length]=
'\0';
141 my_bool dynstr_append_os_quoted(
DYNAMIC_STRING *str,
const char *append, ...)
144 const char *quote_str=
"\"";
145 const uint quote_len= 1;
147 const char *quote_str=
"\'";
148 const uint quote_len= 1;
153 ret&= dynstr_append_mem(str, quote_str, quote_len);
154 va_start(dirty_text, append);
155 while (append != NullS)
157 const char *cur_pos= append;
158 const char *next_pos= cur_pos;
161 while(*(next_pos= strcend(cur_pos, quote_str[0])) !=
'\0')
163 ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
164 ret&= dynstr_append_mem(str ,
"\\", 1);
165 ret&= dynstr_append_mem(str, quote_str, quote_len);
166 cur_pos= next_pos + 1;
168 ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
169 append= va_arg(dirty_text,
char *);
172 ret&= dynstr_append_mem(str, quote_str, quote_len);