]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
8195250e78e54c699d1c41079188c200dfb28a29
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast File Buffering
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #ifndef EOF
8 #include <stdio.h>
9 #endif
10
11 struct fastbuf {
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 */
19 };
20
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);
25
26 void bseek(struct fastbuf *f, uns pos, int whence);
27 void bsetpos(struct fastbuf *f, uns pos);
28
29 extern inline uns btell(struct fastbuf *f)
30 {
31   return f->pos + (f->bptr - f->buffer);
32 }
33
34 int bgetc_slow(struct fastbuf *f);
35 extern inline int bgetc(struct fastbuf *f)
36 {
37   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
38 }
39
40 extern inline void bungetc(struct fastbuf *f, byte c)
41 {
42   *--f->bptr = c;
43 }
44
45 void bputc_slow(struct fastbuf *f, byte c);
46 extern inline void bputc(struct fastbuf *f, byte c)
47 {
48   if (f->bptr < f->bufend)
49     *f->bptr++ = c;
50   else
51     bputc_slow(f, c);
52 }
53
54 word bgetw_slow(struct fastbuf *f);
55 extern inline word bgetw(struct fastbuf *f)
56 {
57 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
58   word w;
59   if (f->bptr + 2 <= f->bstop)
60     {
61       w = * ((word *) f->bptr);
62       f->bptr += 2;
63       return w;
64     }
65   else
66     return bgetw_slow(f);
67 #else
68   word w = bgetc(f);
69 #ifdef CPU_BIG_ENDIAN
70   return (w << 8) | bgetc(f);
71 #else
72   return w | (bgetc(f) << 8);
73 #endif
74 #endif
75 }
76
77 ulg bgetl_slow(struct fastbuf *f);
78 extern inline ulg bgetl(struct fastbuf *f)
79 {
80 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
81   ulg l;
82   if (f->bptr + 4 <= f->bstop)
83     {
84       l = * ((ulg *) f->bptr);
85       f->bptr += 4;
86       return l;
87     }
88   else
89     return bgetl_slow(f);
90 #else
91   ulg l = bgetc(f);
92 #ifdef CPU_BIG_ENDIAN
93   l = (l << 8) | bgetc(f);
94   l = (l << 8) | bgetc(f);
95   return (l << 8) | bgetc(f);
96 #else
97   l = (bgetc(f) << 8) | l;
98   l = (bgetc(f) << 16) | l;
99   return (bgetc(f) << 24) | l;
100 #endif
101 #endif
102 }
103
104 void bputw_slow(struct fastbuf *f, word w);
105 extern inline void bputw(struct fastbuf *f, word w)
106 {
107 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
108   if (f->bptr + 2 <= f->bufend)
109     {
110       * ((word *) f->bptr) = w;
111       f->bptr += 2;
112     }
113   else
114     bputw_slow(f, w);
115 #else
116 #ifdef CPU_BIG_ENDIAN
117   bputc(f, w >> 8);
118   bputc(f, w);
119 #else
120   bputc(f, w);
121   bputc(f, w >> 8);
122 #endif
123 #endif
124 }
125
126 void bputl_slow(struct fastbuf *f, ulg l);
127 extern inline void bputl(struct fastbuf *f, ulg l)
128 {
129 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
130   if (f->bptr + 4 <= f->bufend)
131     {
132       * ((ulg *) f->bptr) = l;
133       f->bptr += 4;
134     }
135   else
136     bputl_slow(f, l);
137 #else
138 #ifdef CPU_BIG_ENDIAN
139   bputc(f, l >> 24);
140   bputc(f, l >> 16);
141   bputc(f, l >> 8);
142   bputc(f, l);
143 #else
144   bputc(f, l);
145   bputc(f, l >> 8);
146   bputc(f, l >> 16);
147   bputc(f, l >> 24);
148 #endif
149 #endif
150 }
151
152 void bread_slow(struct fastbuf *f, void *b, uns l);
153 extern inline void bread(struct fastbuf *f, void *b, uns l)
154 {
155   if (f->bptr + l <= f->bstop)
156     {
157       memcpy(b, f->bptr, l);
158       f->bptr += l;
159     }
160   else
161     bread_slow(f, b, l);
162 }
163
164 void bwrite_slow(struct fastbuf *f, void *b, uns l);
165 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
166 {
167   if (f->bptr + l <= f->bufend)
168     {
169       memcpy(f->bptr, b, l);
170       f->bptr += l;
171     }
172   else
173     bwrite_slow(f, b, l);
174 }
175
176 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
177
178 extern inline byte *                    /* Non-standard */
179 bgets(struct fastbuf *f, byte *b, uns l)
180 {
181   byte *e = b + l - 1;
182   int k;
183
184   k = bgetc(f);
185   if (k == EOF)
186     return NULL;
187   while (b < e)
188     {
189       if (k == '\n' || k == EOF)
190         {
191           *b = 0;
192           return b;
193         }
194       *b++ = k;
195       k = bgetc(f);
196     }
197   die("%s: Line too long", f->name);
198 }
199
200 extern inline void
201 bputs(struct fastbuf *f, byte *b)
202 {
203   while (*b)
204     bputc(f, *b++);
205 }
206
207 extern inline void
208 bputsn(struct fastbuf *f, byte *b)
209 {
210   bputs(f, b);
211   bputc(f, '\n');
212 }
213