]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Killed `typedef unsigned long ulg' and replaced all refs to ulg by either
[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 int bpeekc_slow(struct fastbuf *f);
41 extern inline int bpeekc(struct fastbuf *f)
42 {
43   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
44 }
45
46 extern inline void bungetc(struct fastbuf *f, byte c)
47 {
48   *--f->bptr = c;
49 }
50
51 void bputc_slow(struct fastbuf *f, byte c);
52 extern inline void bputc(struct fastbuf *f, byte c)
53 {
54   if (f->bptr < f->bufend)
55     *f->bptr++ = c;
56   else
57     bputc_slow(f, c);
58 }
59
60 word bgetw_slow(struct fastbuf *f);
61 extern inline word bgetw(struct fastbuf *f)
62 {
63   word w;
64   if (f->bptr + 2 <= f->bstop)
65     {
66       byte *p = f->bptr;
67 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
68       w = * ((word *) p);
69 #else
70 #ifdef CPU_BIG_ENDIAN
71       w = (p[0] << 8) | p[1];
72 #else
73       w = (p[1] << 8) | p[0];
74 #endif
75 #endif
76       f->bptr += 2;
77       return w;
78     }
79   else
80     return bgetw_slow(f);
81 }
82
83 u32 bgetl_slow(struct fastbuf *f);
84 extern inline u32 bgetl(struct fastbuf *f)
85 {
86   u32 l;
87   if (f->bptr + 4 <= f->bstop)
88     {
89       byte *p = f->bptr;
90 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
91       l = * ((u32 *) p);
92 #else
93 #ifdef CPU_BIG_ENDIAN
94       l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
95 #else
96       l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
97 #endif
98 #endif
99       f->bptr += 4;
100       return l;
101     }
102   else
103     return bgetl_slow(f);
104 }
105
106 void bputw_slow(struct fastbuf *f, word w);
107 extern inline void bputw(struct fastbuf *f, word w)
108 {
109   if (f->bptr + 2 <= f->bufend)
110     {
111       byte *p = f->bptr;
112 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
113       * ((word *) p) = w;
114 #else
115 #ifdef CPU_BIG_ENDIAN
116       p[0] = w >> 8U;
117       p[1] = w;
118 #else
119       p[1] = w >> 8U;
120       p[0] = w;
121 #endif
122 #endif
123       f->bptr += 2;
124     }
125   else
126     bputw_slow(f, w);
127 }
128
129 void bputl_slow(struct fastbuf *f, u32 l);
130 extern inline void bputl(struct fastbuf *f, u32 l)
131 {
132   if (f->bptr + 4 <= f->bufend)
133     {
134       byte *p = f->bptr;
135 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
136       * ((u32 *) p) = l;
137 #else
138 #ifdef CPU_BIG_ENDIAN
139       p[0] = l >> 24U;
140       p[1] = l >> 16U;
141       p[2] = l >> 8U;
142       p[3] = l;
143 #else
144       p[3] = l >> 24U;
145       p[2] = l >> 16U;
146       p[1] = l >> 8U;
147       p[0] = l;
148 #endif
149 #endif
150       f->bptr += 4;
151     }
152   else
153     bputl_slow(f, l);
154 }
155
156 void bread_slow(struct fastbuf *f, void *b, uns l);
157 extern inline void bread(struct fastbuf *f, void *b, uns l)
158 {
159   if (f->bptr + l <= f->bstop)
160     {
161       memcpy(b, f->bptr, l);
162       f->bptr += l;
163     }
164   else
165     bread_slow(f, b, l);
166 }
167
168 void bwrite_slow(struct fastbuf *f, void *b, uns l);
169 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
170 {
171   if (f->bptr + l <= f->bufend)
172     {
173       memcpy(f->bptr, b, l);
174       f->bptr += l;
175     }
176   else
177     bwrite_slow(f, b, l);
178 }
179
180 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
181 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
182
183 extern inline void
184 bputs(struct fastbuf *f, byte *b)
185 {
186   bwrite(f, b, strlen(b));
187 }
188
189 extern inline void
190 bputsn(struct fastbuf *f, byte *b)
191 {
192   bputs(f, b);
193   bputc(f, '\n');
194 }
195