Upload 2.0.2
[physicsfs] / extras / abs-file.h
1 /*
2  * stdio/physfs abstraction layer 2003-04-02
3  *
4  * Adam D. Moss <adam@gimp.org> <aspirin@icculus.org>
5  *
6  * These wrapper macros and functions are designed to allow a program
7  * to perform file I/O with identical semantics and syntax regardless
8  * of whether PhysicsFS is being used or not.
9  */
10 #ifndef _ABS_FILE_H
11 #define _ABS_FILE_H
12 /*
13 PLEASE NOTE: This license applies to abs-file.h ONLY (to make it clear that
14 you may embed this wrapper code within commercial software); PhysicsFS itself
15 is (at the time of writing) released under a different license with
16 additional restrictions.
17
18 Copyright (C) 2002-2003 Adam D. Moss (the "Author").  All Rights Reserved.
19
20 Permission is hereby granted, free of charge, to any person obtaining a copy
21 of this software and associated documentation files (the "Software"), to deal
22 in the Software without restriction, including without limitation the rights
23 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 copies of the Software, and to permit persons to whom the Software is fur-
25 nished to do so, subject to the following conditions:
26
27 The above copyright notice and this permission notice shall be included in
28 all copies or substantial portions of the Software.
29
30 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
32 NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
33 AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
34 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
35 NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
37 Except as contained in this notice, the name of the Author of the
38 Software shall not be used in advertising or otherwise to promote the sale,
39 use or other dealings in this Software without prior written authorization
40 from the Author.
41 */
42
43 #include <stdlib.h>
44 #include <stdio.h>
45
46 /*
47  * API:
48  *
49  * Macro/function       use like stdio equivalent...
50  * --------------       ----------------------------
51  * MY_FILETYPE          FILE
52  * MY_OPEN_FOR_READ     fopen(..., "rb")
53  * MY_READ              fread(...)
54  * MY_CLOSE             fclose(...)
55  * MY_GETC              fgetc(...)
56  * MY_GETS              fgets(...)
57  * MY_ATEOF             feof(...)
58  * MY_TELL              ftell(...)
59  * MY_SEEK              fseek(..., SEEK_SET)
60  * MY_REWIND            rewind(...)
61  * MY_SETBUFFER         (not a standard for stdio, does nothing there)
62  */
63
64 /*
65  * Important DEFINEs:
66  *   It is important to define these consistantly across the various
67  *   compilation modules of your program if you wish to exchange file
68  *   handles between them.
69  *
70  *   USE_PHYSFS: Define USE_PHYSFS if PhysicsFS is being used; note that if
71  *     you do intend to use PhysicsFS then you will still need to initialize
72  *     PhysicsFS yourself and set up its search-paths.
73  *
74  * Optional DEFINEs:
75  *
76  *   PHYSFS_DEFAULT_READ_BUFFER <bytes>: If set then abs-file.h sets the
77  *     PhysicsFS buffer size to this value whenever you open a file.  You
78  *     may over-ride this on a per-filehandle basis by using the
79  *     MY_SETBUFFER() macro (which simply does nothing when not using
80  *     PhysicsFS).  If you have not defined this value explicitly then
81  *     abs-file.h will default to the same default buffer size as used by
82  *     stdio if it can be determined, or 8192 bytes otherwise.
83  */
84 #ifndef PHYSFS_DEFAULT_READ_BUFFER
85 #ifdef BUFSIZ
86 #define PHYSFS_DEFAULT_READ_BUFFER BUFSIZ
87 #else
88 #define PHYSFS_DEFAULT_READ_BUFFER 8192
89 #endif
90 #endif
91
92 #ifdef USE_PHYSFS
93
94 #include <physfs.h>
95 #define MY_FILETYPE PHYSFS_File
96 #define MY_SETBUFFER(fp,size) PHYSFS_setBuffer(fp,size)
97 #define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n)
98 #if PHYSFS_DEFAULT_READ_BUFFER
99 static MY_FILETYPE* MY_OPEN_FOR_READ(const char *const filename)
100 {
101   MY_FILETYPE *const file = PHYSFS_openRead(filename);
102   if (file) {
103     MY_SETBUFFER(file, PHYSFS_DEFAULT_READ_BUFFER);
104   }
105   return file;
106 }
107 #else
108 #define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn)
109 #endif
110 static int MY_GETC(MY_FILETYPE *const fp) {
111   unsigned char c;
112   /*if (PHYSFS_eof(fp)) {
113     return EOF;
114   }
115   MY_READ(&c, 1, 1, fp);*/
116   if (MY_READ(&c, 1, 1, fp) != 1) {
117     return EOF;
118   }
119   return c;
120 }
121 static char * MY_GETS(char * const str, const int size, 
122                       MY_FILETYPE *const fp) {
123   int i = 0;
124   int c;
125   do {
126     if (i == size-1) {
127       break;
128     }
129     c = MY_GETC(fp);
130     if (c == EOF) {
131       break;
132     }
133     str[i++] = c;
134   } while (c != '\0' && 
135       c != -1 && 
136       c != '\n');
137   str[i] = '\0';
138   if (i == 0) {
139     return NULL;
140   }
141   return str;
142 }
143 #define MY_CLOSE(fp) PHYSFS_close(fp)
144 #define MY_ATEOF(fp) PHYSFS_eof(fp)
145 #define MY_TELL(fp) PHYSFS_tell(fp)
146 #define MY_SEEK(fp,o) PHYSFS_seek(fp,o)
147 #define MY_REWIND(fp) MY_SEEK(fp,0)
148
149 #else
150
151 #define MY_FILETYPE FILE
152 #define MY_READ(p,s,n,fp) fread(p,s,n,fp)
153 #define MY_OPEN_FOR_READ(n) fopen(n, "rb")
154 #define MY_GETC(fp) fgetc(fp)
155 #define MY_GETS(str,size,fp) fgets(str,size,fp)
156 #define MY_CLOSE(fp) fclose(fp)
157 #define MY_ATEOF(fp) feof(fp)
158 #define MY_TELL(fp) ftell(fp)
159 #define MY_SEEK(fp,o) fseek(fp,o, SEEK_SET)
160 #define MY_REWIND(fp) rewind(fp)
161 /*static void MY_SETBUFFER(const MY_FILETYPE *const file, const int num) { }*/
162 #define MY_SETBUFFER(fp,size)
163 #endif
164
165 #endif