~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/string.c

  • Committer: Monty Taylor
  • Date: 2008-09-05 22:55:50 UTC
  • mfrom: (373.1.9 stl-client-progs)
  • Revision ID: monty@inaugust.com-20080905225550-zco374c9s7kxwqyb
Merged removal of DYNAMIC_STRING.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
23
 
#include <mystrings/m_string.h>
24
 
 
25
 
bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
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))))
39
 
    return(true);
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;
45
 
  return(false);
46
 
}
47
 
 
48
 
 
49
 
bool dynstr_set(DYNAMIC_STRING *str, const char *init_str)
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))))
60
 
      return(true);
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;
69
 
  return(false);
70
 
}
71
 
 
72
 
 
73
 
bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size)
74
 
{
75
 
  if (!additional_size) return(false);
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))))
81
 
      return(true);
82
 
  }
83
 
  return(false);
84
 
}
85
 
 
86
 
 
87
 
bool dynstr_append(DYNAMIC_STRING *str, const char *append)
88
 
{
89
 
  return dynstr_append_mem(str,append,(uint) strlen(append));
90
 
}
91
 
 
92
 
 
93
 
bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,
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))))
103
 
      return true;
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 */
110
 
  return false;
111
 
}
112
 
 
113
 
 
114
 
bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
115
 
{
116
 
  str->length-=n;
117
 
  str->str[str->length]= '\0';
118
 
  return false;
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
 
 
137
 
bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
138
 
{
139
 
  const char *quote_str= "\'";
140
 
  const uint  quote_len= 1;
141
 
  bool ret= true;
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= strrchr(cur_pos, quote_str[0])) != NULL)
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
 
}