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