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 extern const char obuf_dec_digits[10];
00164 extern const char obuf_hex_lcase_digits[16];
00165 extern const char obuf_hex_ucase_digits[16];
00166
00167 #include <fcntl.h>
00169 #define OBUF_CREATE O_CREAT
00170
00171 #define OBUF_EXCLUSIVE O_EXCL
00172
00173 #define OBUF_TRUNCATE O_TRUNC
00174
00175 #define OBUF_APPEND O_APPEND
00176
00177 int obuf_init(obuf* out, int fd, obuf_fn fn, unsigned flags, unsigned bufsize);
00178 int obuf_open(obuf* out, const char* filename, int oflags, int mode, unsigned bufsize);
00179 int obuf_close(obuf* out);
00181 #define obuf_error(out) iobuf_error(&(out)->io)
00182
00183 #define obuf_closed(out) iobuf_closed(&(out)->io)
00184
00185 #define obuf_timedout(out) iobuf_timedout(&((out)->io))
00186 int obuf_flush(obuf* out);
00187 int obuf_sync(obuf* out);
00188 int obuf_write_large(obuf* out, const char* data, unsigned datalen);
00189 int obuf_write(obuf* out, const char* data, unsigned datalen);
00190 int obuf_seek(obuf* out, unsigned offset);
00192 #define obuf_rewind(out) obuf_seek(out,0)
00193
00194 #define obuf_tell(out) ((out)->io.offset+(out)->bufpos)
00195
00196 int obuf_pad(obuf* out, unsigned width, char ch);
00197 int obuf_endl(obuf* out);
00198 int obuf_putc(obuf* out, char ch);
00200 #define obuf_puts(out,str) obuf_write(out,str,strlen(str))
00201 int obuf_put2s(obuf* out, const char* s1, const char* s2);
00202 int obuf_put3s(obuf* out, const char* s1, const char* s2, const char* s3);
00203 int obuf_put4s(obuf* out, const char* s1, const char* s2, const char* s3,
00204 const char* s4);
00205 int obuf_put5s(obuf* out, const char* s1, const char* s2, const char* s3,
00206 const char* s4, const char* s5);
00207 int obuf_put6s(obuf* out, const char* s1, const char* s2, const char* s3,
00208 const char* s4, const char* s5, const char* s6);
00209 int obuf_put7s(obuf* out, const char* s1, const char* s2, const char* s3,
00210 const char* s4, const char* s5, const char* s6, const char* s7);
00212 #define obuf_putstr(out,str) obuf_write(out,(str)->s,(str)->len)
00213 int obuf_putsflush(obuf* out, const char* s);
00214 int obuf_puti(obuf* out, long data);
00215 int obuf_putiw(obuf* out, long data, unsigned width, char pad);
00216 int obuf_putu(obuf* out, unsigned long data);
00217 int obuf_putuw(obuf* out, unsigned long data, unsigned width, char pad);
00218 int obuf_putill(obuf* out, long long data);
00219 int obuf_putiwll(obuf* out, long long data, unsigned width, char pad);
00220 int obuf_putull(obuf* out, unsigned long long data);
00221 int obuf_putuwll(obuf* out, unsigned long long data, unsigned width, char pad);
00222 int obuf_putx(obuf* out, unsigned long data);
00223 int obuf_putxw(obuf* out, unsigned long data, unsigned width, char pad);
00224 int obuf_putX(obuf* out, unsigned long data);
00225 int obuf_putXw(obuf* out, unsigned long data, unsigned width, char pad);
00226 int obuf_putxll(obuf* out, unsigned long long data);
00227 int obuf_putxwll(obuf* out, unsigned long long data, unsigned width, char pad);
00228 int obuf_putXll(obuf* out, unsigned long long data);
00229 int obuf_putXwll(obuf* out, unsigned long long data, unsigned width, char pad);
00230 int obuf_putsnumw(obuf* out, long num, unsigned width, char pad,
00231 unsigned base, const char* digits);
00232 int obuf_putunumw(obuf* out, unsigned long num, unsigned width, char pad,
00233 unsigned base, const char* digits);
00234 int obuf_putsllnumw(obuf* out, long long num, unsigned width, char pad,
00235 unsigned base, const char* digits);
00236 int obuf_putullnumw(obuf* out, unsigned long long num, unsigned width, char pad,
00237 unsigned base, const char* digits);
00238 int obuf_putnetstring(obuf* out, const char* data, unsigned datalen);
00239 int obuf_sign_pad(obuf* out, int sign, unsigned width, char pad);
00240
00241
00244 int iobuf_copy(ibuf* in, obuf* out);
00245 int iobuf_copyflush(ibuf* in, obuf* out);
00246
00247
00248
00249 #endif