be2aba5f8ae171b24c91968bb248ebd2389c7eaf
[plcapi.git] / php / xmlrpc / libxmlrpc / simplestring.c
1 /*
2   This file is part of libXMLRPC - a C library for xml-encoded function calls.
3
4   Author: Dan Libby (dan@libby.com)
5   Epinions.com may be contacted at feedback@epinions-inc.com
6 */
7
8 /*  
9   Copyright 2000 Epinions, Inc. 
10
11   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
12   of charge, to (a) use, copy, distribute, modify, perform and display this 
13   software and associated documentation files (the "Software"), and (b) 
14   permit others to whom the Software is furnished to do so as well.  
15
16   1) The above copyright notice and this permission notice shall be included 
17   without modification in all copies or substantial portions of the 
18   Software.  
19
20   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
21   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
22   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
23   PURPOSE OR NONINFRINGEMENT.  
24
25   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
26   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
27   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
28   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
29   DAMAGES.    
30
31 */
32
33
34 static const char rcsid[] = "#(@) $Id$";
35
36
37 #define SIMPLESTRING_INCR 32
38
39 /****h* ABOUT/simplestring
40  * NAME
41  *   simplestring
42  * AUTHOR
43  *   Dan Libby, aka danda  (dan@libby.com)
44  * CREATION DATE
45  *   06/2000
46  * HISTORY
47  *   $Log: simplestring.c,v $
48  *   Revision 1.4  2003/12/16 21:00:21  sniper
49  *   Fix some compile warnings (patch by Joe Orton)
50  *
51  *   Revision 1.3  2002/08/22 01:25:50  sniper
52  *   kill some compile warnings
53  *
54  *   Revision 1.2  2002/07/05 04:43:53  danda
55  *   merged in updates from SF project.  bring php repository up to date with xmlrpc-epi version 0.51
56  *
57  *   Revision 1.4  2002/02/13 20:58:50  danda
58  *   patch to make source more windows friendly, contributed by Jeff Lawson
59  *
60  *   Revision 1.3  2001/09/29 21:58:05  danda
61  *   adding cvs log to history section
62  *
63  *   10/15/2000 -- danda -- adding robodoc documentation
64  * PORTABILITY
65  *   Coded on RedHat Linux 6.2.  Builds on Solaris x86.  Should build on just
66  *   about anything with minor mods.
67  * NOTES
68  *   This code was written primarily for xmlrpc, but has found some other uses.
69  *
70  *   simplestring is, as the name implies, a simple API for dealing with C strings.
71  *   Why would I write yet another string API?  Because I couldn't find any that were
72  *   a) free / GPL, b) simple/lightweight, c) fast, not doing unneccesary strlens all
73  *   over the place.  So.  It is simple, and it seems to work, and it is pretty fast.
74  *
75  *   Oh, and it is also binary safe, ie it can handle strings with embedded NULLs,
76  *   so long as the real length is passed in.
77  *   
78  *   And the masses rejoiced.
79  *
80  * BUGS
81  *   there must be some.
82  ******/
83
84 #include <stdlib.h>
85 #include <string.h>
86 #include "simplestring.h"
87
88 #define my_free(thing)  if(thing) {free(thing); thing = 0;}
89
90 /*----------------------**
91 * Begin String Functions *
92 *-----------------------*/
93
94 /****f* FUNC/simplestring_init
95  * NAME
96  *   simplestring_init
97  * SYNOPSIS
98  *   void simplestring_init(simplestring* string)
99  * FUNCTION
100  *   initialize string
101  * INPUTS
102  *   string - pointer to a simplestring struct that will be initialized
103  * RESULT
104  *   void
105  * NOTES
106  * SEE ALSO
107  *   simplestring_free ()
108  *   simplestring_clear ()
109  * SOURCE
110  */
111 void simplestring_init(simplestring* string) {
112    memset(string, 0, sizeof(simplestring));
113 }
114 /******/
115
116 static void simplestring_init_str(simplestring* string) {
117    string->str = (char*)malloc(SIMPLESTRING_INCR);
118    if(string->str) {
119       string->str[0] = 0;
120       string->len = 0;
121       string->size = SIMPLESTRING_INCR;
122    }
123    else {
124       string->size = 0;
125    }
126 }
127
128 /****f* FUNC/simplestring_clear
129  * NAME
130  *   simplestring_clear
131  * SYNOPSIS
132  *   void simplestring_clear(simplestring* string)
133  * FUNCTION
134  *   clears contents of a string
135  * INPUTS
136  *   string - the string value to clear
137  * RESULT
138  *   void
139  * NOTES
140  *   This function is very fast as it does not de-allocate any memory.
141  * SEE ALSO
142  * 
143  * SOURCE
144  */
145 void simplestring_clear(simplestring* string) {
146    if(string->str) {
147       string->str[0] = 0;
148    }
149    string->len = 0;
150 }
151 /******/
152
153 /****f* FUNC/simplestring_free
154  * NAME
155  *   simplestring_free
156  * SYNOPSIS
157  *   void simplestring_free(simplestring* string)
158  * FUNCTION
159  *   frees contents of a string, if any. Does *not* free the simplestring struct itself.
160  * INPUTS
161  *   string - value containing string to be free'd
162  * RESULT
163  *   void
164  * NOTES
165  *   caller is responsible for allocating and freeing simplestring* struct itself.
166  * SEE ALSO
167  *   simplestring_init ()
168  * SOURCE
169  */
170 void simplestring_free(simplestring* string) {
171    if(string && string->str) {
172       my_free(string->str);
173       string->len = 0;
174    }
175 }
176 /******/
177
178 /****f* FUNC/simplestring_addn
179  * NAME
180  *   simplestring_addn
181  * SYNOPSIS
182  *   void simplestring_addn(simplestring* string, const char* add, int add_len)
183  * FUNCTION
184  *   copies n characters from source to target string
185  * INPUTS
186  *   target  - target string
187  *   source  - source string
188  *   add_len - number of characters to copy
189  * RESULT
190  *   void
191  * NOTES
192  * SEE ALSO
193  *   simplestring_add ()
194  * SOURCE
195  */
196 void simplestring_addn(simplestring* target, const char* source, int add_len) {
197    if(target && source) {
198       if(!target->str) {
199          simplestring_init_str(target);
200       }
201       if(target->len + add_len + 1 > target->size) {
202          /* newsize is current length + new length */
203          int newsize = target->len + add_len + 1;
204          int incr = target->size * 2;
205
206          /* align to SIMPLESTRING_INCR increments */
207          newsize = newsize - (newsize % incr) + incr;
208          target->str = (char*)realloc(target->str, newsize);
209
210          target->size = target->str ? newsize : 0;
211       }
212
213       if(target->str) {
214          if(add_len) {
215             memcpy(target->str + target->len, source, add_len);
216          }
217          target->len += add_len;
218          target->str[target->len] = 0; /* null terminate */
219       }
220    }
221 }
222 /******/
223
224 /****f* FUNC/simplestring_add
225  * NAME
226  *   simplestring_add
227  * SYNOPSIS
228  *   void simplestring_add(simplestring* string, const char* add)
229  * FUNCTION
230  *   appends a string of unknown length from source to target
231  * INPUTS
232  *   target - the target string to append to
233  *   source - the source string of unknown length
234  * RESULT
235  *   void
236  * NOTES
237  * SEE ALSO
238  *   simplestring_addn ()
239  * SOURCE
240  */
241 void simplestring_add(simplestring* target, const char* source) {
242    if(target && source) {
243       simplestring_addn(target, source, strlen(source));
244    }
245 }
246 /******/
247
248
249 /*----------------------
250 * End String Functions *
251 *--------------------**/