bufio.c revision e24f450b
1/*
2
3Copyright 1991, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29/*
30 * Author:  Keith Packard, MIT X Consortium
31 */
32
33
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37#include <X11/Xos.h>
38#include "fontmisc.h"
39#include "bufio.h"
40#include <errno.h>
41
42static BufFilePtr
43BufFileCreate(char *private,
44              int (*input)(BufFilePtr),
45              int (*output)(int, BufFilePtr),
46              int (*skip)(BufFilePtr, int),
47              int (*close)(BufFilePtr, int))
48{
49    BufFilePtr f;
50
51    f = malloc(sizeof *f);
52    if (!f)
53        return 0;
54    f->private = private;
55    f->bufp = f->buffer;
56    f->left = 0;
57    f->input = input;
58    f->output = output;
59    f->skip = skip;
60    f->eof = 0;
61    f->close = close;
62    return f;
63}
64
65#define FileDes(f)  ((int)(long) (f)->private)
66
67static int
68BufFileRawFill(BufFilePtr f)
69{
70    int left;
71
72    left = read(FileDes(f), (char *) f->buffer, BUFFILESIZE);
73    if (left <= 0) {
74        f->left = 0;
75        return BUFFILEEOF;
76    }
77    f->left = left - 1;
78    f->bufp = f->buffer + 1;
79    return f->buffer[0];
80}
81
82static int
83BufFileRawSkip(BufFilePtr f, int count)
84{
85    int curoff = f->bufp - f->buffer;
86    int fileoff = curoff + f->left;
87
88    if (curoff + count <= fileoff) {
89        f->bufp += count;
90        f->left -= count;
91    }
92    else {
93        int todo = count - (fileoff - curoff);
94
95        if (lseek(FileDes(f), todo, 1) == -1) {
96            if (errno != ESPIPE)
97                return BUFFILEEOF;
98            while (todo) {
99                curoff = BUFFILESIZE;
100                if (curoff > todo)
101                    curoff = todo;
102                fileoff = read(FileDes(f), (char *) f->buffer, curoff);
103                if (fileoff <= 0)
104                    return BUFFILEEOF;
105                todo -= fileoff;
106            }
107        }
108        f->left = 0;
109    }
110    return count;
111}
112
113static int
114BufFileRawClose(BufFilePtr f, int doClose)
115{
116    if (doClose)
117        close(FileDes(f));
118    return 1;
119}
120
121BufFilePtr
122BufFileOpenRead(int fd)
123{
124#if defined (WIN32)
125    /* hv: I'd bet WIN32 has the same effect here */
126    setmode(fd, O_BINARY);
127#endif
128    return BufFileCreate((char *) (long) fd, BufFileRawFill, 0, BufFileRawSkip,
129                         BufFileRawClose);
130}
131
132static int
133BufFileRawFlush(int c, BufFilePtr f)
134{
135    int cnt;
136
137    if (c != BUFFILEEOF)
138        *f->bufp++ = c;
139    cnt = f->bufp - f->buffer;
140    f->bufp = f->buffer;
141    f->left = BUFFILESIZE;
142    if (write(FileDes(f), (char *) f->buffer, cnt) != cnt)
143        return BUFFILEEOF;
144    return c;
145}
146
147static int
148BufFileFlush(BufFilePtr f, int doClose)
149{
150    if (f->bufp != f->buffer)
151        return (*f->output) (BUFFILEEOF, f);
152    return 0;
153}
154
155BufFilePtr
156BufFileOpenWrite(int fd)
157{
158    BufFilePtr f;
159
160#if defined(WIN32)
161    /* hv: I'd bet WIN32 has the same effect here */
162    setmode(fd, O_BINARY);
163#endif
164    f = BufFileCreate((char *) (long) fd, 0, BufFileRawFlush, 0, BufFileFlush);
165    if (f != NULL) {
166        f->bufp = f->buffer;
167        f->left = BUFFILESIZE;
168    }
169    return f;
170}
171
172int
173BufFileWrite(BufFilePtr f, char *b, int n)
174{
175    int cnt = n;
176
177    while (cnt--) {
178        if (BufFilePut(*b++, f) == BUFFILEEOF)
179            return BUFFILEEOF;
180    }
181    return n;
182}
183
184int
185BufFileClose(BufFilePtr f, int doClose)
186{
187    int ret = (*f->close) (f, doClose);
188
189    free(f);
190    return ret;
191}
192