zio should not care about how a user creates a FILE (pipe, socket, popen,
etc).
This commit is contained in:
40
zio.c
40
zio.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* zio.c
|
||||
* a generic input stream interface
|
||||
* $Id: zio.c,v 1.5 1997/06/13 13:49:16 lhf Exp $
|
||||
* $Id: zio.c,v 1.1 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -9,13 +9,6 @@
|
||||
#include <string.h>
|
||||
#include "zio.h"
|
||||
|
||||
#ifdef POPEN
|
||||
FILE *popen();
|
||||
int pclose();
|
||||
#else
|
||||
#define popen(x,y) NULL /* that is, popen always fails */
|
||||
#define pclose(x) (-1)
|
||||
#endif
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
@@ -24,18 +17,12 @@ static int zmfilbuf(ZIO* z)
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
static int zmclose(ZIO* z)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
ZIO* zmopen(ZIO* z, char* b, int size)
|
||||
{
|
||||
if (b==NULL) return NULL;
|
||||
z->n=size;
|
||||
z->p= (unsigned char *)b;
|
||||
z->filbuf=zmfilbuf;
|
||||
z->close=zmclose;
|
||||
z->u=NULL;
|
||||
return z;
|
||||
}
|
||||
@@ -59,11 +46,6 @@ static int zffilbuf(ZIO* z)
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
static int zfclose(ZIO* z)
|
||||
{
|
||||
if (z->u==stdin) return 0;
|
||||
return fclose(z->u);
|
||||
}
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f)
|
||||
{
|
||||
@@ -71,30 +53,10 @@ ZIO* zFopen(ZIO* z, FILE* f)
|
||||
z->n=0;
|
||||
z->p=z->buffer;
|
||||
z->filbuf=zffilbuf;
|
||||
z->close=zfclose;
|
||||
z->u=f;
|
||||
return z;
|
||||
}
|
||||
|
||||
ZIO* zfopen(ZIO* z, char* s, char* m)
|
||||
{
|
||||
return zFopen(z,fopen(s,m));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- pipes --- */
|
||||
|
||||
static int zpclose(ZIO* z)
|
||||
{
|
||||
return pclose(z->u);
|
||||
}
|
||||
|
||||
ZIO* zpopen(ZIO* z, char* s, char* m)
|
||||
{
|
||||
z=zFopen(z,popen(s,m));
|
||||
if (z==NULL) return NULL;
|
||||
z->close=zpclose;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread(ZIO *z, void *b, int n)
|
||||
|
||||
8
zio.h
8
zio.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* zio.h
|
||||
* a generic input stream interface
|
||||
* $Id: zio.h,v 1.3 1997/06/18 21:39:56 roberto Exp roberto $
|
||||
* $Id: zio.h,v 1.4 1997/06/19 18:55:28 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef zio_h
|
||||
@@ -13,8 +13,6 @@
|
||||
|
||||
/* For Lua only */
|
||||
#define zFopen luaZ_Fopen
|
||||
#define zfopen luaZ_fopen
|
||||
#define zpopen luaZ_popen
|
||||
#define zsopen luaZ_sopen
|
||||
#define zmopen luaZ_mopen
|
||||
#define zread luaZ_read
|
||||
@@ -24,8 +22,6 @@
|
||||
typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
|
||||
ZIO* zfopen(ZIO* z, char* s, char* m); /* file by name */
|
||||
ZIO* zpopen(ZIO* z, char* s, char* m); /* pipe */
|
||||
ZIO* zsopen(ZIO* z, char* s); /* string */
|
||||
ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
|
||||
|
||||
@@ -33,7 +29,6 @@ int zread(ZIO* z, void* b, int n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define zclose(z) (*(z)->close)(z)
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +40,6 @@ struct zio {
|
||||
int n; /* bytes still unread */
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
int (*close)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user