author | Ryan C. Gordon <icculus@icculus.org> |
Sat, 23 Jul 2005 22:04:23 +0000 | |
branch | stable-1.0 |
changeset 720 | 31037fbc5dd8 |
parent 578 | bff1af8455ca |
child 654 | c0ae01de361d |
permissions | -rw-r--r-- |
485 | 1 |
/* |
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
2 |
* stdio/physfs abstraction layer 2003-04-02 |
485 | 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 |
|
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
8 |
* of whether PhysicsFS is being used or not. |
485 | 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 |
||
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
18 |
Copyright (C) 2002-2003 Adam D. Moss (the "Author"). All Rights Reserved. |
485 | 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 |
||
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
46 |
/* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
47 |
* API: |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
48 |
* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
49 |
* Macro/function use like stdio equivalent... |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
50 |
* -------------- ---------------------------- |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
51 |
* MY_FILETYPE FILE |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
52 |
* MY_OPEN_FOR_READ fopen(..., "rb") |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
53 |
* MY_READ fread(...) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
54 |
* MY_CLOSE fclose(...) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
55 |
* MY_GETC fgetc(...) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
56 |
* MY_GETS fgets(...) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
57 |
* MY_ATEOF feof(...) |
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
58 |
* MY_TELL ftell(...) |
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
59 |
* MY_SEEK fseek(..., SEEK_SET) |
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
60 |
* MY_REWIND rewind(...) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
61 |
* MY_SETBUFFER (not a standard for stdio, does nothing there) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
62 |
*/ |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
63 |
|
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
64 |
/* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
65 |
* Important DEFINEs: |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
66 |
* It is important to define these consistantly across the various |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
67 |
* compilation modules of your program if you wish to exchange file |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
68 |
* handles between them. |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
69 |
* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
70 |
* USE_PHYSFS: Define USE_PHYSFS if PhysicsFS is being used; note that if |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
71 |
* you do intend to use PhysicsFS then you will still need to initialize |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
72 |
* PhysicsFS yourself and set up its search-paths. |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
73 |
* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
74 |
* Optional DEFINEs: |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
75 |
* |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
76 |
* PHYSFS_DEFAULT_READ_BUFFER <bytes>: If set then abs-file.h sets the |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
77 |
* PhysicsFS buffer size to this value whenever you open a file. You |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
78 |
* may over-ride this on a per-filehandle basis by using the |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
79 |
* MY_SETBUFFER() macro (which simply does nothing when not using |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
80 |
* PhysicsFS). If you have not defined this value explicitly then |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
81 |
* abs-file.h will default to the same default buffer size as used by |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
82 |
* stdio if it can be determined, or 8192 bytes otherwise. |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
83 |
*/ |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
84 |
#ifndef PHYSFS_DEFAULT_READ_BUFFER |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
85 |
#ifdef BUFSIZ |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
86 |
#define PHYSFS_DEFAULT_READ_BUFFER BUFSIZ |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
87 |
#else |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
88 |
#define PHYSFS_DEFAULT_READ_BUFFER 8192 |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
89 |
#endif |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
90 |
#endif |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
91 |
|
485 | 92 |
#ifdef USE_PHYSFS |
93 |
||
94 |
#include <physfs.h> |
|
95 |
#define MY_FILETYPE PHYSFS_file |
|
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
96 |
#define MY_SETBUFFER(fp,size) PHYSFS_setBuffer(fp,size) |
485 | 97 |
#define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n) |
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
98 |
#if PHYSFS_DEFAULT_READ_BUFFER |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
99 |
static MY_FILETYPE* MY_OPEN_FOR_READ(const char *const filename) |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
100 |
{ |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
101 |
MY_FILETYPE *const file = PHYSFS_openRead(filename); |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
102 |
if (file) { |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
103 |
MY_SETBUFFER(file, PHYSFS_DEFAULT_READ_BUFFER); |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
104 |
} |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
105 |
return file; |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
106 |
} |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
107 |
#else |
485 | 108 |
#define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn) |
512
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
109 |
#endif |
5ed9ffa3ad8f
Updates from Adam Moss for new buffering API.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset
|
110 |
static int MY_GETC(MY_FILETYPE *const fp) { |
485 | 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 |
} |
|
578
bff1af8455ca
Tabs-to-spaces patch by James Turk.
Ryan C. Gordon <icculus@icculus.org>
parents:
562
diff
changeset
|
121 |
static char * MY_GETS(char * const str, const int size, |
bff1af8455ca
Tabs-to-spaces patch by James Turk.
Ryan C. Gordon <icculus@icculus.org>
parents:
562
diff
changeset
|
122 |
MY_FILETYPE *const fp) { |
485 | 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; |
|
578
bff1af8455ca
Tabs-to-spaces patch by James Turk.
Ryan C. Gordon <icculus@icculus.org>
parents:
562
diff
changeset
|
134 |
} while (c != '\0' && |
bff1af8455ca
Tabs-to-spaces patch by James Turk.
Ryan C. Gordon <icculus@icculus.org>
parents:
562
diff
changeset
|
135 |
c != -1 && |
bff1af8455ca
Tabs-to-spaces patch by James Turk.
Ryan C. Gordon <icculus@icculus.org>
parents:
562
diff
changeset
|
136 |
c != '\n'); |
485 | 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) |
|
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
145 |
#define MY_TELL(fp) PHYSFS_tell(fp) |
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
146 |
#define MY_SEEK(fp,o) PHYSFS_seek(fp,o) |
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
147 |
#define MY_REWIND(fp) MY_SEEK(fp,0) |
485 | 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) |
|
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
158 |
#define MY_TELL(fp) ftell(fp) |
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
159 |
#define MY_SEEK(fp,o) fseek(fp,o, SEEK_SET) |
485 | 160 |
#define MY_REWIND(fp) rewind(fp) |
562
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
161 |
/*static void MY_SETBUFFER(const MY_FILETYPE *const file, const int num) { }*/ |
1e307df36810
Minor patches from Adam.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset
|
162 |
#define MY_SETBUFFER(fp,size) |
485 | 163 |
#endif |
164 |
||
165 |
#endif |