diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-16 13:50:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-16 13:50:22 -0300 |
commit | 9fe5be3acf340b90fe0c24d36a601adaf6f21c79 (patch) | |
tree | afab102dd8cb6f7c2ad0b403a4006fe75a2ea7fa | |
parent | 611680af08b6102bd260068d9c9ad6961029bd5d (diff) | |
download | lua-9fe5be3acf340b90fe0c24d36a601adaf6f21c79.tar.gz lua-9fe5be3acf340b90fe0c24d36a601adaf6f21c79.tar.bz2 lua-9fe5be3acf340b90fe0c24d36a601adaf6f21c79.zip |
library for uniform buffered input.
-rw-r--r-- | zio.c | 117 | ||||
-rw-r--r-- | zio.h | 47 |
2 files changed, 164 insertions, 0 deletions
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | * zio.c | ||
3 | * a generic input stream interface | ||
4 | * $Id: zio.c,v 1.5 1997/06/13 13:49:16 lhf Exp $ | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include "zio.h" | ||
11 | |||
12 | #ifdef POPEN | ||
13 | FILE *popen(); | ||
14 | int pclose(); | ||
15 | #else | ||
16 | #define popen(x,y) NULL /* that is, popen always fails */ | ||
17 | #define pclose(x) (-1) | ||
18 | #endif | ||
19 | |||
20 | /* ----------------------------------------------------- memory buffers --- */ | ||
21 | |||
22 | static int zmfilbuf(ZIO* z) | ||
23 | { | ||
24 | return EOZ; | ||
25 | } | ||
26 | |||
27 | static int zmclose(ZIO* z) | ||
28 | { | ||
29 | return 1; | ||
30 | } | ||
31 | |||
32 | ZIO* zmopen(ZIO* z, char* b, int size) | ||
33 | { | ||
34 | if (b==NULL) return NULL; | ||
35 | z->n=size; | ||
36 | z->p= (unsigned char *)b; | ||
37 | z->filbuf=zmfilbuf; | ||
38 | z->close=zmclose; | ||
39 | z->u=NULL; | ||
40 | return z; | ||
41 | } | ||
42 | |||
43 | /* ------------------------------------------------------------ strings --- */ | ||
44 | |||
45 | ZIO* zsopen(ZIO* z, char* s) | ||
46 | { | ||
47 | if (s==NULL) return NULL; | ||
48 | return zmopen(z,s,strlen(s)); | ||
49 | } | ||
50 | |||
51 | /* -------------------------------------------------------------- FILEs --- */ | ||
52 | |||
53 | static int zffilbuf(ZIO* z) | ||
54 | { | ||
55 | int n=fread(z->buffer,1,ZBSIZE,z->u); | ||
56 | if (n==0) return EOZ; | ||
57 | z->n=n-1; | ||
58 | z->p=z->buffer; | ||
59 | return *(z->p++); | ||
60 | } | ||
61 | |||
62 | static int zfclose(ZIO* z) | ||
63 | { | ||
64 | if (z->u==stdin) return 0; | ||
65 | return fclose(z->u); | ||
66 | } | ||
67 | |||
68 | ZIO* zFopen(ZIO* z, FILE* f) | ||
69 | { | ||
70 | if (f==NULL) return NULL; | ||
71 | z->n=0; | ||
72 | z->p=z->buffer; | ||
73 | z->filbuf=zffilbuf; | ||
74 | z->close=zfclose; | ||
75 | z->u=f; | ||
76 | return z; | ||
77 | } | ||
78 | |||
79 | ZIO* zfopen(ZIO* z, char* s, char* m) | ||
80 | { | ||
81 | return zFopen(z,fopen(s,m)); | ||
82 | } | ||
83 | |||
84 | /* -------------------------------------------------------------- pipes --- */ | ||
85 | |||
86 | static int zpclose(ZIO* z) | ||
87 | { | ||
88 | return pclose(z->u); | ||
89 | } | ||
90 | |||
91 | ZIO* zpopen(ZIO* z, char* s, char* m) | ||
92 | { | ||
93 | z=zFopen(z,popen(s,m)); | ||
94 | if (z==NULL) return NULL; | ||
95 | z->close=zpclose; | ||
96 | return z; | ||
97 | } | ||
98 | |||
99 | /* --------------------------------------------------------------- read --- */ | ||
100 | int zread(ZIO *z, void *b, int n) | ||
101 | { | ||
102 | while (n) { | ||
103 | int m; | ||
104 | if (z->n == 0) { | ||
105 | if (z->filbuf(z) == EOZ) | ||
106 | return n; /* retorna quantos faltaram ler */ | ||
107 | zungetc(z); /* poe o resultado de filbuf no buffer */ | ||
108 | } | ||
109 | m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */ | ||
110 | memcpy(b, z->p, m); | ||
111 | z->n -= m; | ||
112 | z->p += m; | ||
113 | b = (char *)b + m; | ||
114 | n -= m; | ||
115 | } | ||
116 | return 0; | ||
117 | } | ||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * zio.h | ||
3 | * a generic input stream interface | ||
4 | * $Id: zio.h,v 1.4 1997/06/13 13:49:16 lhf Exp $ | ||
5 | */ | ||
6 | |||
7 | #ifndef zio_h | ||
8 | #define zio_h | ||
9 | |||
10 | #include <stdio.h> | ||
11 | |||
12 | #define EOZ (-1) /* end of stream */ | ||
13 | |||
14 | typedef struct zio ZIO; | ||
15 | |||
16 | int zgetc(ZIO* z); /* get next byte */ | ||
17 | int zungetc(ZIO* z); /* put back last byte read */ | ||
18 | int zread(ZIO* z, void* b, int n); /* read next n bytes */ | ||
19 | int zclose(ZIO* z); /* close stream */ | ||
20 | |||
21 | ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */ | ||
22 | ZIO* zfopen(ZIO* z, char* s, char* m); /* file by name */ | ||
23 | ZIO* zpopen(ZIO* z, char* s, char* m); /* pipe */ | ||
24 | ZIO* zsopen(ZIO* z, char* s); /* string */ | ||
25 | ZIO* zmopen(ZIO* z, char* b, int size); /* memory */ | ||
26 | |||
27 | #define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z)) | ||
28 | #define zungetc(z) (++(z)->n,--(z)->p) | ||
29 | #define zclose(z) (*(z)->close)(z) | ||
30 | |||
31 | |||
32 | |||
33 | /* --------- Private Part ------------------ */ | ||
34 | |||
35 | #define ZBSIZE 256 /* buffer size */ | ||
36 | |||
37 | struct zio { | ||
38 | int n; /* bytes still unread */ | ||
39 | unsigned char* p; /* current position in buffer */ | ||
40 | int (*filbuf)(ZIO* z); | ||
41 | int (*close)(ZIO* z); | ||
42 | void* u; /* additional data */ | ||
43 | unsigned char buffer[ZBSIZE]; /* buffer */ | ||
44 | }; | ||
45 | |||
46 | |||
47 | #endif | ||