2 * Sherlock Library -- Fast File Buffering
4 * (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
12 byte *bptr, *bstop; /* Access pointers */
13 byte *buffer, *bufend; /* Start and end of the buffer */
14 byte *name; /* File name for error messages */
15 uns buflen; /* Size of standard portion of the buffer */
16 sh_off_t pos; /* Position of bptr in the file */
17 sh_off_t fdpos; /* Current position in the file */
18 int fd; /* File descriptor */
21 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
22 struct fastbuf *bfdopen(int fd, uns buffer);
23 void bclose(struct fastbuf *f);
24 void bflush(struct fastbuf *f);
26 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
27 void bsetpos(struct fastbuf *f, sh_off_t pos);
29 extern inline sh_off_t btell(struct fastbuf *f)
31 return f->pos + (f->bptr - f->buffer);
34 int bgetc_slow(struct fastbuf *f);
35 extern inline int bgetc(struct fastbuf *f)
37 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
40 int bpeekc_slow(struct fastbuf *f);
41 extern inline int bpeekc(struct fastbuf *f)
43 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
46 extern inline void bungetc(struct fastbuf *f, byte c)
51 void bputc_slow(struct fastbuf *f, byte c);
52 extern inline void bputc(struct fastbuf *f, byte c)
54 if (f->bptr < f->bufend)
60 word bgetw_slow(struct fastbuf *f);
61 extern inline word bgetw(struct fastbuf *f)
64 if (f->bptr + 2 <= f->bstop)
67 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
71 w = (p[0] << 8) | p[1];
73 w = (p[1] << 8) | p[0];
83 u32 bgetl_slow(struct fastbuf *f);
84 extern inline u32 bgetl(struct fastbuf *f)
87 if (f->bptr + 4 <= f->bstop)
90 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
94 l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
96 l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
103 return bgetl_slow(f);
106 u64 bgetq_slow(struct fastbuf *f);
107 extern inline u64 bgetq(struct fastbuf *f)
109 if (f->bptr + 8 <= f->bstop)
112 memcpy(&l, f->bptr, 8);
117 return bgetq_slow(f);
120 u64 bget5_slow(struct fastbuf *f);
121 extern inline u64 bget5(struct fastbuf *f)
124 if (f->bptr + 5 <= f->bstop)
127 #ifdef CPU_BIG_ENDIAN
128 l = ((u64)p[0] << 32) | ((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
130 l = ((u64)p[4] << 32) | ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
136 return bget5_slow(f);
139 void bputw_slow(struct fastbuf *f, word w);
140 extern inline void bputw(struct fastbuf *f, word w)
142 if (f->bptr + 2 <= f->bufend)
145 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
148 #ifdef CPU_BIG_ENDIAN
162 void bputl_slow(struct fastbuf *f, u32 l);
163 extern inline void bputl(struct fastbuf *f, u32 l)
165 if (f->bptr + 4 <= f->bufend)
168 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
171 #ifdef CPU_BIG_ENDIAN
189 void bputq_slow(struct fastbuf *f, u64 l);
190 extern inline void bputq(struct fastbuf *f, u64 l)
192 if (f->bptr + 8 <= f->bufend)
194 memcpy(f->bptr, &l, 8);
201 void bput5_slow(struct fastbuf *f, u64 l);
202 extern inline void bput5(struct fastbuf *f, u64 l)
204 if (f->bptr + 5 <= f->bufend)
208 #ifdef CPU_BIG_ENDIAN
227 void bread_slow(struct fastbuf *f, void *b, uns l);
228 extern inline void bread(struct fastbuf *f, void *b, uns l)
230 if (f->bptr + l <= f->bstop)
232 memcpy(b, f->bptr, l);
239 void bwrite_slow(struct fastbuf *f, void *b, uns l);
240 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
242 if (f->bptr + l <= f->bufend)
244 memcpy(f->bptr, b, l);
248 bwrite_slow(f, b, l);
251 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
252 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
255 bputs(struct fastbuf *f, byte *b)
257 bwrite(f, b, strlen(b));
261 bputsn(struct fastbuf *f, byte *b)
267 #ifdef SHERLOCK_CONFIG_LARGE_DB
268 #define bgeto(f) bget5(f)
269 #define bputo(f,l) bput5(f,l)
270 #define bgetp(f) bgetq(f)
271 #define bputp(f,l) bputq(f,l)
273 #define bgeto(f) bgetl(f)
274 #define bputo(f,l) bputl(f,l)
275 #define bgetp(f) bgetl(f)
276 #define bputp(f,l) bputl(f,l)