00001 #ifndef IO_BUF__H__
00002 #define IO_BUF__H__
00003
00004 #define LF ((char)10)
00005 #define CR ((char)13)
00006 #define CRLF "\015\012"
00007
00008 struct str;
00009
00023 #define IOBUF_EOF 1
00024
00025 #define IOBUF_ERROR 2
00026
00027 #define IOBUF_TIMEOUT 4
00028
00029 #define IOBUF_BADFLAGS 0xf
00030
00031 #define IOBUF_SEEKABLE 0x10
00032
00033 #define IOBUF_NEEDSCLOSE 0x20
00034
00035 #define IOBUF_NEEDSFREE 0x40
00036
00037 #define IOBUF_NEEDSMUNMAP 0x80
00038 extern unsigned iobuf_bufsize;
00039
00040
00041
00046 struct iobuf
00047 {
00049 int fd;
00051 char* buffer;
00053 unsigned bufsize;
00055 unsigned buflen;
00057 unsigned bufstart;
00059 unsigned offset;
00061 unsigned timeout;
00063 unsigned flags;
00065 int errnum;
00066 };
00067 typedef struct iobuf iobuf;
00068
00070 #define IOBUF_SET_ERROR(io) \
00071 do{ \
00072 io->flags |= IOBUF_ERROR; \
00073 io->errnum = errno; \
00074 return 0; \
00075 }while(0)
00076
00077 int iobuf_init(iobuf* io, int fd, unsigned bufsize, char* buffer,
00078 unsigned flags);
00079 int iobuf_close(iobuf* io);
00081 #define iobuf_closed(io) ((io)->fd == -1)
00082
00083 #define iobuf_error(io) ((io)->flags & IOBUF_ERROR)
00084
00085 #define iobuf_timedout(io) ((io)->flags & IOBUF_TIMEOUT)
00086
00087 #define iobuf_bad(io) ((io)->flags & IOBUF_BADFLAGS)
00088 int iobuf_timeout(iobuf* io, int poll_out);
00089
00090
00095 typedef int (*ibuf_fn)(int, void*, unsigned long);
00096
00098 struct ibuf
00099 {
00101 iobuf io;
00103 unsigned count;
00105 ibuf_fn readfn;
00106 };
00107 typedef struct ibuf ibuf;
00108
00109 extern ibuf inbuf;
00110
00111 int ibuf_init(ibuf* in, int fd, ibuf_fn fn, unsigned flags, unsigned bufsize);
00112 int ibuf_open(ibuf* in, const char* filename, unsigned bufsize);
00113 int ibuf_eof(ibuf* in);
00115 #define ibuf_close(in) iobuf_close(&((in)->io))
00116
00117 #define ibuf_closed(in) iobuf_closed(&((in)->io))
00118
00119 #define ibuf_error(in) iobuf_error(&((in)->io))
00120
00121 #define ibuf_timedout(in) iobuf_timedout(&((in)->io))
00122 int ibuf_refill(ibuf* in);
00123 int ibuf_read_large(ibuf* in, char* data, unsigned datalen);
00124 int ibuf_read(ibuf* in, char* data, unsigned datalen);
00125 unsigned ibuf_tell(ibuf* in);
00126 int ibuf_seek(ibuf* in, unsigned offset);
00128 #define ibuf_rewind(in) ibuf_seek(in,0)
00129
00130 #define ibuf_seekfwd(in,off) ibuf_seek(ibuf_tell(in)+(offset))
00131
00132 int ibuf_peek(ibuf* in, char* ch);
00133 int ibuf_getc(ibuf* in, char* ch);
00134 int ibuf_getu(ibuf* in, unsigned long* data);
00135 int ibuf_gets(ibuf* in, char* data, unsigned datalen, char boundary);
00136 int ibuf_getstr(ibuf* in, struct str* s, char boundary);
00137 int ibuf_getstr_crlf(ibuf* in, struct str* s);
00138
00139
00144 typedef int (*obuf_fn)(int, const void*, unsigned long);
00145
00147 struct obuf
00148 {
00150 iobuf io;
00152 unsigned bufpos;
00154 unsigned count;
00156 obuf_fn writefn;
00157 };
00158 typedef struct obuf obuf;
00159
00160 extern obuf outbuf;
00161 extern obuf errbuf;
00162
00163 #include <fcntl.h>
00165 #define OBUF_CREATE O_CREAT
00166
00167 #define OBUF_EXCLUSIVE O_EXCL
00168
00169 #define OBUF_TRUNCATE O_TRUNC
00170
00171 #define OBUF_APPEND O_APPEND
00172
00173 int obuf_init(obuf* out, int fd, obuf_fn fn, unsigned flags, unsigned bufsize);
00174 int obuf_open(obuf* out, const char* filename, int oflags, int mode, unsigned bufsize);
00175 int obuf_close(obuf* out);
00177 #define obuf_error(out) iobuf_error(&(out)->io)
00178
00179 #define obuf_closed(out) iobuf_closed(&(out)->io)
00180
00181 #define obuf_timedout(out) iobuf_timedout(&((out)->io))
00182 int obuf_flush(obuf* out);
00183 int obuf_sync(obuf* out);
00184 int obuf_write_large(obuf* out, const char* data, unsigned datalen);
00185 int obuf_write(obuf* out, const char* data, unsigned datalen);
00186 int obuf_seek(obuf* out, unsigned offset);
00188 #define obuf_rewind(out) obuf_seek(out,0)
00189
00190 #define obuf_tell(out) ((out)->io.offset+(out)->bufpos)
00191
00192 int obuf_pad(obuf* out, unsigned width, char ch);
00193 int obuf_endl(obuf* out);
00194 int obuf_putc(obuf* out, char ch);
00196 #define obuf_puts(out,str) obuf_write(out,str,strlen(str))
00197 int obuf_put2s(obuf* out, const char* s1, const char* s2);
00198 int obuf_put3s(obuf* out, const char* s1, const char* s2, const char* s3);
00199 int obuf_put4s(obuf* out, const char* s1, const char* s2, const char* s3,
00200 const char* s4);
00201 int obuf_put5s(obuf* out, const char* s1, const char* s2, const char* s3,
00202 const char* s4, const char* s5);
00203 int obuf_put6s(obuf* out, const char* s1, const char* s2, const char* s3,
00204 const char* s4, const char* s5, const char* s6);
00205 int obuf_put7s(obuf* out, const char* s1, const char* s2, const char* s3,
00206 const char* s4, const char* s5, const char* s6, const char* s7);
00208 #define obuf_putstr(out,str) obuf_write(out,(str)->s,(str)->len)
00209 int obuf_putsflush(obuf* out, const char* s);
00210 int obuf_putiw(obuf* out, long data, unsigned width, char pad);
00211 #define obuf_puti(out,data) obuf_putiw(out, data, 0, 0)
00212 int obuf_putuw(obuf* out, unsigned long data, unsigned width, char pad);
00213 #define obuf_putu(out,data) obuf_putuw(out, data, 0, 0)
00214 int obuf_putiwll(obuf* out, long long data, unsigned width, char pad);
00215 #define obuf_putill(out,data) obuf_putiw(out, data, 0, 0)
00216 int obuf_putuwll(obuf* out, unsigned long long data, unsigned width, char pad);
00217 #define obuf_putull(out,data) obuf_putuw(out, data, 0, 0)
00218 int obuf_putxw(obuf* out, unsigned long data, unsigned width, char pad);
00219 #define obuf_putx(out,data) obuf_putxw(out, data, 0, 0)
00220 int obuf_putnetstring(obuf* out, const char* data, unsigned datalen);
00221
00222
00225 int iobuf_copy(ibuf* in, obuf* out);
00226 int iobuf_copyflush(ibuf* in, obuf* out);
00227
00228
00229
00230 #endif