Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

umemstrm.c

00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1997-1999, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *
00009 * File UMEMSTRM.C
00010 *
00011 * @author       Vladimir Weinstein
00012 *
00013 * Modification History:
00014 *
00015 *   Date        Name        Description
00016 *   5/17/00      weiv          Created
00017 *******************************************************************************
00018 */
00019 
00020 #include "umemstrm.h"
00021 #include "cmemory.h"
00022 #include "cstring.h" 
00023 #include "unicode/ustring.h" 
00024 
00025 U_CAPI UMemoryStream * U_EXPORT2 uprv_mstrm_openNew(int32_t size) {
00026     UMemoryStream *MS = (UMemoryStream *)uprv_malloc(sizeof(UMemoryStream));
00027     if(MS == NULL) {
00028         return NULL;
00029     }
00030 
00031     MS->fReadOnly = FALSE;
00032     if (size == 0) {
00033         MS->fSize = 0xFFFF;
00034     } else {
00035         MS->fSize = size;
00036     }
00037     MS->fStart = NULL;
00038     MS->fPos = 0;
00039     MS->fReadPos = 0;
00040     MS->fError = FALSE;
00041     MS->fEof = FALSE;
00042     MS->fStart = (uint8_t *)uprv_malloc(MS->fSize);
00043     if(MS->fStart == NULL) {
00044         MS->fError = TRUE;
00045         uprv_free(MS);
00046         return NULL;
00047     }
00048     return MS;
00049 }
00050 
00051 U_CAPI UMemoryStream * U_EXPORT2 uprv_mstrm_openBuffer(const uint8_t *buffer, int32_t len){
00052     UMemoryStream *MS = (UMemoryStream *)uprv_malloc(sizeof(UMemoryStream));
00053     if(MS == NULL) {
00054         return NULL;
00055     }
00056     MS->fReadOnly = TRUE;
00057     MS->fStart = (uint8_t *)buffer; /*functions themselves take care about constness of buffer - see above line*/
00058     MS->fPos = len;
00059     MS->fReadPos = 0;
00060     MS->fError = FALSE;
00061     MS->fEof = FALSE;
00062     return MS;
00063 }
00064 
00065 U_CAPI void U_EXPORT2 uprv_mstrm_close(UMemoryStream *MS){
00066     if(MS->fReadOnly == FALSE && MS->fStart != NULL) {
00067         uprv_free(MS->fStart);
00068     }
00069     uprv_free(MS);
00070 }
00071 
00072 U_CAPI UBool U_EXPORT2 uprv_mstrm_setError(UMemoryStream *MS){
00073     MS->fError = TRUE;
00074     return MS->fError;
00075 }
00076 
00077 U_CAPI UBool U_EXPORT2 uprv_mstrm_error(UMemoryStream *MS){
00078     return MS->fError;
00079 }
00080 
00081 U_CAPI UBool U_EXPORT2 uprv_mstrm_eof(UMemoryStream *MS){
00082     return MS->fEof;
00083 }
00084 
00085 U_CAPI int32_t U_EXPORT2 uprv_mstrm_read(UMemoryStream *MS, void* addr, int32_t len) {
00086     if(MS->fError == FALSE) {
00087         if(len + MS->fReadPos > MS->fPos) {
00088             len = MS->fPos - MS->fReadPos;
00089             MS->fError = TRUE;
00090             MS->fEof = TRUE;
00091         }
00092 
00093         uprv_memcpy(addr, MS->fStart+MS->fReadPos, len);
00094         MS->fReadPos+=len;
00095 
00096         return len;
00097     } else {
00098         return 0;
00099     }
00100 }
00101 
00102 U_CAPI int32_t U_EXPORT2 uprv_mstrm_write(UMemoryStream *MS, const void *buffer, int32_t len){
00103     if(MS->fError == FALSE) {
00104         if(MS->fReadOnly == FALSE) {
00105             if(len + MS->fPos > MS->fSize) {
00106                 uint8_t *newstart = (uint8_t *)uprv_realloc(MS->fStart, MS->fSize+len);
00107                 if(newstart != NULL) {
00108                     MS->fSize+=len;
00109                     MS->fStart = newstart;
00110                 } else {
00111                     MS->fError = TRUE;
00112                     return -1;
00113                 }
00114             }
00115             uprv_memcpy(MS->fStart + MS->fPos, buffer, len);
00116             MS->fPos += len;
00117             return len;
00118         } else {
00119             MS->fError = TRUE;
00120             return 0;
00121         }
00122     } else {
00123         return 0;
00124     }
00125 }
00126 
00127 U_CAPI const uint8_t * U_EXPORT2 uprv_mstrm_getBuffer(UMemoryStream *MS, int32_t *len){
00128     if(MS->fError == FALSE) {
00129         *len = MS->fPos;
00130         return MS->fStart;
00131     } else {
00132         *len = 0;
00133         return NULL;
00134     }
00135 }
00136 
00137 U_CAPI const uint8_t * U_EXPORT2 uprv_mstrm_getCurrentBuffer(UMemoryStream *MS, int32_t *len){
00138     if(MS->fError == FALSE) {
00139         *len = MS->fPos-MS->fReadPos;
00140         return MS->fStart+MS->fReadPos;
00141     } else {
00142         *len = 0;
00143         return NULL;
00144     }
00145 }
00146 
00147 U_CAPI int32_t U_EXPORT2 uprv_mstrm_skip(UMemoryStream *MS, int32_t len){
00148     if(MS->fError == FALSE) {
00149                 MS->fReadPos+=len;
00150     } else {
00151                 return 0;
00152     }
00153         if(MS->fReadPos>MS->fPos) {
00154                 MS->fError = TRUE;
00155                 return 0;
00156         } else {
00157                 return len;
00158         }
00159 }
00160 
00161 U_CAPI int32_t U_EXPORT2 uprv_mstrm_jump(UMemoryStream *MS, const uint8_t *where){
00162     if(MS->fError == FALSE) {
00163                 MS->fReadPos=(where-MS->fStart);
00164     } else {
00165                 return 0;
00166     }
00167         if(MS->fReadPos>MS->fPos) {
00168                 MS->fError = TRUE;
00169                 return 0;
00170         } else {
00171                 return where-MS->fStart;
00172         }
00173 }
00174 
00175 U_CAPI void U_EXPORT2
00176 uprv_mstrm_write8(UMemoryStream *MS, uint8_t byte) {
00177     if(MS!=NULL) {
00178         uprv_mstrm_write(MS, &byte, 1);
00179     }
00180 }
00181 
00182 U_CAPI void U_EXPORT2
00183 uprv_mstrm_write16(UMemoryStream *MS, uint16_t word) {
00184     if(MS!=NULL) {
00185         uprv_mstrm_write(MS, &word, 2);
00186     }
00187 }
00188 
00189 U_CAPI void U_EXPORT2
00190 uprv_mstrm_write32(UMemoryStream *MS, uint32_t wyde) {
00191     if(MS!=NULL) {
00192         uprv_mstrm_write(MS, &wyde, 4);
00193     }
00194 }
00195 
00196 U_CAPI void U_EXPORT2
00197 uprv_mstrm_writeBlock(UMemoryStream *MS, const void *s, UTextOffset length) {
00198     if(MS!=NULL) {
00199         if(length>0) {
00200             uprv_mstrm_write(MS, s, length);
00201         }
00202     }
00203 }
00204 
00205 U_CAPI void U_EXPORT2
00206 uprv_mstrm_writePadding(UMemoryStream *MS, UTextOffset length) {
00207     static uint8_t padding[16]={
00208         0xaa, 0xaa, 0xaa, 0xaa,
00209         0xaa, 0xaa, 0xaa, 0xaa,
00210         0xaa, 0xaa, 0xaa, 0xaa,
00211         0xaa, 0xaa, 0xaa, 0xaa
00212     };
00213     if(MS!=NULL) {
00214         while(length>=16) {
00215             uprv_mstrm_write(MS, padding, 16);
00216             length-=16;
00217         }
00218         if(length>0) {
00219             uprv_mstrm_write(MS, padding, length);
00220         }
00221     }
00222 }
00223 
00224 U_CAPI void U_EXPORT2
00225 uprv_mstrm_writeString(UMemoryStream *MS, const char *s, UTextOffset length) {
00226     if(MS!=NULL) {
00227         if(length==-1) {
00228             length=uprv_strlen(s);
00229         }
00230         if(length>0) {
00231             uprv_mstrm_write(MS, s, length);
00232         }
00233     }
00234 }
00235 
00236 U_CAPI void U_EXPORT2
00237 uprv_mstrm_writeUString(UMemoryStream *MS, const UChar *s, UTextOffset length) {
00238     if(MS!=NULL) {
00239         if(length==-1) {
00240             length=u_strlen(s);
00241         }
00242         if(length>0) {
00243             uprv_mstrm_write(MS, s, length*sizeof(UChar));
00244         }
00245     }
00246 }
00247 
00248 /*
00249  * Hey, Emacs, please set the following:
00250  *
00251  * Local Variables:
00252  * indent-tabs-mode: nil
00253  * End:
00254  *
00255  */

Generated at Tue Dec 5 10:48:06 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000