aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-20 16:25:54 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-20 16:25:54 -0300
commitf97307b54893a53c985a0dcd8b3bb2b94cedd48d (patch)
treeaa2aa6c7631064f0507747b72f7fba747ccd7026
parent6402bfb1f84391cab2c55bad45fec0f82da3c359 (diff)
downloadlua-f97307b54893a53c985a0dcd8b3bb2b94cedd48d.tar.gz
lua-f97307b54893a53c985a0dcd8b3bb2b94cedd48d.tar.bz2
lua-f97307b54893a53c985a0dcd8b3bb2b94cedd48d.zip
zio should not care about how a user creates a FILE (pipe, socket, popen,
etc).
-rw-r--r--zio.c40
-rw-r--r--zio.h8
2 files changed, 2 insertions, 46 deletions
diff --git a/zio.c b/zio.c
index aa400f7d..c0b25daa 100644
--- a/zio.c
+++ b/zio.c
@@ -1,7 +1,7 @@
1/* 1/*
2* zio.c 2* zio.c
3* a generic input stream interface 3* a generic input stream interface
4* $Id: zio.c,v 1.5 1997/06/13 13:49:16 lhf Exp $ 4* $Id: zio.c,v 1.1 1997/06/16 16:50:22 roberto Exp roberto $
5*/ 5*/
6 6
7#include <stdio.h> 7#include <stdio.h>
@@ -9,13 +9,6 @@
9#include <string.h> 9#include <string.h>
10#include "zio.h" 10#include "zio.h"
11 11
12#ifdef POPEN
13FILE *popen();
14int pclose();
15#else
16#define popen(x,y) NULL /* that is, popen always fails */
17#define pclose(x) (-1)
18#endif
19 12
20/* ----------------------------------------------------- memory buffers --- */ 13/* ----------------------------------------------------- memory buffers --- */
21 14
@@ -24,18 +17,12 @@ static int zmfilbuf(ZIO* z)
24 return EOZ; 17 return EOZ;
25} 18}
26 19
27static int zmclose(ZIO* z)
28{
29 return 1;
30}
31
32ZIO* zmopen(ZIO* z, char* b, int size) 20ZIO* zmopen(ZIO* z, char* b, int size)
33{ 21{
34 if (b==NULL) return NULL; 22 if (b==NULL) return NULL;
35 z->n=size; 23 z->n=size;
36 z->p= (unsigned char *)b; 24 z->p= (unsigned char *)b;
37 z->filbuf=zmfilbuf; 25 z->filbuf=zmfilbuf;
38 z->close=zmclose;
39 z->u=NULL; 26 z->u=NULL;
40 return z; 27 return z;
41} 28}
@@ -59,11 +46,6 @@ static int zffilbuf(ZIO* z)
59 return *(z->p++); 46 return *(z->p++);
60} 47}
61 48
62static int zfclose(ZIO* z)
63{
64 if (z->u==stdin) return 0;
65 return fclose(z->u);
66}
67 49
68ZIO* zFopen(ZIO* z, FILE* f) 50ZIO* zFopen(ZIO* z, FILE* f)
69{ 51{
@@ -71,30 +53,10 @@ ZIO* zFopen(ZIO* z, FILE* f)
71 z->n=0; 53 z->n=0;
72 z->p=z->buffer; 54 z->p=z->buffer;
73 z->filbuf=zffilbuf; 55 z->filbuf=zffilbuf;
74 z->close=zfclose;
75 z->u=f; 56 z->u=f;
76 return z; 57 return z;
77} 58}
78 59
79ZIO* zfopen(ZIO* z, char* s, char* m)
80{
81 return zFopen(z,fopen(s,m));
82}
83
84/* -------------------------------------------------------------- pipes --- */
85
86static int zpclose(ZIO* z)
87{
88 return pclose(z->u);
89}
90
91ZIO* 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 60
99/* --------------------------------------------------------------- read --- */ 61/* --------------------------------------------------------------- read --- */
100int zread(ZIO *z, void *b, int n) 62int zread(ZIO *z, void *b, int n)
diff --git a/zio.h b/zio.h
index 50e8f805..c688df55 100644
--- a/zio.h
+++ b/zio.h
@@ -1,7 +1,7 @@
1/* 1/*
2* zio.h 2* zio.h
3* a generic input stream interface 3* a generic input stream interface
4* $Id: zio.h,v 1.3 1997/06/18 21:39:56 roberto Exp roberto $ 4* $Id: zio.h,v 1.4 1997/06/19 18:55:28 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef zio_h 7#ifndef zio_h
@@ -13,8 +13,6 @@
13 13
14/* For Lua only */ 14/* For Lua only */
15#define zFopen luaZ_Fopen 15#define zFopen luaZ_Fopen
16#define zfopen luaZ_fopen
17#define zpopen luaZ_popen
18#define zsopen luaZ_sopen 16#define zsopen luaZ_sopen
19#define zmopen luaZ_mopen 17#define zmopen luaZ_mopen
20#define zread luaZ_read 18#define zread luaZ_read
@@ -24,8 +22,6 @@
24typedef struct zio ZIO; 22typedef struct zio ZIO;
25 23
26ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */ 24ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
27ZIO* zfopen(ZIO* z, char* s, char* m); /* file by name */
28ZIO* zpopen(ZIO* z, char* s, char* m); /* pipe */
29ZIO* zsopen(ZIO* z, char* s); /* string */ 25ZIO* zsopen(ZIO* z, char* s); /* string */
30ZIO* zmopen(ZIO* z, char* b, int size); /* memory */ 26ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
31 27
@@ -33,7 +29,6 @@ int zread(ZIO* z, void* b, int n); /* read next n bytes */
33 29
34#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z)) 30#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
35#define zungetc(z) (++(z)->n,--(z)->p) 31#define zungetc(z) (++(z)->n,--(z)->p)
36#define zclose(z) (*(z)->close)(z)
37 32
38 33
39 34
@@ -45,7 +40,6 @@ struct zio {
45 int n; /* bytes still unread */ 40 int n; /* bytes still unread */
46 unsigned char* p; /* current position in buffer */ 41 unsigned char* p; /* current position in buffer */
47 int (*filbuf)(ZIO* z); 42 int (*filbuf)(ZIO* z);
48 int (*close)(ZIO* z);
49 void* u; /* additional data */ 43 void* u; /* additional data */
50 unsigned char buffer[ZBSIZE]; /* buffer */ 44 unsigned char buffer[ZBSIZE]; /* buffer */
51}; 45};