aboutsummaryrefslogtreecommitdiff
path: root/lzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'lzio.c')
-rw-r--r--lzio.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/lzio.c b/lzio.c
new file mode 100644
index 00000000..bd714ea1
--- /dev/null
+++ b/lzio.c
@@ -0,0 +1,82 @@
1/*
2** $Id: zio.c,v 1.2 1997/06/20 19:25:54 roberto Exp $
3** a generic input stream interface
4** See Copyright Notice in lua.h
5*/
6
7
8
9#include <stdio.h>
10#include <string.h>
11
12#include "lzio.h"
13
14
15
16/* ----------------------------------------------------- memory buffers --- */
17
18static int zmfilbuf(ZIO* z)
19{
20 return EOZ;
21}
22
23ZIO* zmopen(ZIO* z, char* b, int size)
24{
25 if (b==NULL) return NULL;
26 z->n=size;
27 z->p= (unsigned char *)b;
28 z->filbuf=zmfilbuf;
29 z->u=NULL;
30 return z;
31}
32
33/* ------------------------------------------------------------ strings --- */
34
35ZIO* zsopen(ZIO* z, char* s)
36{
37 if (s==NULL) return NULL;
38 return zmopen(z,s,strlen(s));
39}
40
41/* -------------------------------------------------------------- FILEs --- */
42
43static int zffilbuf(ZIO* z)
44{
45 int n=fread(z->buffer,1,ZBSIZE,z->u);
46 if (n==0) return EOZ;
47 z->n=n-1;
48 z->p=z->buffer;
49 return *(z->p++);
50}
51
52
53ZIO* zFopen(ZIO* z, FILE* f)
54{
55 if (f==NULL) return NULL;
56 z->n=0;
57 z->p=z->buffer;
58 z->filbuf=zffilbuf;
59 z->u=f;
60 return z;
61}
62
63
64/* --------------------------------------------------------------- read --- */
65int zread(ZIO *z, void *b, int n)
66{
67 while (n) {
68 int m;
69 if (z->n == 0) {
70 if (z->filbuf(z) == EOZ)
71 return n; /* retorna quantos faltaram ler */
72 zungetc(z); /* poe o resultado de filbuf no buffer */
73 }
74 m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
75 memcpy(b, z->p, m);
76 z->n -= m;
77 z->p += m;
78 b = (char *)b + m;
79 n -= m;
80 }
81 return 0;
82}