1997-06-16 20:50:22 +04:00
|
|
|
/*
|
1999-08-17 00:52:00 +04:00
|
|
|
** $Id: lzio.c,v 1.7 1999/03/05 13:15:50 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** a generic input stream interface
|
|
|
|
** See Copyright Notice in lua.h
|
1997-06-16 20:50:22 +04:00
|
|
|
*/
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
#include "lzio.h"
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------- memory buffers --- */
|
|
|
|
|
1998-12-28 16:44:54 +03:00
|
|
|
static int zmfilbuf (ZIO* z) {
|
1999-08-17 00:52:00 +04:00
|
|
|
return EOZ;
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
|
|
|
|
1998-12-28 16:44:54 +03:00
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
ZIO* zmopen (ZIO* z, const char* b, int size, const char *name) {
|
|
|
|
if (b==NULL) return NULL;
|
|
|
|
z->n = size;
|
|
|
|
z->p = (unsigned const char *)b;
|
|
|
|
z->filbuf = zmfilbuf;
|
|
|
|
z->u = NULL;
|
|
|
|
z->name = name;
|
|
|
|
return z;
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------ strings --- */
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
ZIO* zsopen (ZIO* z, const char* s, const char *name) {
|
|
|
|
if (s==NULL) return NULL;
|
|
|
|
return zmopen(z,s,strlen(s),name);
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------- FILEs --- */
|
|
|
|
|
1999-03-04 17:50:26 +03:00
|
|
|
static int zffilbuf (ZIO* z) {
|
1999-08-17 00:52:00 +04:00
|
|
|
int n;
|
|
|
|
if (feof((FILE *)z->u)) return EOZ;
|
|
|
|
n = fread(z->buffer,1,ZBSIZE,z->u);
|
|
|
|
if (n==0) return EOZ;
|
|
|
|
z->n = n-1;
|
|
|
|
z->p = z->buffer;
|
|
|
|
return *(z->p++);
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 00:52:00 +04:00
|
|
|
ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
|
|
|
if (f==NULL) return NULL;
|
|
|
|
z->n = 0;
|
|
|
|
z->p = z->buffer;
|
|
|
|
z->filbuf = zffilbuf;
|
|
|
|
z->u = f;
|
|
|
|
z->name = name;
|
|
|
|
return z;
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------- read --- */
|
1999-02-26 00:07:26 +03:00
|
|
|
int zread (ZIO *z, void *b, int n) {
|
1997-06-16 20:50:22 +04:00
|
|
|
while (n) {
|
|
|
|
int m;
|
|
|
|
if (z->n == 0) {
|
|
|
|
if (z->filbuf(z) == EOZ)
|
1999-02-26 00:07:26 +03:00
|
|
|
return n; /* return number of missing bytes */
|
|
|
|
zungetc(z); /* put result from 'filbuf' in the buffer */
|
1997-06-16 20:50:22 +04:00
|
|
|
}
|
1999-02-26 00:07:26 +03:00
|
|
|
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
1997-06-16 20:50:22 +04:00
|
|
|
memcpy(b, z->p, m);
|
|
|
|
z->n -= m;
|
|
|
|
z->p += m;
|
|
|
|
b = (char *)b + m;
|
|
|
|
n -= m;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|