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