]> git.sesse.net Git - rdpsrv/blob - Xserver/lib/font/fontfile/bufio.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / lib / font / fontfile / bufio.c
1 /* $XConsortium: bufio.c,v 1.8 94/04/17 20:17:00 gildea Exp $ */
2 /* $XFree86: xc/lib/font/fontfile/bufio.c,v 3.0 1994/12/17 09:41:39 dawes Exp $ */
3
4 /*
5
6 Copyright (c) 1991  X Consortium
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
26
27 Except as contained in this notice, the name of the X Consortium shall
28 not be used in advertising or otherwise to promote the sale, use or
29 other dealings in this Software without prior written authorization
30 from the X Consortium.
31
32 */
33
34 /*
35  * Author:  Keith Packard, MIT X Consortium
36  */
37
38
39 #include <X11/Xos.h>
40 #include <fontmisc.h>
41 #include <bufio.h>
42 #include <errno.h>
43 #ifdef X_NOT_STDC_ENV
44 extern int errno;
45 #endif
46
47 BufFilePtr
48 BufFileCreate (private, io, skip, close)
49     char    *private;
50     int     (*io)();
51     int     (*skip)();
52     int     (*close)();
53 {
54     BufFilePtr  f;
55
56     f = (BufFilePtr) xalloc (sizeof *f);
57     if (!f)
58         return 0;
59     f->private = private;
60     f->bufp = f->buffer;
61     f->left = 0;
62     f->io = io;
63     f->skip = skip;
64     f->close = close;
65     return f;
66 }
67
68 #define FileDes(f)  ((int) (f)->private)
69
70 static int
71 BufFileRawFill (f)
72     BufFilePtr  f;
73 {
74     int left;
75
76     left = read (FileDes(f), (char *)f->buffer, BUFFILESIZE);
77     if (left <= 0) {
78         f->left = 0;
79         return BUFFILEEOF;
80     }
81     f->left = left - 1;
82     f->bufp = f->buffer + 1;
83     return f->buffer[0];
84 }
85
86 static int
87 BufFileRawSkip (f, count)
88     BufFilePtr  f;
89     int         count;
90 {
91     int     curoff;
92     int     fileoff;
93     int     todo;
94
95     curoff = f->bufp - f->buffer;
96     fileoff = curoff + f->left;
97     if (curoff + count <= fileoff) {
98         f->bufp += count;
99         f->left -= count;
100     } else {
101         todo = count - (fileoff - curoff);
102         if (lseek (FileDes(f), todo, 1) == -1) {
103             if (errno != ESPIPE)
104                 return BUFFILEEOF;
105             while (todo) {
106                 curoff = BUFFILESIZE;
107                 if (curoff > todo)
108                     curoff = todo;
109                 fileoff = read (FileDes(f), (char *)f->buffer, curoff);
110                 if (fileoff <= 0)
111                     return BUFFILEEOF;
112                 todo -= fileoff;
113             }
114         }
115         f->left = 0;
116     }
117     return count;
118 }
119
120 static int
121 BufFileRawClose (f, doClose)
122     BufFilePtr  f;
123 {
124     if (doClose)
125         close (FileDes (f));
126     return 1;
127 }
128
129 BufFilePtr
130 BufFileOpenRead (fd)
131     int fd;
132 {
133 #ifdef __EMX__
134     /* hv: I'd bet WIN32 has the same effect here */
135     setmode(fd,O_BINARY);
136 #endif
137     return BufFileCreate ((char *) fd, BufFileRawFill, BufFileRawSkip, BufFileRawClose);
138 }
139
140 static
141 BufFileRawFlush (c, f)
142     int         c;
143     BufFilePtr  f;
144 {
145     int cnt;
146
147     if (c != BUFFILEEOF)
148         *f->bufp++ = c;
149     cnt = f->bufp - f->buffer;
150     f->bufp = f->buffer;
151     f->left = BUFFILESIZE;
152     if (write (FileDes(f), (char *)f->buffer, cnt) != cnt)
153         return BUFFILEEOF;
154     return c;
155 }
156
157 BufFilePtr
158 BufFileOpenWrite (fd)
159     int fd;
160 {
161     BufFilePtr  f;
162
163 #ifdef __EMX__
164     /* hv: I'd bet WIN32 has the same effect here */
165     setmode(fd,O_BINARY);
166 #endif
167     f = BufFileCreate ((char *) fd, BufFileRawFlush, 0, BufFileFlush);
168     f->bufp = f->buffer;
169     f->left = BUFFILESIZE;
170     return f;
171 }
172
173 BufFileRead (f, b, n)
174     BufFilePtr  f;
175     char        *b;
176     int         n;
177 {
178     int     c, cnt;
179     cnt = n;
180     while (cnt--) {
181         c = BufFileGet (f);
182         if (c == BUFFILEEOF)
183             break;
184         *b++ = c;
185     }
186     return n - cnt - 1;
187 }
188
189 BufFileWrite (f, b, n)
190     BufFilePtr  f;
191     char        *b;
192     int         n;
193 {
194     int     cnt;
195     cnt = n;
196     while (cnt--) {
197         if (BufFilePut (*b++, f) == BUFFILEEOF)
198             return BUFFILEEOF;
199     }
200     return n;
201 }
202
203 int
204 BufFileFlush (f)
205     BufFilePtr  f;
206 {
207     if (f->bufp != f->buffer)
208         (*f->io) (BUFFILEEOF, f);
209 }
210
211 int
212 BufFileClose (f, doClose)
213     BufFilePtr  f;
214 {
215     (void) (*f->close) (f, doClose);
216     xfree (f);
217 }
218
219 int
220 BufFileFree (f)
221     BufFilePtr  f;
222 {
223     xfree (f);
224 }