]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.c
26df0c2f9afb17d202ac08d6809948b7bd633973
[libucw.git] / lib / fastbuf.c
1 /*
2  *      UCW Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 void bclose(struct fastbuf *f)
17 {
18   if (f)
19     {
20       bflush(f);
21       if (f->close)
22         f->close(f);
23     }
24 }
25
26 void bflush(struct fastbuf *f)
27 {
28   if (f->bptr > f->bstop)
29     f->spout(f);
30   else if (f->bstop > f->buffer)
31     f->bptr = f->bstop = f->buffer;
32 }
33
34 inline void bsetpos(struct fastbuf *f, sh_off_t pos)
35 {
36   /* We can optimize seeks only when reading */
37   if (pos >= f->pos - (f->bstop - f->buffer) && pos <= f->pos)
38     f->bptr = f->bstop + (pos - f->pos);
39   else
40     {
41       bflush(f);
42       if (!f->seek || !f->seek(f, pos, SEEK_SET))
43         die("bsetpos: stream not seekable");
44     }
45 }
46
47 void bseek(struct fastbuf *f, sh_off_t pos, int whence)
48 {
49   switch (whence)
50     {
51     case SEEK_SET:
52       return bsetpos(f, pos);
53     case SEEK_CUR:
54       return bsetpos(f, btell(f) + pos);
55     case SEEK_END:
56       bflush(f);
57       if (!f->seek || !f->seek(f, pos, SEEK_END))
58         die("bseek: stream not seekable");
59       break;
60     default:
61       die("bseek: invalid whence=%d", whence);
62     }
63 }
64
65 int bgetc_slow(struct fastbuf *f)
66 {
67   if (f->bptr < f->bstop)
68     return *f->bptr++;
69   if (!f->refill(f))
70     return -1;
71   return *f->bptr++;
72 }
73
74 int bpeekc_slow(struct fastbuf *f)
75 {
76   if (f->bptr < f->bstop)
77     return *f->bptr;
78   if (!f->refill(f))
79     return -1;
80   return *f->bptr;
81 }
82
83 void bputc_slow(struct fastbuf *f, uns c)
84 {
85   if (f->bptr >= f->bufend)
86     f->spout(f);
87   *f->bptr++ = c;
88 }
89
90 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check)
91 {
92   uns total = 0;
93   while (l)
94     {
95       uns k = f->bstop - f->bptr;
96
97       if (!k)
98         {
99           f->refill(f);
100           k = f->bstop - f->bptr;
101           if (!k)
102             break;
103         }
104       if (k > l)
105         k = l;
106       memcpy(b, f->bptr, k);
107       f->bptr += k;
108       b = (byte *)b + k;
109       l -= k;
110       total += k;
111     }
112   if (check && l)
113     if (check == 2)
114       die("breada: short read");
115     else if (total)
116       die("breadb: short read");
117   return total;
118 }
119
120 void bwrite_slow(struct fastbuf *f, void *b, uns l)
121 {
122   while (l)
123     {
124       uns k = f->bufend - f->bptr;
125
126       if (!k)
127         {
128           f->spout(f);
129           k = f->bufend - f->bptr;
130         }
131       if (k > l)
132         k = l;
133       memcpy(f->bptr, b, k);
134       f->bptr += k;
135       b = (byte *)b + k;
136       l -= k;
137     }
138 }
139
140 void
141 bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
142 {
143   while (l)
144     {
145       byte *fptr, *tptr;
146       uns favail, tavail, n;
147
148       favail = bdirect_read_prepare(f, &fptr);
149       if (!favail)
150         {
151           if (l == ~0U)
152             return;
153           die("bbcopy: source exhausted");
154         }
155       tavail = bdirect_write_prepare(t, &tptr);
156       n = MIN(l, favail);
157       n = MIN(n, tavail);
158       memcpy(tptr, fptr, n);
159       bdirect_read_commit(f, fptr + n);
160       bdirect_write_commit(t, tptr + n);
161       if (l != ~0U)
162         l -= n;
163     }
164 }
165
166 int
167 bconfig(struct fastbuf *f, uns item, int value)
168 {
169   return f->config ? f->config(f, item, value) : -1;
170 }
171
172 void
173 brewind(struct fastbuf *f)
174 {
175   bflush(f);
176   bsetpos(f, 0);
177 }
178
179 int
180 bskip_slow(struct fastbuf *f, uns len)
181 {
182   while (len)
183     {
184       byte *buf;
185       uns l = bdirect_read_prepare(f, &buf);
186       if (!l)
187         return 0;
188       l = MIN(l, len);
189       bdirect_read_commit(f, buf+l);
190       len -= l;
191     }
192   return 1;
193 }
194
195 sh_off_t
196 bfilesize(struct fastbuf *f)
197 {
198   if (!f)
199     return 0;
200   sh_off_t pos = btell(f);
201   bflush(f);
202   if (!f->seek(f, 0, SEEK_END))
203     return -1;
204   sh_off_t len = btell(f);
205   bsetpos(f, pos);
206   return len;
207 }