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