summaryrefslogtreecommitdiff
path: root/ext/rpc/xmlrpc/libxmlrpc/simplestring.c
blob: 7ce68d34101c3525d6d16e9af2d0ed428ee98503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
  This file is part of libXMLRPC - a C library for xml-encoded function calls.

  Author: Dan Libby (dan@libby.com)
  Epinions.com may be contacted at feedback@epinions-inc.com
*/

/*  
  Copyright 2000 Epinions, Inc. 

  Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
  of charge, to (a) use, copy, distribute, modify, perform and display this 
  software and associated documentation files (the "Software"), and (b) 
  permit others to whom the Software is furnished to do so as well.  

  1) The above copyright notice and this permission notice shall be included 
  without modification in all copies or substantial portions of the 
  Software.  

  2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
  ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
  IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  PURPOSE OR NONINFRINGEMENT.  

  3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
  SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
  OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
  NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
  DAMAGES.    

*/


static const char rcsid[] = "#(@) $Id$";


#define SIMPLESTRING_INCR 32

/****h* ABOUT/simplestring
 * NAME
 *   simplestring
 * AUTHOR
 *   Dan Libby, aka danda  (dan@libby.com)
 * CREATION DATE
 *   06/2000
 * HISTORY
 *   10/15/2000 -- danda -- adding robodoc documentation
 * PORTABILITY
 *   Coded on RedHat Linux 6.2.  Builds on Solaris x86.  Should build on just
 *   about anything with minor mods.
 * NOTES
 *   This code was written primarily for xmlrpc, but has found some other uses.
 *
 *   simplestring is, as the name implies, a simple API for dealing with C strings.
 *   Why would I write yet another string API?  Because I couldn't find any that were
 *   a) free / GPL, b) simple/lightweight, c) fast, not doing unneccesary strlens all
 *   over the place.  So.  It is simple, and it seems to work, and it is pretty fast.
 *
 *   Oh, and it is also binary safe, ie it can handle strings with embedded NULLs,
 *   so long as the real length is passed in.
 *   
 *   And the masses rejoiced.
 *
 * BUGS
 *   there must be some.
 ******/


#include "simplestring.h"

#define my_free(thing)  if(thing) {free(thing); thing = 0;}

/*----------------------**
* Begin String Functions *
*-----------------------*/

/****f* FUNC/simplestring_init
 * NAME
 *   simplestring_init
 * SYNOPSIS
 *   void simplestring_init(simplestring* string)
 * FUNCTION
 *   initialize string
 * INPUTS
 *   string - pointer to a simplestring struct that will be initialized
 * RESULT
 *   void
 * NOTES
 * SEE ALSO
 *   simplestring_free ()
 *   simplestring_clear ()
 * SOURCE
 */
void simplestring_init(simplestring* string) {
   memset(string, 0, sizeof(simplestring));
}
/******/

static void simplestring_init_str(simplestring* string) {
   string->str = (char*)malloc(SIMPLESTRING_INCR);
   if(string->str) {
      string->str[0] = 0;
      string->len = 0;
      string->size = SIMPLESTRING_INCR;
   }
   else {
      string->size = 0;
   }
}

/****f* FUNC/simplestring_clear
 * NAME
 *   simplestring_clear
 * SYNOPSIS
 *   void simplestring_clear(simplestring* string)
 * FUNCTION
 *   clears contents of a string
 * INPUTS
 *   string - the string value to clear
 * RESULT
 *   void
 * NOTES
 *   This function is very fast as it does not de-allocate any memory.
 * SEE ALSO
 * 
 * SOURCE
 */
void simplestring_clear(simplestring* string) {
   if(string->str) {
      string->str[0] = 0;
   }
   string->len = 0;
}
/******/

/****f* FUNC/simplestring_free
 * NAME
 *   simplestring_free
 * SYNOPSIS
 *   void simplestring_free(simplestring* string)
 * FUNCTION
 *   frees contents of a string, if any. Does *not* free the simplestring struct itself.
 * INPUTS
 *   string - value containing string to be free'd
 * RESULT
 *   void
 * NOTES
 *   caller is responsible for allocating and freeing simplestring* struct itself.
 * SEE ALSO
 *   simplestring_init ()
 * SOURCE
 */
void simplestring_free(simplestring* string) {
   if(string && string->str) {
      my_free(string->str);
      string->len = 0;
   }
}
/******/

/****f* FUNC/simplestring_addn
 * NAME
 *   simplestring_addn
 * SYNOPSIS
 *   void simplestring_addn(simplestring* string, const char* add, int add_len)
 * FUNCTION
 *   copies n characters from source to target string
 * INPUTS
 *   target  - target string
 *   source  - source string
 *   add_len - number of characters to copy
 * RESULT
 *   void
 * NOTES
 * SEE ALSO
 *   simplestring_add ()
 * SOURCE
 */
void simplestring_addn(simplestring* target, const char* source, int add_len) {
   if(target && source) {
      if(!target->str) {
         simplestring_init_str(target);
      }
      if(target->len + add_len + 1 > target->size) {
         /* newsize is current length + new length */
         int newsize = target->len + add_len + 1;
         int incr = target->size * 2;

         /* align to SIMPLESTRING_INCR increments */
         newsize = newsize - (newsize % incr) + incr;
         target->str = (char*)realloc(target->str, newsize);

         target->size = target->str ? newsize : 0;
      }

      if(target->str) {
         if(add_len) {
            memcpy(target->str + target->len, source, add_len);
         }
         target->len += add_len;
         target->str[target->len] = 0; /* null terminate */
      }
   }
}
/******/

/****f* FUNC/simplestring_add
 * NAME
 *   simplestring_add
 * SYNOPSIS
 *   void simplestring_add(simplestring* string, const char* add)
 * FUNCTION
 *   appends a string of unknown length from source to target
 * INPUTS
 *   target - the target string to append to
 *   source - the source string of unknown length
 * RESULT
 *   void
 * NOTES
 * SEE ALSO
 *   simplestring_addn ()
 * SOURCE
 */
void simplestring_add(simplestring* target, const char* source) {
   if(target && source) {
      simplestring_addn(target, source, strlen(source));
   }
}
/******/


/*----------------------
* End String Functions *
*--------------------**/