Initial revision
[LeanCalc.git] / calc / calc / file.h
1 /*
2  * file - file I/O routines callable by users
3  *
4  * Copyright (C) 1999  David I. Bell and Landon Curt Noll
5  *
6  * Primary author:  David I. Bell
7  *
8  * Calc is open software; you can redistribute it and/or modify it under
9  * the terms of the version 2.1 of the GNU Lesser General Public License
10  * as published by the Free Software Foundation.
11  *
12  * Calc is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
15  * Public License for more details.
16  *
17  * A copy of version 2.1 of the GNU Lesser General Public License is
18  * distributed with calc under the filename COPYING-LGPL.  You should have
19  * received a copy with calc; if not, write to Free Software Foundation, Inc.
20  * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * @(#) $Revision$
23  * @(#) $Id$
24  * @(#) $Source$
25  *
26  * Under source code control:   1996/05/24 05:55:58
27  * File existed as early as:    1996
28  *
29  * chongo <was here> /\oo/\     http://www.isthe.com/chongo/
30  * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
31  */
32
33
34 #if !defined(__FILE_H__)
35 #define __FILE_H__
36
37
38 #if defined(CALC_SRC)   /* if we are building from the calc source tree */
39 # include "calc/have_fpos.h"
40 #else
41 # include <calc/have_fpos.h>
42 #endif
43
44
45 /*
46  * Definition of opened files.
47  */
48 typedef struct {
49         FILEID id;              /* id to identify this file */
50         FILE *fp;               /* real file structure for I/O */
51         dev_t dev;              /* file device */
52         ino_t inode;            /* file inode */
53         char *name;             /* file name */
54         BOOL reading;           /* TRUE if opened for reading */
55         BOOL writing;           /* TRUE if opened for writing */
56         char action;            /* most recent use for 'r', 'w' or 0 */
57         char mode[sizeof("rb+")];/* open mode */
58 } FILEIO;
59
60
61 /*
62  * fgetpos/fsetpos vs fseek/ftell interface
63  *
64  * f_seek_set(FILE *stream, FILEPOS *loc)
65  *      Seek loc bytes from the beginning of the open file, stream.
66  *
67  * f_tell(FILE *stream, FILEPOS *loc)
68  *      Set loc to bytes from the beinning of the open file, stream.
69  *
70  * We assume that if your system does not have fgetpos/fsetpos,
71  * then it will have a FILEPOS that is a scalar type (e.g., long).
72  * Some obscure systems without fgetpos/fsetpos may not have a simple
73  * scalar type.  In these cases the f_tell macro below will fail.
74  */
75 #if defined(HAVE_FPOS)
76
77 #define f_seek_set(stream, loc) fsetpos((FILE*)(stream), (FILEPOS*)(loc))
78 #define f_tell(stream, loc) fgetpos((FILE*)(stream), (FILEPOS*)(loc))
79
80 #else
81
82 #define f_seek_set(stream, loc)  \
83         fseek((FILE*)(stream), *(FILEPOS*)(loc), SEEK_SET)
84 #define f_tell(stream, loc) (*((FILEPOS*)(loc)) = ftell((FILE*)(stream)))
85
86 #endif
87
88
89 /*
90  * external functions
91  */
92 extern FILEIO * findid(FILEID id, int writable);
93 extern int fgetposid(FILEID id, FILEPOS *ptr);
94 extern int fsetposid(FILEID id, FILEPOS *ptr);
95 extern int get_open_siz(FILE *fp, ZVALUE *res);
96 extern char* findfname(FILEID);
97
98
99 #endif /* !__FILE_H__ */