diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | d600a6b5b358c28d482b01f10bfa3292b17f5d12 (patch) | |
tree | 8f9047a23315351b32571092cade0883ace6f60e /zio.c | |
parent | 75ac0d217266dba48a887df96d37398140e22b9e (diff) | |
download | lua-d600a6b5b358c28d482b01f10bfa3292b17f5d12.tar.gz lua-d600a6b5b358c28d482b01f10bfa3292b17f5d12.tar.bz2 lua-d600a6b5b358c28d482b01f10bfa3292b17f5d12.zip |
a generic input stream interface
Diffstat (limited to 'zio.c')
-rw-r--r-- | zio.c | 79 |
1 files changed, 0 insertions, 79 deletions
@@ -1,79 +0,0 @@ | |||
1 | /* | ||
2 | * zio.c | ||
3 | * a generic input stream interface | ||
4 | * $Id: zio.c,v 1.1 1997/06/16 16:50:22 roberto Exp roberto $ | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include "zio.h" | ||
11 | |||
12 | |||
13 | /* ----------------------------------------------------- memory buffers --- */ | ||
14 | |||
15 | static int zmfilbuf(ZIO* z) | ||
16 | { | ||
17 | return EOZ; | ||
18 | } | ||
19 | |||
20 | ZIO* zmopen(ZIO* z, char* b, int size) | ||
21 | { | ||
22 | if (b==NULL) return NULL; | ||
23 | z->n=size; | ||
24 | z->p= (unsigned char *)b; | ||
25 | z->filbuf=zmfilbuf; | ||
26 | z->u=NULL; | ||
27 | return z; | ||
28 | } | ||
29 | |||
30 | /* ------------------------------------------------------------ strings --- */ | ||
31 | |||
32 | ZIO* zsopen(ZIO* z, char* s) | ||
33 | { | ||
34 | if (s==NULL) return NULL; | ||
35 | return zmopen(z,s,strlen(s)); | ||
36 | } | ||
37 | |||
38 | /* -------------------------------------------------------------- FILEs --- */ | ||
39 | |||
40 | static int zffilbuf(ZIO* z) | ||
41 | { | ||
42 | int n=fread(z->buffer,1,ZBSIZE,z->u); | ||
43 | if (n==0) return EOZ; | ||
44 | z->n=n-1; | ||
45 | z->p=z->buffer; | ||
46 | return *(z->p++); | ||
47 | } | ||
48 | |||
49 | |||
50 | ZIO* zFopen(ZIO* z, FILE* f) | ||
51 | { | ||
52 | if (f==NULL) return NULL; | ||
53 | z->n=0; | ||
54 | z->p=z->buffer; | ||
55 | z->filbuf=zffilbuf; | ||
56 | z->u=f; | ||
57 | return z; | ||
58 | } | ||
59 | |||
60 | |||
61 | /* --------------------------------------------------------------- read --- */ | ||
62 | int zread(ZIO *z, void *b, int n) | ||
63 | { | ||
64 | while (n) { | ||
65 | int m; | ||
66 | if (z->n == 0) { | ||
67 | if (z->filbuf(z) == EOZ) | ||
68 | return n; /* retorna quantos faltaram ler */ | ||
69 | zungetc(z); /* poe o resultado de filbuf no buffer */ | ||
70 | } | ||
71 | m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */ | ||
72 | memcpy(b, z->p, m); | ||
73 | z->n -= m; | ||
74 | z->p += m; | ||
75 | b = (char *)b + m; | ||
76 | n -= m; | ||
77 | } | ||
78 | return 0; | ||
79 | } | ||