/* * mfile.c -- memory buffered file * (C)Copyright 2000 by TAJIRI Yasuhiro * This file is part of Enfle. * * Last Modified: Thu Aug 10 07:20:25 2000. * $Id: mfile.c,v 1.1 2000/08/09 22:20:34 sian Exp $ * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include #include #include #include "mfile.h" #define min(x,y) ((x) < (y) ? (x) : (y)) #define max(x,y) ((x) > (y) ? (x) : (y)) MFILE* mopen(int init_size) { MFILE* mp = (MFILE*)malloc(sizeof(MFILE)); if(mp==NULL)return NULL; mp->buf=malloc(init_size); if(mp->buf==NULL ){ free(mp); return NULL; } mp->size=init_size; mp->pos=0; return mp; } int mclose(MFILE* stream) { free(stream->buf); free(stream); return 0; } size_t mread(char* buf,size_t size, size_t nmemb, MFILE*stream) { int pos = stream->pos; int count=stream->size; char* b = (stream->buf)+pos; size_t len=size*nmemb; if (pos >= count) { return -1; } if (pos + len > count) { len = count - pos; } if (len <= 0) { return 0; } stream->pos = pos+len; // memmove(buf, b, len); for(pos=0;pospos; int newcount = count + 1; if (newcount > stream->size) { stream->size=max(stream->size << 1, newcount); printf("new size=%d\n",stream->size); stream->buf = realloc(stream->buf,stream->size); if(stream->buf==NULL){ stream->size=0; return -1; } } stream->pos = newcount; return (stream->buf[count] = (unsigned char)c); } int mputcn(int c, MFILE *stream,size_t len) { int count=stream->pos; /* char* b = stream->buf+count; */ int newcount = count + len; if (newcount > stream->size) { stream->size=max(stream->size << 1, newcount); printf("new size=%d\n",stream->size); stream->buf = realloc(stream->buf,stream->size); if(stream->buf==NULL){ stream->size=0; return -1; } } stream->pos = newcount; memset(stream->buf+count, c, len); stream->pos = newcount; return len; } size_t mwrite(char* buf,size_t size, size_t nmemb, MFILE*stream) { int count=stream->pos; char* b = stream->buf+count; size_t len=size*nmemb; int newcount = count + len; if (newcount > stream->size) { stream->size=max(stream->size << 1, newcount); stream->buf = realloc(stream->buf,stream->size); if(stream->buf==NULL){ stream->size=0; return 0; } b = stream->buf+count; } memmove(b, buf, len); stream->pos = newcount; return nmemb; } int mseek( MFILE *stream, long offset, int whence) { if (stream == NULL) return -1; switch (whence) { case SEEK_SET: stream->pos=offset; break; case SEEK_CUR: stream->pos+=offset; break; case SEEK_END: stream->pos=stream->size-offset; break; default: fprintf(stderr, "archive_seek: FATAL: Invalid whence specified: %d\n", whence); } return stream->pos; } long mtell( MFILE *stream) { if (stream == NULL) return -1; return stream->pos; }