Skip to content

Latest commit

 

History

History
165 lines (152 loc) · 5.2 KB

abs-file.h

File metadata and controls

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