1 /* |
1 /* |
2 * stdio/physfs abstraction layer 2002-08-29 |
2 * stdio/physfs abstraction layer 2002-12-03 |
3 * |
3 * |
4 * Adam D. Moss <adam@gimp.org> <aspirin@icculus.org> |
4 * Adam D. Moss <adam@gimp.org> <aspirin@icculus.org> |
5 * |
5 * |
6 * These wrapper macros and functions are designed to allow a program |
6 * These wrapper macros and functions are designed to allow a program |
7 * to perform file I/O with identical semantics and syntax regardless |
7 * to perform file I/O with identical semantics and syntax regardless |
8 * of whether PhysicsFS is being used or not. Define USE_PHYSFS if |
8 * of whether PhysicsFS is being used or not. |
9 * PhysicsFS is being usedl in this case you will still need to initialize |
|
10 * PhysicsFS yourself and set up its search-paths. |
|
11 */ |
9 */ |
12 #ifndef _ABS_FILE_H |
10 #ifndef _ABS_FILE_H |
13 #define _ABS_FILE_H |
11 #define _ABS_FILE_H |
14 /* |
12 /* |
15 PLEASE NOTE: This license applies to abs-file.h ONLY (to make it clear that |
13 PLEASE NOTE: This license applies to abs-file.h ONLY (to make it clear that |
43 */ |
41 */ |
44 |
42 |
45 #include <stdlib.h> |
43 #include <stdlib.h> |
46 #include <stdio.h> |
44 #include <stdio.h> |
47 |
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_REWIND rewind(...) |
|
59 * MY_SETBUFFER (not a standard for stdio, does nothing there) |
|
60 */ |
|
61 |
|
62 /* |
|
63 * Important DEFINEs: |
|
64 * It is important to define these consistantly across the various |
|
65 * compilation modules of your program if you wish to exchange file |
|
66 * handles between them. |
|
67 * |
|
68 * USE_PHYSFS: Define USE_PHYSFS if PhysicsFS is being used; note that if |
|
69 * you do intend to use PhysicsFS then you will still need to initialize |
|
70 * PhysicsFS yourself and set up its search-paths. |
|
71 * |
|
72 * Optional DEFINEs: |
|
73 * |
|
74 * PHYSFS_DEFAULT_READ_BUFFER <bytes>: If set then abs-file.h sets the |
|
75 * PhysicsFS buffer size to this value whenever you open a file. You |
|
76 * may over-ride this on a per-filehandle basis by using the |
|
77 * MY_SETBUFFER() macro (which simply does nothing when not using |
|
78 * PhysicsFS). If you have not defined this value explicitly then |
|
79 * abs-file.h will default to the same default buffer size as used by |
|
80 * stdio if it can be determined, or 8192 bytes otherwise. |
|
81 */ |
|
82 #ifndef PHYSFS_DEFAULT_READ_BUFFER |
|
83 #ifdef BUFSIZ |
|
84 #define PHYSFS_DEFAULT_READ_BUFFER BUFSIZ |
|
85 #else |
|
86 #define PHYSFS_DEFAULT_READ_BUFFER 8192 |
|
87 #endif |
|
88 #endif |
|
89 |
48 #ifdef USE_PHYSFS |
90 #ifdef USE_PHYSFS |
49 |
91 |
50 #include <physfs.h> |
92 #include <physfs.h> |
51 #define MY_FILETYPE PHYSFS_file |
93 #define MY_FILETYPE PHYSFS_file |
|
94 #define MY_SETBUFFER(fp,size) PHYSFS_setBuffer(fp,size) |
52 #define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n) |
95 #define MY_READ(p,s,n,fp) PHYSFS_read(fp,p,s,n) |
|
96 #if PHYSFS_DEFAULT_READ_BUFFER |
|
97 static MY_FILETYPE* MY_OPEN_FOR_READ(const char *const filename) |
|
98 { |
|
99 MY_FILETYPE *const file = PHYSFS_openRead(filename); |
|
100 if (file) { |
|
101 MY_SETBUFFER(file, PHYSFS_DEFAULT_READ_BUFFER); |
|
102 } |
|
103 return file; |
|
104 } |
|
105 #else |
53 #define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn) |
106 #define MY_OPEN_FOR_READ(fn) PHYSFS_openRead(fn) |
54 static int MY_GETC(MY_FILETYPE * fp) { |
107 #endif |
|
108 static int MY_GETC(MY_FILETYPE *const fp) { |
55 unsigned char c; |
109 unsigned char c; |
56 /*if (PHYSFS_eof(fp)) { |
110 /*if (PHYSFS_eof(fp)) { |
57 return EOF; |
111 return EOF; |
58 } |
112 } |
59 MY_READ(&c, 1, 1, fp);*/ |
113 MY_READ(&c, 1, 1, fp);*/ |
60 if (MY_READ(&c, 1, 1, fp) != 1) { |
114 if (MY_READ(&c, 1, 1, fp) != 1) { |
61 return EOF; |
115 return EOF; |
62 } |
116 } |
63 return c; |
117 return c; |
64 } |
118 } |
65 static char * MY_GETS(char * const str, int size, MY_FILETYPE * fp) { |
119 static char * MY_GETS(char * const str, const int size, |
|
120 MY_FILETYPE *const fp) { |
66 int i = 0; |
121 int i = 0; |
67 int c; |
122 int c; |
68 do { |
123 do { |
69 if (i == size-1) { |
124 if (i == size-1) { |
70 break; |
125 break; |
95 #define MY_GETC(fp) fgetc(fp) |
150 #define MY_GETC(fp) fgetc(fp) |
96 #define MY_GETS(str,size,fp) fgets(str,size,fp) |
151 #define MY_GETS(str,size,fp) fgets(str,size,fp) |
97 #define MY_CLOSE(fp) fclose(fp) |
152 #define MY_CLOSE(fp) fclose(fp) |
98 #define MY_ATEOF(fp) feof(fp) |
153 #define MY_ATEOF(fp) feof(fp) |
99 #define MY_REWIND(fp) rewind(fp) |
154 #define MY_REWIND(fp) rewind(fp) |
100 |
155 static void MY_SETBUFFER(const MY_FILETYPE *const file, const int num) { } |
101 #endif |
156 #endif |
102 |
157 |
103 #endif |
158 #endif |