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