2 * Sherlock Library -- Fast File Buffering
4 * (c) 1997 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 uns pos; /* Position of bptr in the file */
17 uns 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, uns pos, int whence);
27 void bsetpos(struct fastbuf *f, uns pos);
29 extern inline uns 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 ulg bgetl_slow(struct fastbuf *f);
84 extern inline ulg 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 void bputw_slow(struct fastbuf *f, word w);
107 extern inline void bputw(struct fastbuf *f, word w)
109 if (f->bptr + 2 <= f->bufend)
112 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
115 #ifdef CPU_BIG_ENDIAN
129 void bputl_slow(struct fastbuf *f, ulg l);
130 extern inline void bputl(struct fastbuf *f, ulg l)
132 if (f->bptr + 4 <= f->bufend)
135 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
138 #ifdef CPU_BIG_ENDIAN
156 void bread_slow(struct fastbuf *f, void *b, uns l);
157 extern inline void bread(struct fastbuf *f, void *b, uns l)
159 if (f->bptr + l <= f->bstop)
161 memcpy(b, f->bptr, l);
168 void bwrite_slow(struct fastbuf *f, void *b, uns l);
169 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
171 if (f->bptr + l <= f->bufend)
173 memcpy(f->bptr, b, l);
177 bwrite_slow(f, b, l);
180 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
181 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
184 bputs(struct fastbuf *f, byte *b)
186 bwrite(f, b, strlen(b));
190 bputsn(struct fastbuf *f, byte *b)