]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Added another version of bgets() which doesn't die on too long lines and
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_FASTBUF_H
11 #define _SHERLOCK_FASTBUF_H
12
13 #ifndef EOF
14 #include <stdio.h>
15 #endif
16
17 #include <string.h>
18 #include <stdarg.h>
19
20 #include "lib/unaligned.h"
21
22 /*
23  *  Generic buffered I/O. You supply hooks to be called for low-level operations
24  *  (swapping of buffers, seeking and closing), we do the rest.
25  *
26  *  Buffer layout when reading:
27  *
28  *  +----------------+---------------------------+
29  *  | read data      | free space                |
30  *  +----------------+---------------------------+
31  *  ^        ^        ^                           ^
32  *  buffer   bptr     bstop                       bufend
33  *
34  *  After the last character is read, bptr == bstop and buffer refill
35  *  is deferred to the next read attempt. This gives us an easy way
36  *  how to implement bungetc().
37  *
38  *  When writing:
39  *
40  *  +--------+--------------+--------------------+
41  *  | unused | written data | free space         |
42  *  +--------+--------------+--------------------+
43  *  ^         ^              ^                    ^
44  *  buffer    bstop          bptr                 bufend
45  *
46  *  Dirty tricks:
47  *
48  *    - You can mix reads and writes on the same stream, but you must
49  *      call bflush() in between and remember that the file position
50  *      points after the flushed buffer which is not necessarily the same
51  *      as after the data you've read.
52  *    - The spout/refill hooks can change not only bptr and bstop, but also
53  *      the location of the buffer; fb-mem.c takes advantage of it.
54  */
55
56 struct fastbuf {
57   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
58   byte *bptr, *bstop;                   /* Access pointers */
59   byte *buffer, *bufend;                /* Start and end of the buffer */
60   byte *name;                           /* File name for error messages */
61   sh_off_t pos;                         /* Position of bstop in the file */
62   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
63   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
64   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
65   void (*close)(struct fastbuf *);      /* Close the stream */
66   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
67 };
68
69 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
70
71 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
72 struct fastbuf *bopen_tmp(uns buffer);
73 struct fastbuf *bfdopen(int fd, uns buffer);
74 struct fastbuf *bfdopen_shared(int fd, uns buffer);
75
76 /* FastIO on in-memory streams */
77
78 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
79 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
80
81 /* FastIO on memory mapped files */
82
83 struct fastbuf *bopen_mm(byte *name, uns mode);
84
85 /* Configuring stream parameters */
86
87 int bconfig(struct fastbuf *f, uns type, int data);
88
89 #define BCONFIG_IS_TEMP_FILE 0
90
91 /* Universal functions working on all fastbuf's */
92
93 void bclose(struct fastbuf *f);
94 void bflush(struct fastbuf *f);
95 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
96 void bsetpos(struct fastbuf *f, sh_off_t pos);
97
98 static inline sh_off_t btell(struct fastbuf *f)
99 {
100   return f->pos + (f->bptr - f->bstop);
101 }
102
103 int bgetc_slow(struct fastbuf *f);
104 static inline int bgetc(struct fastbuf *f)
105 {
106   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
107 }
108
109 int bpeekc_slow(struct fastbuf *f);
110 static inline int bpeekc(struct fastbuf *f)
111 {
112   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
113 }
114
115 static inline void bungetc(struct fastbuf *f)
116 {
117   f->bptr--;
118 }
119
120 void bputc_slow(struct fastbuf *f, uns c);
121 static inline void bputc(struct fastbuf *f, uns c)
122 {
123   if (f->bptr < f->bufend)
124     *f->bptr++ = c;
125   else
126     bputc_slow(f, c);
127 }
128
129 int bgetw_slow(struct fastbuf *f);
130 static inline int bgetw(struct fastbuf *f)
131 {
132   int w;
133   if (f->bptr + 2 <= f->bstop)
134     {
135       w = GET_U16(f->bptr);
136       f->bptr += 2;
137       return w;
138     }
139   else
140     return bgetw_slow(f);
141 }
142
143 u32 bgetl_slow(struct fastbuf *f);
144 static inline u32 bgetl(struct fastbuf *f)
145 {
146   u32 l;
147   if (f->bptr + 4 <= f->bstop)
148     {
149       l = GET_U32(f->bptr);
150       f->bptr += 4;
151       return l;
152     }
153   else
154     return bgetl_slow(f);
155 }
156
157 u64 bgetq_slow(struct fastbuf *f);
158 static inline u64 bgetq(struct fastbuf *f)
159 {
160   u64 l;
161   if (f->bptr + 8 <= f->bstop)
162     {
163       l = GET_U64(f->bptr);
164       f->bptr += 8;
165       return l;
166     }
167   else
168     return bgetq_slow(f);
169 }
170
171 u64 bget5_slow(struct fastbuf *f);
172 static inline u64 bget5(struct fastbuf *f)
173 {
174   u64 l;
175   if (f->bptr + 5 <= f->bstop)
176     {
177       l = GET_U40(f->bptr);
178       f->bptr += 5;
179       return l;
180     }
181   else
182     return bget5_slow(f);
183 }
184
185 void bputw_slow(struct fastbuf *f, uns w);
186 static inline void bputw(struct fastbuf *f, uns w)
187 {
188   if (f->bptr + 2 <= f->bufend)
189     {
190       PUT_U16(f->bptr, w);
191       f->bptr += 2;
192     }
193   else
194     bputw_slow(f, w);
195 }
196
197 void bputl_slow(struct fastbuf *f, u32 l);
198 static inline void bputl(struct fastbuf *f, u32 l)
199 {
200   if (f->bptr + 4 <= f->bufend)
201     {
202       PUT_U32(f->bptr, l);
203       f->bptr += 4;
204     }
205   else
206     bputl_slow(f, l);
207 }
208
209 void bputq_slow(struct fastbuf *f, u64 l);
210 static inline void bputq(struct fastbuf *f, u64 l)
211 {
212   if (f->bptr + 8 <= f->bufend)
213     {
214       PUT_U64(f->bptr, l);
215       f->bptr += 8;
216     }
217   else
218     bputq_slow(f, l);
219 }
220
221 void bput5_slow(struct fastbuf *f, u64 l);
222 static inline void bput5(struct fastbuf *f, u64 l)
223 {
224   if (f->bptr + 5 <= f->bufend)
225     {
226       PUT_U40(f->bptr, l);
227       f->bptr += 5;
228     }
229   else
230     bput5_slow(f, l);
231 }
232
233 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
234 static inline uns bread(struct fastbuf *f, void *b, uns l)
235 {
236   if (f->bptr + l <= f->bstop)
237     {
238       memcpy(b, f->bptr, l);
239       f->bptr += l;
240       return l;
241     }
242   else
243     return bread_slow(f, b, l, 0);
244 }
245
246 static inline uns breadb(struct fastbuf *f, void *b, uns l)
247 {
248   if (f->bptr + l <= f->bstop)
249     {
250       memcpy(b, f->bptr, l);
251       f->bptr += l;
252       return l;
253     }
254   else
255     return bread_slow(f, b, l, 1);
256 }
257
258 void bwrite_slow(struct fastbuf *f, void *b, uns l);
259 static inline void bwrite(struct fastbuf *f, void *b, uns l)
260 {
261   if (f->bptr + l <= f->bufend)
262     {
263       memcpy(f->bptr, b, l);
264       f->bptr += l;
265     }
266   else
267     bwrite_slow(f, b, l);
268 }
269
270 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
271 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
272 byte *bgets0(struct fastbuf *f, byte *b, uns l);
273
274 static inline void
275 bputs(struct fastbuf *f, byte *b)
276 {
277   bwrite(f, b, strlen(b));
278 }
279
280 static inline void
281 bputs0(struct fastbuf *f, byte *b)
282 {
283   bwrite(f, b, strlen(b)+1);
284 }
285
286 static inline void
287 bputsn(struct fastbuf *f, byte *b)
288 {
289   bputs(f, b);
290   bputc(f, '\n');
291 }
292
293 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
294 static inline void
295 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
296 {
297   if (f->bptr + l <= f->bstop &&
298       t->bptr + l <= t->bufend)
299     {
300       memcpy(t->bptr, f->bptr, l);
301       t->bptr += l;
302       f->bptr += l;
303     }
304   else
305     bbcopy_slow(f, t, l);
306 }
307
308 /* I/O on addr_int_t */
309
310 #ifdef CPU_64BIT_POINTERS
311 #define bputa(x,p) bputq(x,p)
312 #define bgeta(x) bgetq(x)
313 #else
314 #define bputa(x,p) bputl(x,p)
315 #define bgeta(x) bgetl(x)
316 #endif
317
318 /* Direct I/O on buffers */
319
320 static inline uns
321 bdirect_read_prepare(struct fastbuf *f, byte **buf)
322 {
323   if (f->bptr == f->bstop && !f->refill(f))
324     return 0;
325   *buf = f->bptr;
326   return f->bstop - f->bptr;
327 }
328
329 static inline void
330 bdirect_read_commit(struct fastbuf *f, byte *pos)
331 {
332   f->bptr = pos;
333 }
334
335 static inline uns
336 bdirect_write_prepare(struct fastbuf *f, byte **buf)
337 {
338   if (f->bptr == f->bufend)
339     f->spout(f);
340   *buf = f->bptr;
341   return f->bufend - f->bptr;
342 }
343
344 static inline void
345 bdirect_write_commit(struct fastbuf *f, byte *pos)
346 {
347   f->bptr = pos;
348 }
349
350 /* Formatted output */
351
352 int bprintf(struct fastbuf *b, byte *msg, ...);
353 int vbprintf(struct fastbuf *b, byte *msg, va_list args);
354
355 #endif