Back to index

php5  5.3.10
simplestring.c
Go to the documentation of this file.
00001 /*
00002   This file is part of libXMLRPC - a C library for xml-encoded function calls.
00003 
00004   Author: Dan Libby (dan@libby.com)
00005   Epinions.com may be contacted at feedback@epinions-inc.com
00006 */
00007 
00008 /*  
00009   Copyright 2000 Epinions, Inc. 
00010 
00011   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
00012   of charge, to (a) use, copy, distribute, modify, perform and display this 
00013   software and associated documentation files (the "Software"), and (b) 
00014   permit others to whom the Software is furnished to do so as well.  
00015 
00016   1) The above copyright notice and this permission notice shall be included 
00017   without modification in all copies or substantial portions of the 
00018   Software.  
00019 
00020   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
00021   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
00022   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00023   PURPOSE OR NONINFRINGEMENT.  
00024 
00025   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
00026   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
00027   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
00028   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
00029   DAMAGES.    
00030 
00031 */
00032 
00033 
00034 static const char rcsid[] = "#(@) $Id: simplestring.c 146618 2003-12-16 21:00:21Z sniper $";
00035 
00036 
00037 #define SIMPLESTRING_INCR 32
00038 
00039 /****h* ABOUT/simplestring
00040  * NAME
00041  *   simplestring
00042  * AUTHOR
00043  *   Dan Libby, aka danda  (dan@libby.com)
00044  * CREATION DATE
00045  *   06/2000
00046  * HISTORY
00047  *   $Log$
00048  *   Revision 1.3  2002/08/22 01:25:50  sniper
00049  *   kill some compile warnings
00050  *
00051  *   Revision 1.2  2002/07/05 04:43:53  danda
00052  *   merged in updates from SF project.  bring php repository up to date with xmlrpc-epi version 0.51
00053  *
00054  *   Revision 1.4  2002/02/13 20:58:50  danda
00055  *   patch to make source more windows friendly, contributed by Jeff Lawson
00056  *
00057  *   Revision 1.3  2001/09/29 21:58:05  danda
00058  *   adding cvs log to history section
00059  *
00060  *   10/15/2000 -- danda -- adding robodoc documentation
00061  * PORTABILITY
00062  *   Coded on RedHat Linux 6.2.  Builds on Solaris x86.  Should build on just
00063  *   about anything with minor mods.
00064  * NOTES
00065  *   This code was written primarily for xmlrpc, but has found some other uses.
00066  *
00067  *   simplestring is, as the name implies, a simple API for dealing with C strings.
00068  *   Why would I write yet another string API?  Because I couldn't find any that were
00069  *   a) free / GPL, b) simple/lightweight, c) fast, not doing unneccesary strlens all
00070  *   over the place.  So.  It is simple, and it seems to work, and it is pretty fast.
00071  *
00072  *   Oh, and it is also binary safe, ie it can handle strings with embedded NULLs,
00073  *   so long as the real length is passed in.
00074  *   
00075  *   And the masses rejoiced.
00076  *
00077  * BUGS
00078  *   there must be some.
00079  ******/
00080 
00081 #include <stdlib.h>
00082 #include <string.h>
00083 #include "simplestring.h"
00084 
00085 #define my_free(thing)  if(thing) {free(thing); thing = 0;}
00086 
00087 /*----------------------**
00088 * Begin String Functions *
00089 *-----------------------*/
00090 
00091 /****f* FUNC/simplestring_init
00092  * NAME
00093  *   simplestring_init
00094  * SYNOPSIS
00095  *   void simplestring_init(simplestring* string)
00096  * FUNCTION
00097  *   initialize string
00098  * INPUTS
00099  *   string - pointer to a simplestring struct that will be initialized
00100  * RESULT
00101  *   void
00102  * NOTES
00103  * SEE ALSO
00104  *   simplestring_free ()
00105  *   simplestring_clear ()
00106  * SOURCE
00107  */
00108 void simplestring_init(simplestring* string) {
00109    memset(string, 0, sizeof(simplestring));
00110 }
00111 /******/
00112 
00113 static void simplestring_init_str(simplestring* string) {
00114    string->str = (char*)malloc(SIMPLESTRING_INCR);
00115    if(string->str) {
00116       string->str[0] = 0;
00117       string->len = 0;
00118       string->size = SIMPLESTRING_INCR;
00119    }
00120    else {
00121       string->size = 0;
00122    }
00123 }
00124 
00125 /****f* FUNC/simplestring_clear
00126  * NAME
00127  *   simplestring_clear
00128  * SYNOPSIS
00129  *   void simplestring_clear(simplestring* string)
00130  * FUNCTION
00131  *   clears contents of a string
00132  * INPUTS
00133  *   string - the string value to clear
00134  * RESULT
00135  *   void
00136  * NOTES
00137  *   This function is very fast as it does not de-allocate any memory.
00138  * SEE ALSO
00139  * 
00140  * SOURCE
00141  */
00142 void simplestring_clear(simplestring* string) {
00143    if(string->str) {
00144       string->str[0] = 0;
00145    }
00146    string->len = 0;
00147 }
00148 /******/
00149 
00150 /****f* FUNC/simplestring_free
00151  * NAME
00152  *   simplestring_free
00153  * SYNOPSIS
00154  *   void simplestring_free(simplestring* string)
00155  * FUNCTION
00156  *   frees contents of a string, if any. Does *not* free the simplestring struct itself.
00157  * INPUTS
00158  *   string - value containing string to be free'd
00159  * RESULT
00160  *   void
00161  * NOTES
00162  *   caller is responsible for allocating and freeing simplestring* struct itself.
00163  * SEE ALSO
00164  *   simplestring_init ()
00165  * SOURCE
00166  */
00167 void simplestring_free(simplestring* string) {
00168    if(string && string->str) {
00169       my_free(string->str);
00170       string->len = 0;
00171    }
00172 }
00173 /******/
00174 
00175 /****f* FUNC/simplestring_addn
00176  * NAME
00177  *   simplestring_addn
00178  * SYNOPSIS
00179  *   void simplestring_addn(simplestring* string, const char* add, int add_len)
00180  * FUNCTION
00181  *   copies n characters from source to target string
00182  * INPUTS
00183  *   target  - target string
00184  *   source  - source string
00185  *   add_len - number of characters to copy
00186  * RESULT
00187  *   void
00188  * NOTES
00189  * SEE ALSO
00190  *   simplestring_add ()
00191  * SOURCE
00192  */
00193 void simplestring_addn(simplestring* target, const char* source, int add_len) {
00194    if(target && source) {
00195       if(!target->str) {
00196          simplestring_init_str(target);
00197       }
00198       if(target->len + add_len + 1 > target->size) {
00199          /* newsize is current length + new length */
00200          int newsize = target->len + add_len + 1;
00201          int incr = target->size * 2;
00202 
00203          /* align to SIMPLESTRING_INCR increments */
00204          newsize = newsize - (newsize % incr) + incr;
00205          target->str = (char*)realloc(target->str, newsize);
00206 
00207          target->size = target->str ? newsize : 0;
00208       }
00209 
00210       if(target->str) {
00211          if(add_len) {
00212             memcpy(target->str + target->len, source, add_len);
00213          }
00214          target->len += add_len;
00215          target->str[target->len] = 0; /* null terminate */
00216       }
00217    }
00218 }
00219 /******/
00220 
00221 /****f* FUNC/simplestring_add
00222  * NAME
00223  *   simplestring_add
00224  * SYNOPSIS
00225  *   void simplestring_add(simplestring* string, const char* add)
00226  * FUNCTION
00227  *   appends a string of unknown length from source to target
00228  * INPUTS
00229  *   target - the target string to append to
00230  *   source - the source string of unknown length
00231  * RESULT
00232  *   void
00233  * NOTES
00234  * SEE ALSO
00235  *   simplestring_addn ()
00236  * SOURCE
00237  */
00238 void simplestring_add(simplestring* target, const char* source) {
00239    if(target && source) {
00240       simplestring_addn(target, source, strlen(source));
00241    }
00242 }
00243 /******/
00244 
00245 
00246 /*----------------------
00247 * End String Functions *
00248 *--------------------**/