/* All code by Tom Fjellstrom except the mem_[im](ge|pu)t[wl] functions by Shawm Hargreaves (shawn@talula.demon.co.uk) */ #include #include #include typedef struct mfile { int len; int pos; void *data; } mfile; mfile *mem_fopen(int len, void *f) { mfile *new = NULL; if(!f) return NULL; new = calloc(1,sizeof(mfile)); if(!new) return NULL; new->len = len; new->data = f; new->pos = 0; return new; } int mem_freopen(int len, void *d, mfile *f) { if(!f || !d) return 0; f->len = len; f->pos = 0; f->data = d; return 1; } void mem_fclose(mfile *f) { // why wasn't this in the code ??? Added by MNI if (f->data) free(f->data); free(f); f=NULL; } int mem_fread(void *buf, int size, mfile *f) { int i; char *t = NULL,*t2 = NULL; if(!buf || !f) return 0; t = buf; t2 = f->data; for(i=0; i!=size; i++) { if(f->pos>=f->len || i>=f->len) return (sizepos); f->pos++; } return (sizedata; for(i=0; i!=size; i++) { if(f->pos>=f->len || i>=f->len) return (sizepos) = t[i]; f->pos++; } return (sizepos>=f->len) return EOF; t = f->data; tmp = *(t+f->pos); f->pos++; return tmp; } char mem_putc(char c, mfile *f) { char *t; if(f->pos>=f->len) return EOF; t = f->data; *(t+f->pos) = c; f->pos++; return c; } int mem_igetw(mfile *f) { int b1, b2; if ((b1 = mem_getc(f)) != EOF) if ((b2 = mem_getc(f)) != EOF) return ((b2 << 8) | b1); return EOF; } long mem_igetl(mfile *f) { int b1, b2, b3, b4; if ((b1 = mem_getc(f)) != EOF) if ((b2 = mem_getc(f)) != EOF) if ((b3 = mem_getc(f)) != EOF) if ((b4 = mem_getc(f)) != EOF) return (((long)b4 << 24) | ((long)b3 << 16) | ((long)b2 << 8) | (long)b1); return EOF; } int mem_mgetw(mfile *f) { int b1, b2; if ((b1 = mem_getc(f)) != EOF) if ((b2 = mem_getc(f)) != EOF) return ((b1 << 8) | b2); return EOF; } long mem_mgetl(mfile *f) { int b1, b2, b3, b4; if ((b1 = mem_getc(f)) != EOF) if ((b2 = mem_getc(f)) != EOF) if ((b3 = mem_getc(f)) != EOF) if ((b4 = mem_getc(f)) != EOF) return (((long)b1 << 24) | ((long)b2 << 16) | ((long)b3 << 8) | (long)b4); return EOF; } int mem_iputw(int w, mfile *f) { int b1, b2; b1 = (w & 0xFF00) >> 8; b2 = w & 0x00FF; if (mem_putc(b2,f)==b2) if (mem_putc(b1,f)==b1) return w; return EOF; } long mem_iputl(long l, mfile *f) { int b1, b2, b3, b4; b1 = (int)((l & 0xFF000000L) >> 24); b2 = (int)((l & 0x00FF0000L) >> 16); b3 = (int)((l & 0x0000FF00L) >> 8); b4 = (int)l & 0x00FF; if (mem_putc(b4,f)==b4) if (mem_putc(b3,f)==b3) if (mem_putc(b2,f)==b2) if (mem_putc(b1,f)==b1) return l; return EOF; } int mem_mputw(int w, mfile *f) { int b1, b2; b1 = (w & 0xFF00) >> 8; b2 = w & 0x00FF; if (mem_putc(b1,f)==b1) if (mem_putc(b2,f)==b2) return w; return EOF; } long mem_mputl(long l, mfile *f) { int b1, b2, b3, b4; b1 = (int)((l & 0xFF000000L) >> 24); b2 = (int)((l & 0x00FF0000L) >> 16); b3 = (int)((l & 0x0000FF00L) >> 8); b4 = (int)l & 0x00FF; if (mem_putc(b1,f)==b1) if (mem_putc(b2,f)==b2) if (mem_putc(b3,f)==b3) if (mem_putc(b4,f)==b4) return l; return EOF; } int mem_fseek(mfile *f, int off, int mode) { if(!f) return EOF; if(mode==SEEK_SET) { if(off>f->len) off=f->len; f->pos = abs(off); } else if(mode==SEEK_CUR) { if(f->pos+off>f->len) { f->pos=f->len; return EOF; } else if(f->pos+off<0) { f->pos = 0; return -1; } f->pos += off; } else if(mode==SEEK_END) { int os = -abs(off); f->pos = f->len+os; } return 0; } int mem_ftell(mfile *f) { if(f) return f->pos; return -1; } int mem_feof(mfile *f) { if(f->poslen) return 0; return 1; } #ifdef STANDALONE char data[255] = "Hello How Are you?"; int main() { char t[255],tmp; int j; mfile *f = NULL; f = mem_fopen(data,18); if(!f) { printf("couldn't open mfile\n"); return 0; } while(!mem_feof(f)) { printf("%c\n",mem_getc(f)); } mem_fclose(f); return 0; } #endif /* #ifdef STANDALONE */