Skip to content

Latest commit

 

History

History
213 lines (163 loc) · 5.78 KB

archiver_dir.c

File metadata and controls

213 lines (163 loc) · 5.78 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
/*
* Standard directory I/O support routines for PhysicsFS.
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Jul 7, 2001
Jul 7, 2001
5
6
7
8
9
10
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
Jul 8, 2001
Jul 8, 2001
11
#include <string.h>
Jul 8, 2001
Jul 8, 2001
12
#include "physfs.h"
Jul 7, 2001
Jul 7, 2001
13
14
15
16
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Aug 30, 2010
Aug 30, 2010
17
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
Jul 9, 2001
Jul 9, 2001
18
Aug 30, 2010
Aug 30, 2010
19
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Jul 8, 2001
Jul 8, 2001
20
{
Jul 8, 2001
Jul 8, 2001
21
const char *dirsep = PHYSFS_getDirSeparator();
Sep 26, 2004
Sep 26, 2004
22
char *retval = NULL;
Aug 30, 2010
Aug 30, 2010
23
24
const size_t namelen = strlen(name);
const size_t seplen = strlen(dirsep);
Jul 8, 2001
Jul 8, 2001
25
Aug 30, 2010
Aug 30, 2010
26
assert(io == NULL); /* shouldn't create an Io for these. */
Aug 24, 2010
Aug 24, 2010
27
BAIL_IF_MACRO(!__PHYSFS_platformIsDirectory(name), ERR_NOT_AN_ARCHIVE, NULL);
Jul 8, 2001
Jul 8, 2001
28
Mar 14, 2005
Mar 14, 2005
29
retval = allocator.Malloc(namelen + seplen + 1);
Jul 8, 2001
Jul 8, 2001
30
31
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 30, 2010
Aug 30, 2010
32
33
/* make sure there's a dir separator at the end of the string */
/* !!! FIXME: is there really any place where (seplen != 1)? */
Sep 26, 2004
Sep 26, 2004
34
strcpy(retval, name);
Jul 8, 2001
Jul 8, 2001
35
if (strcmp((name + namelen) - seplen, dirsep) != 0)
Sep 26, 2004
Sep 26, 2004
36
strcat(retval, dirsep);
Oct 9, 2001
Oct 9, 2001
37
Jan 28, 2010
Jan 28, 2010
38
return retval;
Jul 8, 2001
Jul 8, 2001
39
40
41
} /* DIR_openArchive */
Aug 30, 2010
Aug 30, 2010
42
43
/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
Sep 29, 2004
Sep 29, 2004
44
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
45
46
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 8, 2001
Jul 8, 2001
47
{
Aug 30, 2010
Aug 30, 2010
48
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
Sep 29, 2004
Sep 29, 2004
49
50
if (d != NULL)
{
Sep 18, 2005
Sep 18, 2005
51
52
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
Mar 14, 2005
Mar 14, 2005
53
allocator.Free(d);
Sep 29, 2004
Sep 29, 2004
54
} /* if */
Jul 8, 2001
Jul 8, 2001
55
56
57
} /* DIR_enumerateFiles */
Sep 26, 2004
Sep 26, 2004
58
static int DIR_exists(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
59
{
Sep 26, 2004
Sep 26, 2004
60
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
61
62
63
64
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformExists(f);
Mar 14, 2005
Mar 14, 2005
65
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
66
return retval;
Jul 8, 2001
Jul 8, 2001
67
68
69
} /* DIR_exists */
Sep 26, 2004
Sep 26, 2004
70
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
71
{
Sep 26, 2004
Sep 26, 2004
72
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
73
int retval = 0;
Jul 8, 2001
Jul 8, 2001
74
75
BAIL_IF_MACRO(d == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
76
77
78
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
Mar 14, 2005
Mar 14, 2005
79
allocator.Free(d);
Jan 28, 2010
Jan 28, 2010
80
return retval;
Jul 8, 2001
Jul 8, 2001
81
82
83
} /* DIR_isDirectory */
Sep 26, 2004
Sep 26, 2004
84
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
85
{
Sep 26, 2004
Sep 26, 2004
86
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
87
int retval = 0;
Jul 8, 2001
Jul 8, 2001
88
May 21, 2002
May 21, 2002
89
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
90
91
92
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
Mar 14, 2005
Mar 14, 2005
93
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
94
return retval;
Jul 8, 2001
Jul 8, 2001
95
96
97
} /* DIR_isSymLink */
Aug 30, 2010
Aug 30, 2010
98
99
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
Jul 8, 2001
Jul 8, 2001
100
{
Sep 26, 2004
Sep 26, 2004
101
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 30, 2010
Aug 30, 2010
102
103
104
105
106
107
108
PHYSFS_Io *io = NULL;
int existtmp = 0;
if (fileExists == NULL)
fileExists = &existtmp;
*fileExists = 0;
Jul 8, 2001
Jul 8, 2001
109
110
111
BAIL_IF_MACRO(f == NULL, NULL, NULL);
Aug 30, 2010
Aug 30, 2010
112
113
114
io = __PHYSFS_createNativeIo(f, mode);
allocator.Free(f);
if (io == NULL)
Aug 21, 2002
Aug 21, 2002
115
116
{
*fileExists = __PHYSFS_platformExists(f);
Aug 30, 2010
Aug 30, 2010
117
return NULL;
Aug 21, 2002
Aug 21, 2002
118
119
} /* if */
Aug 30, 2010
Aug 30, 2010
120
121
*fileExists = 1;
return io;
Jul 8, 2001
Jul 8, 2001
122
123
124
} /* doOpen */
Aug 30, 2010
Aug 30, 2010
125
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
126
{
Aug 30, 2010
Aug 30, 2010
127
return doOpen(opaque, fnm, 'r', exist);
Jul 8, 2001
Jul 8, 2001
128
129
130
} /* DIR_openRead */
Aug 30, 2010
Aug 30, 2010
131
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
132
{
Aug 30, 2010
Aug 30, 2010
133
return doOpen(opaque, filename, 'w', NULL);
Jul 8, 2001
Jul 8, 2001
134
135
136
} /* DIR_openWrite */
Aug 30, 2010
Aug 30, 2010
137
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
138
{
Aug 30, 2010
Aug 30, 2010
139
return doOpen(opaque, filename, 'a', NULL);
Jul 8, 2001
Jul 8, 2001
140
141
142
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
143
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
144
{
Sep 26, 2004
Sep 26, 2004
145
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
146
147
148
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
149
retval = __PHYSFS_platformDelete(f);
Mar 14, 2005
Mar 14, 2005
150
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
151
return retval;
Jul 8, 2001
Jul 8, 2001
152
153
154
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
155
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
156
{
Sep 26, 2004
Sep 26, 2004
157
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
158
159
160
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 23, 2001
Aug 23, 2001
161
retval = __PHYSFS_platformMkDir(f);
Mar 14, 2005
Mar 14, 2005
162
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
163
return retval;
Jul 8, 2001
Jul 8, 2001
164
165
166
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
167
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
168
{
Mar 14, 2005
Mar 14, 2005
169
allocator.Free(opaque);
Jul 8, 2001
Jul 8, 2001
170
171
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
172
Aug 22, 2010
Aug 22, 2010
173
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
Feb 15, 2010
Feb 15, 2010
174
175
176
177
178
179
180
181
182
183
184
PHYSFS_Stat *stat)
{
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
int retval = 0;
BAIL_IF_MACRO(d == NULL, NULL, 0);
retval = __PHYSFS_platformStat(d, exists, stat);
allocator.Free(d);
return retval;
} /* DIR_stat */
Sep 29, 2004
Sep 29, 2004
185
186
187
188
189
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
190
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
&__PHYSFS_ArchiveInfo_DIR,
DIR_openArchive, /* openArchive() method */
DIR_enumerateFiles, /* enumerateFiles() method */
DIR_exists, /* exists() method */
DIR_isDirectory, /* isDirectory() method */
DIR_isSymLink, /* isSymLink() method */
DIR_openRead, /* openRead() method */
DIR_openWrite, /* openWrite() method */
DIR_openAppend, /* openAppend() method */
DIR_remove, /* remove() method */
DIR_mkdir, /* mkdir() method */
DIR_dirClose, /* dirClose() method */
Aug 30, 2010
Aug 30, 2010
209
DIR_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
210
211
};
Jul 7, 2001
Jul 7, 2001
212
/* end of dir.c ... */