Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Related Pages

iobuf.h

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 int ibuf_readall(ibuf* in, struct str* s);
00139 int ibuf_openreadclose(const char* filename, struct str* s);
00140 /* @} */
00141 
00146 typedef int (*obuf_fn)(int, const void*, unsigned long);
00147 
00149 struct obuf
00150 {
00152   iobuf io;
00154   unsigned bufpos;
00156   unsigned count;
00158   obuf_fn writefn;
00159 };
00160 typedef struct obuf obuf;
00161 
00162 extern obuf outbuf;
00163 extern obuf errbuf;
00164 
00165 extern const char obuf_dec_digits[10];
00166 extern const char obuf_hex_lcase_digits[16];
00167 extern const char obuf_hex_ucase_digits[16];
00168 
00169 #include <fcntl.h>
00171 #define OBUF_CREATE O_CREAT
00172 
00173 #define OBUF_EXCLUSIVE O_EXCL
00174 
00175 #define OBUF_TRUNCATE O_TRUNC
00176 
00177 #define OBUF_APPEND O_APPEND
00178 
00179 int obuf_init(obuf* out, int fd, obuf_fn fn, unsigned flags, unsigned bufsize);
00180 int obuf_open(obuf* out, const char* filename, int oflags, int mode, unsigned bufsize);
00181 int obuf_close(obuf* out);
00183 #define obuf_error(out) iobuf_error(&(out)->io)
00184 
00185 #define obuf_closed(out) iobuf_closed(&(out)->io)
00186 
00187 #define obuf_timedout(out) iobuf_timedout(&((out)->io))
00188 int obuf_flush(obuf* out);
00189 int obuf_sync(obuf* out);
00190 int obuf_write_large(obuf* out, const char* data, unsigned datalen);
00191 int obuf_write(obuf* out, const char* data, unsigned datalen);
00192 int obuf_seek(obuf* out, unsigned offset);
00194 #define obuf_rewind(out) obuf_seek(out,0)
00195 
00196 #define obuf_tell(out) ((out)->io.offset+(out)->bufpos)
00197 
00198 int obuf_pad(obuf* out, unsigned width, char ch);
00199 int obuf_endl(obuf* out);
00200 int obuf_putc(obuf* out, char ch);
00202 #define obuf_puts(out,str) obuf_write(out,str,strlen(str))
00203 int obuf_put2s(obuf* out, const char* s1, const char* s2);
00204 int obuf_put3s(obuf* out, const char* s1, const char* s2, const char* s3);
00205 int obuf_put4s(obuf* out, const char* s1, const char* s2, const char* s3,
00206                const char* s4);
00207 int obuf_put5s(obuf* out, const char* s1, const char* s2, const char* s3,
00208                const char* s4, const char* s5);
00209 int obuf_put6s(obuf* out, const char* s1, const char* s2, const char* s3,
00210                const char* s4, const char* s5, const char* s6);
00211 int obuf_put7s(obuf* out, const char* s1, const char* s2, const char* s3,
00212                const char* s4, const char* s5, const char* s6, const char* s7);
00213 int obuf_putns(obuf* out, unsigned int count, ...);
00215 #define obuf_putstr(out,str) obuf_write(out,(str)->s,(str)->len)
00216 int obuf_putsflush(obuf* out, const char* s);
00217 int obuf_puti(obuf* out, long data);
00218 int obuf_putiw(obuf* out, long data, unsigned width, char pad);
00219 int obuf_putu(obuf* out, unsigned long data);
00220 int obuf_putuw(obuf* out, unsigned long data, unsigned width, char pad);
00221 int obuf_putill(obuf* out, long long data);
00222 int obuf_putiwll(obuf* out, long long data, unsigned width, char pad);
00223 int obuf_putull(obuf* out, unsigned long long data);
00224 int obuf_putuwll(obuf* out, unsigned long long data, unsigned width, char pad);
00225 int obuf_putx(obuf* out, unsigned long data);
00226 int obuf_putxw(obuf* out, unsigned long data, unsigned width, char pad);
00227 int obuf_putX(obuf* out, unsigned long data);
00228 int obuf_putXw(obuf* out, unsigned long data, unsigned width, char pad);
00229 int obuf_putxll(obuf* out, unsigned long long data);
00230 int obuf_putxwll(obuf* out, unsigned long long data, unsigned width, char pad);
00231 int obuf_putXll(obuf* out, unsigned long long data);
00232 int obuf_putXwll(obuf* out, unsigned long long data, unsigned width, char pad);
00233 int obuf_putsnumw(obuf* out, long num, unsigned width, char pad,
00234                   unsigned base, const char* digits);
00235 int obuf_putunumw(obuf* out, unsigned long num, unsigned width, char pad,
00236                   unsigned base, const char* digits);
00237 int obuf_putsllnumw(obuf* out, long long num, unsigned width, char pad,
00238                     unsigned base, const char* digits);
00239 int obuf_putullnumw(obuf* out, unsigned long long num, unsigned width, char pad,
00240                     unsigned base, const char* digits);
00241 int obuf_putnetstring(obuf* out, const char* data, unsigned datalen);
00242 int obuf_sign_pad(obuf* out, int sign, unsigned width, char pad);
00243 /* @} */
00244 
00247 int iobuf_copy(ibuf* in, obuf* out);
00248 int iobuf_copyflush(ibuf* in, obuf* out);
00249 int ibuf_copytofd(ibuf* in, int out);
00250 int obuf_copyfromfd(int in, obuf* out);
00251 
00252 /* @} */
00253 
00254 #endif

Generated on Tue Mar 23 21:58:09 2004 for bglibs by doxygen 1.3.5