Back to index

php5  5.3.10
mbfl_memory_device.c
Go to the documentation of this file.
00001 /*
00002  * "streamable kanji code filter and converter"
00003  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
00004  *
00005  * LICENSE NOTICES
00006  *
00007  * This file is part of "streamable kanji code filter and converter",
00008  * which is distributed under the terms of GNU Lesser General Public 
00009  * License (version 2) as published by the Free Software Foundation.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with "streamable kanji code filter and converter";
00018  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
00019  * Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * The author of this file:
00022  *
00023  */
00024 /*
00025  * The source code included in this files was separated from mbfilter.c
00026  * by Moriyoshi Koizumi <moriyoshi@php.net> on 20 Dec 2002. The file
00027  * mbfilter.c is included in this package .
00028  *
00029  */
00030 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 
00035 #ifdef HAVE_STDDEF_H
00036 #include <stddef.h>
00037 #endif
00038 
00039 #include "mbfl_allocators.h"
00040 #include "mbfl_string.h"
00041 #include "mbfl_memory_device.h"
00042 
00043 /*
00044  * memory device output functions
00045  */
00046 void
00047 mbfl_memory_device_init(mbfl_memory_device *device, int initsz, int allocsz)
00048 {
00049        if (device) {
00050               device->length = 0;
00051               device->buffer = (unsigned char *)0;
00052               if (initsz > 0) {
00053                      device->buffer = (unsigned char *)mbfl_malloc(initsz*sizeof(unsigned char));
00054                      if (device->buffer != NULL) {
00055                             device->length = initsz;
00056                      }
00057               }
00058               device->pos= 0;
00059               if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
00060                      device->allocsz = allocsz;
00061               } else {
00062                      device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
00063               }
00064        }
00065 }
00066 
00067 void
00068 mbfl_memory_device_realloc(mbfl_memory_device *device, int initsz, int allocsz)
00069 {
00070        unsigned char *tmp;
00071 
00072        if (device) {
00073               if (initsz > device->length) {
00074                      tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, initsz*sizeof(unsigned char));
00075                      if (tmp != NULL) {
00076                             device->buffer = tmp;
00077                             device->length = initsz;
00078                      }
00079               }
00080               if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
00081                      device->allocsz = allocsz;
00082               } else {
00083                      device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
00084               }
00085        }
00086 }
00087 
00088 void
00089 mbfl_memory_device_clear(mbfl_memory_device *device)
00090 {
00091        if (device) {
00092               if (device->buffer) {
00093                      mbfl_free(device->buffer);
00094               }
00095               device->buffer = (unsigned char *)0;
00096               device->length = 0;
00097               device->pos = 0;
00098        }
00099 }
00100 
00101 void
00102 mbfl_memory_device_reset(mbfl_memory_device *device)
00103 {
00104        if (device) {
00105               device->pos = 0;
00106        }
00107 }
00108 
00109 void
00110 mbfl_memory_device_unput(mbfl_memory_device *device)
00111 {
00112        if (device->pos > 0) {
00113               device->pos--;
00114        }
00115 }
00116 
00117 mbfl_string *
00118 mbfl_memory_device_result(mbfl_memory_device *device, mbfl_string *result)
00119 {
00120        if (device && result) {
00121               result->len = device->pos;
00122               mbfl_memory_device_output4('\0', device);
00123               result->val = device->buffer;
00124               device->buffer = (unsigned char *)0;
00125               device->length = 0;
00126               device->pos= 0;
00127               if (result->val == NULL) {
00128                      result->len = 0;
00129                      result = NULL;
00130               }
00131        } else {
00132               result = NULL;
00133        }
00134 
00135        return result;
00136 }
00137 
00138 int
00139 mbfl_memory_device_output(int c, void *data)
00140 {
00141        mbfl_memory_device *device = (mbfl_memory_device *)data;
00142 
00143        if (device->pos >= device->length) {
00144               /* reallocate buffer */
00145               int newlen;
00146               unsigned char *tmp;
00147 
00148               newlen = device->length + device->allocsz;
00149               tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
00150               if (tmp == NULL) {
00151                      return -1;
00152               }
00153               device->length = newlen;
00154               device->buffer = tmp;
00155        }
00156 
00157        device->buffer[device->pos++] = (unsigned char)c;
00158        return c;
00159 }
00160 
00161 int
00162 mbfl_memory_device_output2(int c, void *data)
00163 {
00164        mbfl_memory_device *device = (mbfl_memory_device *)data;
00165 
00166        if ((device->pos + 2) >= device->length) {
00167               /* reallocate buffer */
00168               int newlen;
00169               unsigned char *tmp;
00170 
00171               newlen = device->length + device->allocsz;
00172               tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
00173               if (tmp == NULL) {
00174                      return -1;
00175               }
00176               device->length = newlen;
00177               device->buffer = tmp;
00178        }
00179 
00180        device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
00181        device->buffer[device->pos++] = (unsigned char)(c & 0xff);
00182 
00183        return c;
00184 }
00185 
00186 int
00187 mbfl_memory_device_output4(int c, void* data)
00188 {
00189        mbfl_memory_device *device = (mbfl_memory_device *)data;
00190 
00191        if ((device->pos + 4) >= device->length) {
00192               /* reallocate buffer */
00193               int newlen;
00194               unsigned char *tmp;
00195 
00196               newlen = device->length + device->allocsz;
00197               tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
00198               if (tmp == NULL) {
00199                      return -1;
00200               }
00201               device->length = newlen;
00202               device->buffer = tmp;
00203        }
00204 
00205        device->buffer[device->pos++] = (unsigned char)((c >> 24) & 0xff);
00206        device->buffer[device->pos++] = (unsigned char)((c >> 16) & 0xff);
00207        device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
00208        device->buffer[device->pos++] = (unsigned char)(c & 0xff);
00209 
00210        return c;
00211 }
00212 
00213 int
00214 mbfl_memory_device_strcat(mbfl_memory_device *device, const char *psrc)
00215 {
00216        int len;
00217        unsigned char *w;
00218        const unsigned char *p;
00219 
00220        len = 0;
00221        p = psrc;
00222        while (*p) {
00223               p++;
00224               len++;
00225        }
00226 
00227        if ((device->pos + len) >= device->length) {
00228               /* reallocate buffer */
00229               int newlen = device->length + (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)*sizeof(unsigned char);
00230               unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
00231               if (tmp == NULL) {
00232                      return -1;
00233               }
00234               device->length = newlen;
00235               device->buffer = tmp;
00236        }
00237 
00238        p = psrc;
00239        w = &device->buffer[device->pos];
00240        device->pos += len;
00241        while (len > 0) {
00242               *w++ = *p++;
00243               len--;
00244        }
00245 
00246        return len;
00247 }
00248 
00249 int
00250 mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, int len)
00251 {
00252        unsigned char *w;
00253 
00254        if ((device->pos + len) >= device->length) {
00255               /* reallocate buffer */
00256               int newlen = device->length + len + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
00257               unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
00258               if (tmp == NULL) {
00259                      return -1;
00260               }
00261               device->length = newlen;
00262               device->buffer = tmp;
00263        }
00264 
00265        w = &device->buffer[device->pos];
00266        device->pos += len;
00267        while (len > 0) {
00268               *w++ = *psrc++;
00269               len--;
00270        }
00271 
00272        return len;
00273 }
00274 
00275 int
00276 mbfl_memory_device_devcat(mbfl_memory_device *dest, mbfl_memory_device *src)
00277 {
00278        int n;
00279        unsigned char *p, *w;
00280 
00281        if ((dest->pos + src->pos) >= dest->length) {
00282               /* reallocate buffer */
00283               int newlen = dest->length + src->pos + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
00284               unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)dest->buffer, newlen*sizeof(unsigned char));
00285               if (tmp == NULL) {
00286                      return -1;
00287               }
00288               dest->length = newlen;
00289               dest->buffer = tmp;
00290        }
00291 
00292        p = src->buffer;
00293        w = &dest->buffer[dest->pos];
00294        n = src->pos;
00295        dest->pos += n;
00296        while (n > 0) {
00297               *w++ = *p++;
00298               n--;
00299        }
00300 
00301        return n;
00302 }
00303 
00304 void
00305 mbfl_wchar_device_init(mbfl_wchar_device *device)
00306 {
00307        if (device) {
00308               device->buffer = (unsigned int *)0;
00309               device->length = 0;
00310               device->pos= 0;
00311               device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
00312        }
00313 }
00314 
00315 void
00316 mbfl_wchar_device_clear(mbfl_wchar_device *device)
00317 {
00318        if (device) {
00319               if (device->buffer) {
00320                      mbfl_free(device->buffer);
00321               }
00322               device->buffer = (unsigned int*)0;
00323               device->length = 0;
00324               device->pos = 0;
00325        }
00326 }
00327 
00328 int
00329 mbfl_wchar_device_output(int c, void *data)
00330 {
00331        mbfl_wchar_device *device = (mbfl_wchar_device *)data;
00332 
00333        if (device->pos >= device->length) {
00334               /* reallocate buffer */
00335               int newlen;
00336               unsigned int *tmp;
00337 
00338               newlen = device->length + device->allocsz;
00339               tmp = (unsigned int *)mbfl_realloc((void *)device->buffer, newlen*sizeof(int));
00340               if (tmp == NULL) {
00341                      return -1;
00342               }
00343               device->length = newlen;
00344               device->buffer = tmp;
00345        }
00346 
00347        device->buffer[device->pos++] = c;
00348 
00349        return c;
00350 }