- Main Page
- Data Structures
- Files

- File List
- Globals

# jasperserver/stringbuffer.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2007 JasperSoft http://www.jaspersoft.com
    00003  *
    00004  * This program is free software; you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation; either version 2 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed WITHOUT ANY WARRANTY; and without the
    00010  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00011  * See the GNU General Public License for more details.
    00012  *
    00013  * You should have received a copy of the GNU General Public License
    00014  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
    00015  * or write to:
    00016  *
    00017  * Free Software Foundation, Inc.,
    00018  * 59 Temple Place - Suite 330,
    00019  * Boston, MA  USA  02111-1307
    00020  */
    00021
    00022 #include "stringbuffer.h"
    00023 #include <stdlib.h>
    00024 #include <string.h>
    00025 #include <stdio.h>
    00026
    00027 #define INCREMENT_STRING_SIZE 256
    00028
    00040 static  xmlChar * jasperserver_stringVSprintf(const char *format, va_list argptr);
    00041
    00053 int jasperserver_string_vformat(jasperserver_string_t *jsstr,const char *format, va_list argptr);
    00054
    00055
    00056 jasperserver_string_t *jasperserver_string_new()
    00057 {
    00058        jasperserver_string_t *jsstr = (jasperserver_string_t *)malloc( sizeof(jasperserver_string_t));
    00059        //jasperserver_init_string(jsstr);
    00060        jsstr->buffer = xmlBufferCreate();
    00061        return jsstr;
    00062 }
    00063
    00064 //void jasperserver_init_string(jasperserver_string_t *jsstr)
    00065 //{
    00066 //     jsstr->buffer = NULL;
    00067 //     jsstr->bufSize = 0;
    00068 //     jsstr->strLen = 0;
    00069 //}
    00070
    00071 void jasperserver_string_free(jasperserver_string_t *jsstr)
    00072 {
    00073      if (!jsstr) return;
    00074      if (jsstr->buffer)
    00075      {
    00076         xmlBufferFree(jsstr->buffer);
    00077      }
    00078 }
    00079 /*
    00080 * Append a zero terminated C string to an XML buffer.
    00081 */
    00082 void jasperserver_string_cappend(jasperserver_string_t *jsstr,const char *utf)
    00083 {
    00084     if (!utf || !jsstr)
    00085     {
    00086       return;
    00087     }
    00088
    00089     xmlBufferCCat(jsstr->buffer, utf);
    00090 }
    00091
    00092 /*
    00093 * Append a zero terminated string to an jasperserver_string.
    00094 */
    00095 void jasperserver_string_append(jasperserver_string_t *jsstr,const xmlChar *utf)
    00096 {
    00097     if (!utf || !jsstr)
    00098     {
    00099       return;
    00100     }
    00101
    00102     xmlBufferCat(jsstr->buffer, utf);
    00103 }
    00104
    00105 //    if (jsstr->buffer == NULL)
    00106 //    {
    00107 //       jsstr->buffer = (char *)malloc(INCREMENT_STRING_SIZE);
    00108 //       jsstr->bufSize = INCREMENT_STRING_SIZE;
    00109 //       sprintf(jsstr->buffer, "");
    00110 //       jsstr->strLen = 0;
    00111 //    }
    00112 //
    00113 //    while (xmlUTF8Strsize(str)+1 > jsstr->bufSize - jsstr->strLen)
    00114 //    {
    00115 //        jsstr->buffer = (char *)realloc(jsstr->buffer, (jsstr->bufSize + INCREMENT_STRING_SIZE)* sizeof(char));
    00116 //        jsstr->bufSize += INCREMENT_STRING_SIZE;
    00117 //    }
    00118 //    strncat(jsstr->buffer,str,strlen(str));
    00119 //    jsstr->strLen += strlen(str);
    00120
    00121
    00122
    00123 //void jasperserver_replace_string(jasperserver_string_t *jsstr,const char *oldString, const char *newString)
    00124 //{
    00125 //     char *sPtr;
    00126 //     if (!oldString || !jsstr || !newString)
    00127 //     {
    00128 //       return;
    00129 //     }
    00130 //
    00131 //     sPtr = strstr(jsstr->buffer, oldString);
    00132 //     while (sPtr != NULL)
    00133 //     {
    00134 //           // Move the remaining string...
    00135 //           int pos = strlen(newString) - strlen(oldString);
    00136 //
    00137 //           if (pos != 0)
    00138 //           {
    00139 //               // check in there is space available...
    00140 //               if (jsstr->strLen + 1 + pos > jsstr->bufSize)
    00141 //               {
    00142 //                  jsstr->buffer = (char *)realloc(jsstr->buffer, (jsstr->bufSize + INCREMENT_STRING_SIZE)* sizeof(char));
    00143 //                  jsstr->bufSize += INCREMENT_STRING_SIZE;
    00144 //               }
    00145 //               printf("Positions: %d",pos);
    00146 //               fflush(stdout);
    00147 //               memmove(sPtr + strlen(newString), sPtr + strlen(oldString), strlen(sPtr)+1);
    00148 //               jsstr->strLen += pos;
    00149 //           }
    00150 //           memcpy(sPtr, newString, strlen(newString));
    00151 //
    00152 //           sPtr += strlen(newString);
    00153 //           sPtr = strstr(sPtr, oldString);
    00154 //     }
    00155 //
    00156 //}
    00157
    00158
    00159 void jasperserver_string_set(jasperserver_string_t *jsstr,const xmlChar *str)
    00160 {
    00161     if (!jsstr || !str) return;
    00162
    00163     xmlBufferEmpty( jsstr->buffer );
    00164     jasperserver_string_append(jsstr, str );
    00165 }
    00166
    00167 void jasperserver_string_cset(jasperserver_string_t *jsstr,const char *str)
    00168 {
    00169     if (!jsstr || !str) return;
    00170
    00171     xmlBufferEmpty( jsstr->buffer );
    00172     jasperserver_string_cappend(jsstr, str );
    00173 }
    00174
    00175
    00176 int jasperserver_string_format(jasperserver_string_t *jsstr,const char *format, ...)
    00177 {
    00178     int rc;
    00179     xmlChar *buf;
    00180     va_list ap;
    00181
    00182     if (format == NULL) return -1;
    00183
    00184     va_start(ap, format);
    00185
    00186     rc = jasperserver_string_vformat(jsstr, format, ap);
    00187
    00188     va_end(ap);
    00189     return rc;
    00190 }
    00191
    00192
    00193 int jasperserver_string_vformat(jasperserver_string_t *jsstr,const char *format, va_list argptr)
    00194 {
    00195     int rc;
    00196     xmlChar *buf;
    00197
    00198     buf = jasperserver_stringVSprintf(format, argptr);
    00199     if (buf == 0) return 0;
    00200     jasperserver_string_append(jsstr, buf);
    00201     rc = strlen(buf);
    00202     xmlFree(buf);
    00203     return rc;
    00204 }
    00205
    00206
    00207 static xmlChar *
    00208 jasperserver_stringVSprintf(const char *format, va_list argptr)
    00209 {
    00210     int size;
    00211     int count;
    00212     xmlChar *buf;
    00213
    00214     size = 512;
    00215     buf = (xmlChar *) xmlMalloc(size);
    00216     if (buf == NULL) {
    00217         return NULL;
    00218     }
    00219
    00220     while (((count = vsnprintf((char *) buf, size, format, argptr)) < 0)
    00221            || (count == size - 1) || (count == size) || (count > size)) {
    00222         xmlFree(buf);
    00223         size += 512;
    00224         buf = (xmlChar *) xmlMalloc(size);
    00225         if (buf == NULL) {
    00226             return NULL;
    00227         }
    00228     }
    00229
    00230     return buf;
    00231 }