scuffed-code/icu4c/source/common/umemstrm.c
Vladimir Weinstein 1583efe468 ICU-406 putting collation in resource bundles
X-SVN-Rev: 1463
2000-05-24 19:58:40 +00:00

134 lines
3.4 KiB
C

/*
*******************************************************************************
*
* Copyright (C) 1997-1999, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
*
* File UMEMSTRM.C
*
* @author Vladimir Weinstein
*
* Modification History:
*
* Date Name Description
* 5/17/00 weiv Created
*******************************************************************************
*/
#include "umemstrm.h"
#include "cmemory.h"
U_CAPI UMemoryStream * U_EXPORT2 uprv_mstrm_openNew(int32_t size) {
UMemoryStream *MS = (UMemoryStream *)uprv_malloc(sizeof(UMemoryStream));
if(MS == NULL) {
return NULL;
}
MS->fReadOnly = FALSE;
if (size == 0) {
MS->fSize = 0xFFFF;
} else {
MS->fSize = size;
}
MS->fStart = NULL;
MS->fPos = 0;
MS->fReadPos = 0;
MS->fError = FALSE;
MS->fEof = FALSE;
MS->fStart = (uint8_t *)uprv_malloc(MS->fSize);
if(MS->fStart == NULL) {
MS->fError = TRUE;
uprv_free(MS);
return NULL;
}
return MS;
}
U_CAPI UMemoryStream * U_EXPORT2 uprv_mstrm_openBuffer(const uint8_t *buffer, int32_t len){
UMemoryStream *MS = (UMemoryStream *)uprv_malloc(sizeof(UMemoryStream));
if(MS == NULL) {
return NULL;
}
MS->fReadOnly = TRUE;
MS->fStart = (uint8_t *)buffer; /*functions themselves take care about constness of buffer - see above line*/
MS->fPos = len;
MS->fReadPos = 0;
MS->fError = FALSE;
MS->fEof = FALSE;
return MS;
}
U_CAPI void U_EXPORT2 uprv_mstrm_close(UMemoryStream *MS){
if(MS->fReadOnly == FALSE && MS->fStart != NULL) {
uprv_free(MS->fStart);
}
uprv_free(MS);
}
U_CAPI UBool U_EXPORT2 uprv_mstrm_setError(UMemoryStream *MS){
MS->fError = TRUE;
return MS->fError;
}
U_CAPI UBool U_EXPORT2 uprv_mstrm_error(UMemoryStream *MS){
return MS->fError;
}
U_CAPI UBool U_EXPORT2 uprv_mstrm_eof(UMemoryStream *MS){
return MS->fEof;
}
U_CAPI int32_t U_EXPORT2 uprv_mstrm_read(UMemoryStream *MS, void* addr, int32_t len) {
if(MS->fError == FALSE) {
if(len + MS->fReadPos > MS->fPos) {
len = MS->fPos - MS->fReadPos;
MS->fError = TRUE;
MS->fEof = TRUE;
}
uprv_memcpy(addr, MS->fStart+MS->fReadPos, len);
MS->fReadPos+=len;
return len;
} else {
return 0;
}
}
U_CAPI int32_t U_EXPORT2 uprv_mstrm_write(UMemoryStream *MS, const uint8_t *buffer, int32_t len){
if(MS->fError == FALSE) {
if(MS->fReadOnly == FALSE) {
if(len + MS->fPos > MS->fSize) {
uint8_t *newstart = (uint8_t *)uprv_realloc(MS->fStart, MS->fSize+len);
if(newstart != NULL) {
MS->fSize+=len;
MS->fStart = newstart;
} else {
MS->fError = TRUE;
return -1;
}
}
uprv_memcpy(MS->fStart + MS->fPos, buffer, len);
MS->fPos += len;
return len;
} else {
MS->fError = TRUE;
return 0;
}
} else {
return 0;
}
}
U_CAPI uint8_t * U_EXPORT2 uprv_mstrm_getBuffer(UMemoryStream *MS, int32_t *len){
if(MS->fError == FALSE) {
*len = MS->fPos;
return MS->fStart;
} else {
*len = 0;
return NULL;
}
}