2 This file is part of libXMLRPC - a C library for xml-encoded function calls.
4 Author: Dan Libby (dan@libby.com)
5 Epinions.com may be contacted at feedback@epinions-inc.com
9 Copyright 2000 Epinions, Inc.
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.
16 1) The above copyright notice and this permission notice shall be included
17 without modification in all copies or substantial portions of the
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.
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
34 static const char rcsid[] = "#(@) $Id$";
37 #define SIMPLESTRING_INCR 32
39 /****h* ABOUT/simplestring
43 * Dan Libby, aka danda (dan@libby.com)
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)
51 * Revision 1.3 2002/08/22 01:25:50 sniper
52 * kill some compile warnings
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
57 * Revision 1.4 2002/02/13 20:58:50 danda
58 * patch to make source more windows friendly, contributed by Jeff Lawson
60 * Revision 1.3 2001/09/29 21:58:05 danda
61 * adding cvs log to history section
63 * 10/15/2000 -- danda -- adding robodoc documentation
65 * Coded on RedHat Linux 6.2. Builds on Solaris x86. Should build on just
66 * about anything with minor mods.
68 * This code was written primarily for xmlrpc, but has found some other uses.
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.
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.
78 * And the masses rejoiced.
86 #include "simplestring.h"
88 #define my_free(thing) if(thing) {free(thing); thing = 0;}
90 /*----------------------**
91 * Begin String Functions *
92 *-----------------------*/
94 /****f* FUNC/simplestring_init
98 * void simplestring_init(simplestring* string)
102 * string - pointer to a simplestring struct that will be initialized
107 * simplestring_free ()
108 * simplestring_clear ()
111 void simplestring_init(simplestring* string) {
112 memset(string, 0, sizeof(simplestring));
116 static void simplestring_init_str(simplestring* string) {
117 string->str = (char*)malloc(SIMPLESTRING_INCR);
121 string->size = SIMPLESTRING_INCR;
128 /****f* FUNC/simplestring_clear
132 * void simplestring_clear(simplestring* string)
134 * clears contents of a string
136 * string - the string value to clear
140 * This function is very fast as it does not de-allocate any memory.
145 void simplestring_clear(simplestring* string) {
153 /****f* FUNC/simplestring_free
157 * void simplestring_free(simplestring* string)
159 * frees contents of a string, if any. Does *not* free the simplestring struct itself.
161 * string - value containing string to be free'd
165 * caller is responsible for allocating and freeing simplestring* struct itself.
167 * simplestring_init ()
170 void simplestring_free(simplestring* string) {
171 if(string && string->str) {
172 my_free(string->str);
178 /****f* FUNC/simplestring_addn
182 * void simplestring_addn(simplestring* string, const char* add, int add_len)
184 * copies n characters from source to target string
186 * target - target string
187 * source - source string
188 * add_len - number of characters to copy
193 * simplestring_add ()
196 void simplestring_addn(simplestring* target, const char* source, int add_len) {
197 if(target && source) {
199 simplestring_init_str(target);
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;
206 /* align to SIMPLESTRING_INCR increments */
207 newsize = newsize - (newsize % incr) + incr;
208 target->str = (char*)realloc(target->str, newsize);
210 target->size = target->str ? newsize : 0;
215 memcpy(target->str + target->len, source, add_len);
217 target->len += add_len;
218 target->str[target->len] = 0; /* null terminate */
224 /****f* FUNC/simplestring_add
228 * void simplestring_add(simplestring* string, const char* add)
230 * appends a string of unknown length from source to target
232 * target - the target string to append to
233 * source - the source string of unknown length
238 * simplestring_addn ()
241 void simplestring_add(simplestring* target, const char* source) {
242 if(target && source) {
243 simplestring_addn(target, source, strlen(source));
249 /*----------------------
250 * End String Functions *
251 *--------------------**/