Annotation of embedaddon/php/ext/xmlrpc/libxmlrpc/simplestring.c, revision 1.1
1.1 ! misho 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: simplestring.c 146618 2003-12-16 21:00:21Z sniper $";
! 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$
! 48: * Revision 1.3 2002/08/22 01:25:50 sniper
! 49: * kill some compile warnings
! 50: *
! 51: * Revision 1.2 2002/07/05 04:43:53 danda
! 52: * merged in updates from SF project. bring php repository up to date with xmlrpc-epi version 0.51
! 53: *
! 54: * Revision 1.4 2002/02/13 20:58:50 danda
! 55: * patch to make source more windows friendly, contributed by Jeff Lawson
! 56: *
! 57: * Revision 1.3 2001/09/29 21:58:05 danda
! 58: * adding cvs log to history section
! 59: *
! 60: * 10/15/2000 -- danda -- adding robodoc documentation
! 61: * PORTABILITY
! 62: * Coded on RedHat Linux 6.2. Builds on Solaris x86. Should build on just
! 63: * about anything with minor mods.
! 64: * NOTES
! 65: * This code was written primarily for xmlrpc, but has found some other uses.
! 66: *
! 67: * simplestring is, as the name implies, a simple API for dealing with C strings.
! 68: * Why would I write yet another string API? Because I couldn't find any that were
! 69: * a) free / GPL, b) simple/lightweight, c) fast, not doing unneccesary strlens all
! 70: * over the place. So. It is simple, and it seems to work, and it is pretty fast.
! 71: *
! 72: * Oh, and it is also binary safe, ie it can handle strings with embedded NULLs,
! 73: * so long as the real length is passed in.
! 74: *
! 75: * And the masses rejoiced.
! 76: *
! 77: * BUGS
! 78: * there must be some.
! 79: ******/
! 80:
! 81: #include <stdlib.h>
! 82: #include <string.h>
! 83: #include "simplestring.h"
! 84:
! 85: #define my_free(thing) if(thing) {free(thing); thing = 0;}
! 86:
! 87: /*----------------------**
! 88: * Begin String Functions *
! 89: *-----------------------*/
! 90:
! 91: /****f* FUNC/simplestring_init
! 92: * NAME
! 93: * simplestring_init
! 94: * SYNOPSIS
! 95: * void simplestring_init(simplestring* string)
! 96: * FUNCTION
! 97: * initialize string
! 98: * INPUTS
! 99: * string - pointer to a simplestring struct that will be initialized
! 100: * RESULT
! 101: * void
! 102: * NOTES
! 103: * SEE ALSO
! 104: * simplestring_free ()
! 105: * simplestring_clear ()
! 106: * SOURCE
! 107: */
! 108: void simplestring_init(simplestring* string) {
! 109: memset(string, 0, sizeof(simplestring));
! 110: }
! 111: /******/
! 112:
! 113: static void simplestring_init_str(simplestring* string) {
! 114: string->str = (char*)malloc(SIMPLESTRING_INCR);
! 115: if(string->str) {
! 116: string->str[0] = 0;
! 117: string->len = 0;
! 118: string->size = SIMPLESTRING_INCR;
! 119: }
! 120: else {
! 121: string->size = 0;
! 122: }
! 123: }
! 124:
! 125: /****f* FUNC/simplestring_clear
! 126: * NAME
! 127: * simplestring_clear
! 128: * SYNOPSIS
! 129: * void simplestring_clear(simplestring* string)
! 130: * FUNCTION
! 131: * clears contents of a string
! 132: * INPUTS
! 133: * string - the string value to clear
! 134: * RESULT
! 135: * void
! 136: * NOTES
! 137: * This function is very fast as it does not de-allocate any memory.
! 138: * SEE ALSO
! 139: *
! 140: * SOURCE
! 141: */
! 142: void simplestring_clear(simplestring* string) {
! 143: if(string->str) {
! 144: string->str[0] = 0;
! 145: }
! 146: string->len = 0;
! 147: }
! 148: /******/
! 149:
! 150: /****f* FUNC/simplestring_free
! 151: * NAME
! 152: * simplestring_free
! 153: * SYNOPSIS
! 154: * void simplestring_free(simplestring* string)
! 155: * FUNCTION
! 156: * frees contents of a string, if any. Does *not* free the simplestring struct itself.
! 157: * INPUTS
! 158: * string - value containing string to be free'd
! 159: * RESULT
! 160: * void
! 161: * NOTES
! 162: * caller is responsible for allocating and freeing simplestring* struct itself.
! 163: * SEE ALSO
! 164: * simplestring_init ()
! 165: * SOURCE
! 166: */
! 167: void simplestring_free(simplestring* string) {
! 168: if(string && string->str) {
! 169: my_free(string->str);
! 170: string->len = 0;
! 171: }
! 172: }
! 173: /******/
! 174:
! 175: /****f* FUNC/simplestring_addn
! 176: * NAME
! 177: * simplestring_addn
! 178: * SYNOPSIS
! 179: * void simplestring_addn(simplestring* string, const char* add, int add_len)
! 180: * FUNCTION
! 181: * copies n characters from source to target string
! 182: * INPUTS
! 183: * target - target string
! 184: * source - source string
! 185: * add_len - number of characters to copy
! 186: * RESULT
! 187: * void
! 188: * NOTES
! 189: * SEE ALSO
! 190: * simplestring_add ()
! 191: * SOURCE
! 192: */
! 193: void simplestring_addn(simplestring* target, const char* source, int add_len) {
! 194: if(target && source) {
! 195: if(!target->str) {
! 196: simplestring_init_str(target);
! 197: }
! 198: if(target->len + add_len + 1 > target->size) {
! 199: /* newsize is current length + new length */
! 200: int newsize = target->len + add_len + 1;
! 201: int incr = target->size * 2;
! 202:
! 203: /* align to SIMPLESTRING_INCR increments */
! 204: newsize = newsize - (newsize % incr) + incr;
! 205: target->str = (char*)realloc(target->str, newsize);
! 206:
! 207: target->size = target->str ? newsize : 0;
! 208: }
! 209:
! 210: if(target->str) {
! 211: if(add_len) {
! 212: memcpy(target->str + target->len, source, add_len);
! 213: }
! 214: target->len += add_len;
! 215: target->str[target->len] = 0; /* null terminate */
! 216: }
! 217: }
! 218: }
! 219: /******/
! 220:
! 221: /****f* FUNC/simplestring_add
! 222: * NAME
! 223: * simplestring_add
! 224: * SYNOPSIS
! 225: * void simplestring_add(simplestring* string, const char* add)
! 226: * FUNCTION
! 227: * appends a string of unknown length from source to target
! 228: * INPUTS
! 229: * target - the target string to append to
! 230: * source - the source string of unknown length
! 231: * RESULT
! 232: * void
! 233: * NOTES
! 234: * SEE ALSO
! 235: * simplestring_addn ()
! 236: * SOURCE
! 237: */
! 238: void simplestring_add(simplestring* target, const char* source) {
! 239: if(target && source) {
! 240: simplestring_addn(target, source, strlen(source));
! 241: }
! 242: }
! 243: /******/
! 244:
! 245:
! 246: /*----------------------
! 247: * End String Functions *
! 248: *--------------------**/
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>