2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: bufio.c /main/5 1996/11/01 10:11:35 drk $
26 * Copyright 1991 Massachusetts Institute of Technology
28 * Permission to use, copy, modify, distribute, and sell this software and its
29 * documentation for any purpose is hereby granted without fee, provided that
30 * the above copyright notice appear in all copies and that both that
31 * copyright notice and this permission notice appear in supporting
32 * documentation, and that the name of M.I.T. not be used in advertising or
33 * publicity pertaining to distribution of the software without specific,
34 * written prior permission. M.I.T. makes no representations about the
35 * suitability of this software for any purpose. It is provided "as is"
36 * without express or implied warranty.
38 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
40 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
42 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
43 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 * Author: Keith Packard, MIT X Consortium
49 /* #include "fontmisc.h" */
63 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
66 #define FileDes(f) ((intptr_t) (f)->hidden)
67 #define CompressFileDes(f) (((CECompressInfoPtr) (f)->hidden)->fd)
68 #define CompressSize(f) (((CECompressInfoPtr) (f)->hidden)->size)
79 curoff = f->bufp - f->buffer;
80 fileoff = curoff + f->left;
81 if (curoff + count <= fileoff) {
85 todo = count - (fileoff - curoff);
86 if (lseek (FileDes(f), todo, 1) == -1) {
93 fileoff = read (FileDes(f), f->buffer, curoff);
113 cnt = f->bufp - f->buffer;
115 f->left = BUFFILESIZE;
116 if (write (FileDes(f), f->buffer, cnt) != cnt)
122 _DtHelpCeBufFileOpenWr (int fd)
126 f = _DtHelpCeBufFileCreate ((char *) (intptr_t) fd, BufFileRawFlush, NULL, _DtHelpCeBufFileFlush);
128 f->left = BUFFILESIZE;
132 #ifdef obsolete_function
133 _DtHelpCeBufFileWrite (
141 if (BufFilePut (*b++, f) == BUFFILEEOF)
149 _DtHelpCeBufFileFlush (BufFilePtr f, int doClose)
151 if (f->bufp != f->buffer)
152 (*f->io) (BUFFILEEOF, f);
155 return (close (FileDes(f)));
160 /*****************************************************************************
162 *****************************************************************************/
163 /*****************************************************************************
164 * Routines working on a File descriptor
165 *****************************************************************************/
167 FdRawRead (BufFilePtr f)
171 left = read (FileDes(f), f->buffer, BUFFILESIZE);
177 f->bufp = f->buffer + 1;
191 /*****************************************************************************
192 * Routines working on a Raw Compressed file
193 *****************************************************************************/
195 CompressRawRead (BufFilePtr f)
200 left = read (CompressFileDes(f), f->buffer,
201 MIN(CompressSize(f),BUFFILESIZE));
207 CompressSize(f) -= left;
209 f->bufp = f->buffer + 1;
219 close (CompressFileDes (f));
224 /*****************************************************************************
225 * Routines working on a Pipe
226 *****************************************************************************/
227 /*****************************************************************************
228 * Function: int RdPipeStream (BufFilePtr f)
234 *****************************************************************************/
236 RdPipeStream (BufFilePtr f)
240 left = fread(f->buffer, 1, BUFFILESIZE, FileStream(f));
248 clearerr(FileStream(f));
250 f->bufp = f->buffer + 1;
254 /*********************************************************************
255 * Procedure: int ClosePipeStream (BufFilePtr f);
261 ********************************************************************/
268 pclose(FileStream(f));
273 /*****************************************************************************
274 * Semi-Public Routines
275 *****************************************************************************/
276 /*****************************************************************************
277 * Function: BufFilePtr _DtHelpCeBufFileCreate (char *hidden,
278 * int (*io)(), int (*skip)(),
281 * Returns: A pointer to malloc'ed memory or NULL.
283 * Purpose: Create a buffered i/o mechanism.
285 *****************************************************************************/
287 _DtHelpCeBufFileCreate (
295 f = (BufFilePtr) malloc (sizeof *f);
307 /*****************************************************************************
308 * Function: BufFilePtr _DtHelpCeBufFileRdWithFd (int fd)
310 * Returns: A pointer to malloc'ed memory or NULL.
312 * Purpose: Create a buffered i/o mechanism using a file descriptor
313 * as private data and attaching a raw read to the i/o
316 *****************************************************************************/
318 _DtHelpCeBufFileRdWithFd (int fd)
320 return _DtHelpCeBufFileCreate ((char *) (intptr_t) fd, FdRawRead, BufFileRawSkip, FdClose);
323 /*****************************************************************************
324 * Function: BufFilePtr _DtHelpCeBufFileRdRawZ (CECompressInfoPtr file)
326 * Returns: A pointer to malloc'ed memory or NULL.
328 * Purpose: Create a buffered i/o mechanism using a file descriptor
329 * as private data and attaching a raw read of compressed
330 * data to the i/o routine.
332 *****************************************************************************/
334 _DtHelpCeBufFileRdRawZ (CECompressInfoPtr file)
336 return _DtHelpCeBufFileCreate ((char *) file, CompressRawRead, NULL,
340 /*****************************************************************************
341 * Function: void _DtHelpCeBufFileClose (BufFilePtr file, int doClose)
345 * Purpose: Calls the close routine associated with the pointer.
346 * Frees the BufFile information.
348 *****************************************************************************/
350 _DtHelpCeBufFileClose (
354 (void) (*f->close) (f, doClose);
358 /*****************************************************************************
359 * Function: void _DtHelpCeBufFileRd (BufFilePtr file, int doClose)
363 * Purpose: Calls the close routine associated with the pointer.
364 * Frees the BufFile information.
366 *****************************************************************************/
384 /*****************************************************************************
385 * Function: BufFilePtr _DtHelpCeCreatePipeBufFile (FILE *stream)
387 * Returns: A pointer to malloc'ed memory or NULL.
389 * Purpose: Create a buffered i/o mechanism using a pipe descriptor
390 * as private data and attaching a raw read to the i/o
393 *****************************************************************************/
395 _DtHelpCeCreatePipeBufFile (FILE *stream)
397 return _DtHelpCeBufFileCreate ((char *) stream,
398 RdPipeStream, NULL, ClosePipeStream);