]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Added bopen_tmp() for opening of temporary files.
[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 *bopen_tmp(uns buffer);
63 struct fastbuf *bfdopen(int fd, uns buffer);
64 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
65
66 /* FastIO on in-memory streams */
67
68 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
69 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
70
71 /* Universal functions working on all fastbuf's */
72
73 void bclose(struct fastbuf *f);
74 void bflush(struct fastbuf *f);
75 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
76 void bsetpos(struct fastbuf *f, sh_off_t pos);
77
78 static inline sh_off_t btell(struct fastbuf *f)
79 {
80   return f->pos + (f->bptr - f->buffer);
81 }
82
83 int bgetc_slow(struct fastbuf *f);
84 static inline int bgetc(struct fastbuf *f)
85 {
86   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
87 }
88
89 int bpeekc_slow(struct fastbuf *f);
90 static inline int bpeekc(struct fastbuf *f)
91 {
92   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
93 }
94
95 static inline void bungetc(struct fastbuf *f)
96 {
97   f->bptr--;
98 }
99
100 void bputc_slow(struct fastbuf *f, uns c);
101 static inline void bputc(struct fastbuf *f, uns c)
102 {
103   if (f->bptr < f->bufend)
104     *f->bptr++ = c;
105   else
106     bputc_slow(f, c);
107 }
108
109 int bgetw_slow(struct fastbuf *f);
110 static inline int bgetw(struct fastbuf *f)
111 {
112   int w;
113   if (f->bptr + 2 <= f->bstop)
114     {
115       w = GET_U16(f->bptr);
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       l = GET_U32(f->bptr);
130       f->bptr += 4;
131       return l;
132     }
133   else
134     return bgetl_slow(f);
135 }
136
137 u64 bgetq_slow(struct fastbuf *f);
138 static inline u64 bgetq(struct fastbuf *f)
139 {
140   u64 l;
141   if (f->bptr + 8 <= f->bstop)
142     {
143       l = GET_U64(f->bptr);
144       f->bptr += 8;
145       return l;
146     }
147   else
148     return bgetq_slow(f);
149 }
150
151 u64 bget5_slow(struct fastbuf *f);
152 static inline u64 bget5(struct fastbuf *f)
153 {
154   u64 l;
155   if (f->bptr + 5 <= f->bstop)
156     {
157       l = GET_U40(f->bptr);
158       f->bptr += 5;
159       return l;
160     }
161   else
162     return bget5_slow(f);
163 }
164
165 void bputw_slow(struct fastbuf *f, uns w);
166 static inline void bputw(struct fastbuf *f, uns w)
167 {
168   if (f->bptr + 2 <= f->bufend)
169     {
170       PUT_U16(f->bptr, w);
171       f->bptr += 2;
172     }
173   else
174     bputw_slow(f, w);
175 }
176
177 void bputl_slow(struct fastbuf *f, u32 l);
178 static inline void bputl(struct fastbuf *f, u32 l)
179 {
180   if (f->bptr + 4 <= f->bufend)
181     {
182       PUT_U32(f->bptr, l);
183       f->bptr += 4;
184     }
185   else
186     bputl_slow(f, l);
187 }
188
189 void bputq_slow(struct fastbuf *f, u64 l);
190 static inline void bputq(struct fastbuf *f, u64 l)
191 {
192   if (f->bptr + 8 <= f->bufend)
193     {
194       PUT_U64(f->bptr, l);
195       f->bptr += 8;
196     }
197   else
198     bputq_slow(f, l);
199 }
200
201 void bput5_slow(struct fastbuf *f, u64 l);
202 static inline void bput5(struct fastbuf *f, u64 l)
203 {
204   if (f->bptr + 5 <= f->bufend)
205     {
206       PUT_U40(f->bptr, l);
207       f->bptr += 5;
208     }
209   else
210     bput5_slow(f, l);
211 }
212
213 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
214 static inline uns bread(struct fastbuf *f, void *b, uns l)
215 {
216   if (f->bptr + l <= f->bstop)
217     {
218       memcpy(b, f->bptr, l);
219       f->bptr += l;
220       return l;
221     }
222   else
223     return bread_slow(f, b, l, 0);
224 }
225
226 static inline uns breadb(struct fastbuf *f, void *b, uns l)
227 {
228   if (f->bptr + l <= f->bstop)
229     {
230       memcpy(b, f->bptr, l);
231       f->bptr += l;
232       return l;
233     }
234   else
235     return bread_slow(f, b, l, 1);
236 }
237
238 void bwrite_slow(struct fastbuf *f, void *b, uns l);
239 static inline void bwrite(struct fastbuf *f, void *b, uns l)
240 {
241   if (f->bptr + l <= f->bufend)
242     {
243       memcpy(f->bptr, b, l);
244       f->bptr += l;
245     }
246   else
247     bwrite_slow(f, b, l);
248 }
249
250 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
251 byte *bgets0(struct fastbuf *f, byte *b, uns l);
252
253 static inline void
254 bputs(struct fastbuf *f, byte *b)
255 {
256   bwrite(f, b, strlen(b));
257 }
258
259 static inline void
260 bputs0(struct fastbuf *f, byte *b)
261 {
262   bwrite(f, b, strlen(b)+1);
263 }
264
265 static inline void
266 bputsn(struct fastbuf *f, byte *b)
267 {
268   bputs(f, b);
269   bputc(f, '\n');
270 }
271
272 /* I/O on addr_int_t */
273
274 #ifdef CPU_64BIT_POINTERS
275 #define bputa(x,p) bputq(x,p)
276 #define bgeta(x) bgetq(x)
277 #else
278 #define bputa(x,p) bputl(x,p)
279 #define bgeta(x) bgetl(x)
280 #endif
281
282 /* Direct I/O on buffers */
283
284 int bdirect_read(struct fastbuf *f, byte **buf);
285 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
286 void bdirect_write_commit(struct fastbuf *f, byte *pos);
287
288 #endif