2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2000 Martin Mares <mj@ucw.cz>
13 void bclose(struct fastbuf *f)
20 void bflush(struct fastbuf *f)
22 if (f->bptr != f->buffer)
23 { /* Have something to flush */
24 if (f->bstop > f->buffer) /* Read data? */
26 f->bptr = f->bstop = f->buffer;
29 else /* Write data... */
34 inline void bsetpos(struct fastbuf *f, sh_off_t pos)
36 if (pos >= f->pos && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer)))
37 f->bptr = f->buffer + (pos - f->pos);
41 f->seek(f, pos, SEEK_SET);
45 void bseek(struct fastbuf *f, sh_off_t pos, int whence)
52 return bsetpos(f, pos);
54 return bsetpos(f, btell(f) + pos);
57 f->seek(f, pos, SEEK_END);
60 die("bseek: invalid whence=%d", whence);
64 int bgetc_slow(struct fastbuf *f)
66 if (f->bptr < f->bstop)
73 int bpeekc_slow(struct fastbuf *f)
75 if (f->bptr < f->bstop)
82 void bputc_slow(struct fastbuf *f, byte c)
84 if (f->bptr >= f->bufend)
89 word bgetw_slow(struct fastbuf *f)
91 word w = bgetc_slow(f);
93 return (w << 8) | bgetc_slow(f);
95 return w | (bgetc_slow(f) << 8);
99 u32 bgetl_slow(struct fastbuf *f)
101 u32 l = bgetc_slow(f);
102 #ifdef CPU_BIG_ENDIAN
103 l = (l << 8) | bgetc_slow(f);
104 l = (l << 8) | bgetc_slow(f);
105 return (l << 8) | bgetc_slow(f);
107 l = (bgetc_slow(f) << 8) | l;
108 l = (bgetc_slow(f) << 16) | l;
109 return (bgetc_slow(f) << 24) | l;
113 u64 bgetq_slow(struct fastbuf *f)
116 #ifdef CPU_BIG_ENDIAN
123 return ((u64) h << 32) | l;
126 u64 bget5_slow(struct fastbuf *f)
129 #ifdef CPU_BIG_ENDIAN
136 return ((u64) h << 32) | l;
139 void bputw_slow(struct fastbuf *f, word w)
141 #ifdef CPU_BIG_ENDIAN
142 bputc_slow(f, w >> 8);
146 bputc_slow(f, w >> 8);
150 void bputl_slow(struct fastbuf *f, u32 l)
152 #ifdef CPU_BIG_ENDIAN
153 bputc_slow(f, l >> 24);
154 bputc_slow(f, l >> 16);
155 bputc_slow(f, l >> 8);
159 bputc_slow(f, l >> 8);
160 bputc_slow(f, l >> 16);
161 bputc_slow(f, l >> 24);
165 void bputq_slow(struct fastbuf *f, u64 q)
167 #ifdef CPU_BIG_ENDIAN
168 bputl_slow(f, q >> 32);
172 bputl_slow(f, q >> 32);
176 void bput5_slow(struct fastbuf *f, u64 o)
180 #ifdef CPU_BIG_ENDIAN
189 void bread_slow(struct fastbuf *f, void *b, uns l)
193 uns k = f->bstop - f->bptr;
198 k = f->bstop - f->bptr;
200 die("bread on %s: file exhausted", f->name);
204 memcpy(b, f->bptr, k);
211 void bwrite_slow(struct fastbuf *f, void *b, uns l)
215 uns k = f->bufend - f->bptr;
220 k = f->bufend - f->bptr;
224 memcpy(f->bptr, b, k);
231 byte * /* Non-standard */
232 bgets(struct fastbuf *f, byte *b, uns l)
242 if (k == '\n' || k == EOF)
250 die("%s: Line too long", f->name);