]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
69df9b710bc4f565a38c85b65143fdf7f84dba41
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2000 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
19 #include "lib/unaligned.h"
20
21 /*
22  *  Generic buffered I/O on a top of buffer swapping functions.
23  *
24  *  Buffer layout when reading:
25  *
26  *  +----------------+---------------------------+
27  *  | read data      | free space                |
28  *  +----------------+---------------------------+
29  *  ^        ^        ^                           ^
30  *  buffer   bptr     bstop                       bufend
31  *
32  *  After the last character is read, bptr == bstop and buffer refill
33  *  is deferred to the next read attempt. This gives us an easy way
34  *  how to implement bungetc().
35  *
36  *  When writing:
37  *
38  *  +----------------+---------------------------+
39  *  | written data   | free space                |
40  *  +----------------+---------------------------+
41  *  ^                 ^                           ^
42  *  buffer=bstop      bptr                        bufend
43  */
44
45 struct fastbuf {
46   byte *bptr, *bstop;                   /* Access pointers */
47   byte *buffer, *bufend;                /* Start and end of the buffer */
48   byte *name;                           /* File name for error messages */
49   uns buflen;                           /* Size of the buffer */
50   sh_off_t pos;                         /* Position of buffer start in the file */
51   sh_off_t fdpos;                       /* Current position in the non-buffered file */
52   int fd;                               /* File descriptor, -1 if not a real file */
53   int is_temp_file;                     /* Is a temporary file, delete on close */
54   void *lldata;                         /* Data private to access functions below */
55   void *llpos;                          /* ... continued ... */
56   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
57   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
58   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
59   void (*close)(struct fastbuf *);      /* Close the stream */
60 };
61
62 /* FastIO on standard files */
63
64 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
65 struct fastbuf *bopen_tmp(uns buffer);
66 struct fastbuf *bfdopen(int fd, uns buffer);
67 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
68
69 /* FastIO on in-memory streams */
70
71 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
72 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
73
74 /* Universal functions working on all fastbuf's */
75
76 void bclose(struct fastbuf *f);
77 void bflush(struct fastbuf *f);
78 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
79 void bsetpos(struct fastbuf *f, sh_off_t pos);
80
81 static inline sh_off_t btell(struct fastbuf *f)
82 {
83   return f->pos + (f->bptr - f->buffer);
84 }
85
86 int bgetc_slow(struct fastbuf *f);
87 static inline int bgetc(struct fastbuf *f)
88 {
89   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
90 }
91
92 int bpeekc_slow(struct fastbuf *f);
93 static inline int bpeekc(struct fastbuf *f)
94 {
95   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
96 }
97
98 static inline void bungetc(struct fastbuf *f)
99 {
100   f->bptr--;
101 }
102
103 void bputc_slow(struct fastbuf *f, uns c);
104 static inline void bputc(struct fastbuf *f, uns c)
105 {
106   if (f->bptr < f->bufend)
107     *f->bptr++ = c;
108   else
109     bputc_slow(f, c);
110 }
111
112 int bgetw_slow(struct fastbuf *f);
113 static inline int bgetw(struct fastbuf *f)
114 {
115   int w;
116   if (f->bptr + 2 <= f->bstop)
117     {
118       w = GET_U16(f->bptr);
119       f->bptr += 2;
120       return w;
121     }
122   else
123     return bgetw_slow(f);
124 }
125
126 u32 bgetl_slow(struct fastbuf *f);
127 static inline u32 bgetl(struct fastbuf *f)
128 {
129   u32 l;
130   if (f->bptr + 4 <= f->bstop)
131     {
132       l = GET_U32(f->bptr);
133       f->bptr += 4;
134       return l;
135     }
136   else
137     return bgetl_slow(f);
138 }
139
140 u64 bgetq_slow(struct fastbuf *f);
141 static inline u64 bgetq(struct fastbuf *f)
142 {
143   u64 l;
144   if (f->bptr + 8 <= f->bstop)
145     {
146       l = GET_U64(f->bptr);
147       f->bptr += 8;
148       return l;
149     }
150   else
151     return bgetq_slow(f);
152 }
153
154 u64 bget5_slow(struct fastbuf *f);
155 static inline u64 bget5(struct fastbuf *f)
156 {
157   u64 l;
158   if (f->bptr + 5 <= f->bstop)
159     {
160       l = GET_U40(f->bptr);
161       f->bptr += 5;
162       return l;
163     }
164   else
165     return bget5_slow(f);
166 }
167
168 void bputw_slow(struct fastbuf *f, uns w);
169 static inline void bputw(struct fastbuf *f, uns w)
170 {
171   if (f->bptr + 2 <= f->bufend)
172     {
173       PUT_U16(f->bptr, w);
174       f->bptr += 2;
175     }
176   else
177     bputw_slow(f, w);
178 }
179
180 void bputl_slow(struct fastbuf *f, u32 l);
181 static inline void bputl(struct fastbuf *f, u32 l)
182 {
183   if (f->bptr + 4 <= f->bufend)
184     {
185       PUT_U32(f->bptr, l);
186       f->bptr += 4;
187     }
188   else
189     bputl_slow(f, l);
190 }
191
192 void bputq_slow(struct fastbuf *f, u64 l);
193 static inline void bputq(struct fastbuf *f, u64 l)
194 {
195   if (f->bptr + 8 <= f->bufend)
196     {
197       PUT_U64(f->bptr, l);
198       f->bptr += 8;
199     }
200   else
201     bputq_slow(f, l);
202 }
203
204 void bput5_slow(struct fastbuf *f, u64 l);
205 static inline void bput5(struct fastbuf *f, u64 l)
206 {
207   if (f->bptr + 5 <= f->bufend)
208     {
209       PUT_U40(f->bptr, l);
210       f->bptr += 5;
211     }
212   else
213     bput5_slow(f, l);
214 }
215
216 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
217 static inline uns bread(struct fastbuf *f, void *b, uns l)
218 {
219   if (f->bptr + l <= f->bstop)
220     {
221       memcpy(b, f->bptr, l);
222       f->bptr += l;
223       return l;
224     }
225   else
226     return bread_slow(f, b, l, 0);
227 }
228
229 static inline uns breadb(struct fastbuf *f, void *b, uns l)
230 {
231   if (f->bptr + l <= f->bstop)
232     {
233       memcpy(b, f->bptr, l);
234       f->bptr += l;
235       return l;
236     }
237   else
238     return bread_slow(f, b, l, 1);
239 }
240
241 void bwrite_slow(struct fastbuf *f, void *b, uns l);
242 static inline void bwrite(struct fastbuf *f, void *b, uns l)
243 {
244   if (f->bptr + l <= f->bufend)
245     {
246       memcpy(f->bptr, b, l);
247       f->bptr += l;
248     }
249   else
250     bwrite_slow(f, b, l);
251 }
252
253 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
254 byte *bgets0(struct fastbuf *f, byte *b, uns l);
255
256 static inline void
257 bputs(struct fastbuf *f, byte *b)
258 {
259   bwrite(f, b, strlen(b));
260 }
261
262 static inline void
263 bputs0(struct fastbuf *f, byte *b)
264 {
265   bwrite(f, b, strlen(b)+1);
266 }
267
268 static inline void
269 bputsn(struct fastbuf *f, byte *b)
270 {
271   bputs(f, b);
272   bputc(f, '\n');
273 }
274
275 /* I/O on addr_int_t */
276
277 #ifdef CPU_64BIT_POINTERS
278 #define bputa(x,p) bputq(x,p)
279 #define bgeta(x) bgetq(x)
280 #else
281 #define bputa(x,p) bputl(x,p)
282 #define bgeta(x) bgetl(x)
283 #endif
284
285 /* Direct I/O on buffers */
286
287 int bdirect_read_prepare(struct fastbuf *f, byte **buf);
288 void bdirect_read_commit(struct fastbuf *f, byte *pos);
289 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
290 void bdirect_write_commit(struct fastbuf *f, byte *pos);
291
292 #endif