Skip to content

Latest commit

 

History

History
170 lines (130 loc) · 4.71 KB

archiver_dir.c

File metadata and controls

170 lines (130 loc) · 4.71 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
11
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Aug 30, 2010
Aug 30, 2010
12
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
Jul 9, 2001
Jul 9, 2001
13
Aug 30, 2010
Aug 30, 2010
14
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Jul 8, 2001
Jul 8, 2001
15
{
Sep 5, 2010
Sep 5, 2010
16
PHYSFS_Stat statbuf;
Jul 8, 2001
Jul 8, 2001
17
const char *dirsep = PHYSFS_getDirSeparator();
Sep 26, 2004
Sep 26, 2004
18
char *retval = NULL;
Aug 30, 2010
Aug 30, 2010
19
20
const size_t namelen = strlen(name);
const size_t seplen = strlen(dirsep);
Sep 5, 2010
Sep 5, 2010
21
int exists = 0;
Jul 8, 2001
Jul 8, 2001
22
Aug 30, 2010
Aug 30, 2010
23
assert(io == NULL); /* shouldn't create an Io for these. */
Sep 5, 2010
Sep 5, 2010
24
25
26
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &statbuf), NULL, NULL);
if ((!exists) || (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY))
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
Jul 8, 2001
Jul 8, 2001
27
Mar 14, 2005
Mar 14, 2005
28
retval = allocator.Malloc(namelen + seplen + 1);
Jul 8, 2001
Jul 8, 2001
29
30
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 30, 2010
Aug 30, 2010
31
32
/* 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
33
strcpy(retval, name);
Jul 8, 2001
Jul 8, 2001
34
if (strcmp((name + namelen) - seplen, dirsep) != 0)
Sep 26, 2004
Sep 26, 2004
35
strcat(retval, dirsep);
Oct 9, 2001
Oct 9, 2001
36
Jan 28, 2010
Jan 28, 2010
37
return retval;
Jul 8, 2001
Jul 8, 2001
38
39
40
} /* DIR_openArchive */
Aug 30, 2010
Aug 30, 2010
41
42
/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
Sep 29, 2004
Sep 29, 2004
43
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
44
45
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 8, 2001
Jul 8, 2001
46
{
Aug 30, 2010
Aug 30, 2010
47
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
Sep 29, 2004
Sep 29, 2004
48
49
if (d != NULL)
{
Sep 18, 2005
Sep 18, 2005
50
51
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
Mar 14, 2005
Mar 14, 2005
52
allocator.Free(d);
Sep 29, 2004
Sep 29, 2004
53
} /* if */
Jul 8, 2001
Jul 8, 2001
54
55
56
} /* DIR_enumerateFiles */
Aug 30, 2010
Aug 30, 2010
57
58
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
Jul 8, 2001
Jul 8, 2001
59
{
Sep 26, 2004
Sep 26, 2004
60
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 30, 2010
Aug 30, 2010
61
62
63
64
65
66
67
PHYSFS_Io *io = NULL;
int existtmp = 0;
if (fileExists == NULL)
fileExists = &existtmp;
*fileExists = 0;
Jul 8, 2001
Jul 8, 2001
68
69
70
BAIL_IF_MACRO(f == NULL, NULL, NULL);
Aug 30, 2010
Aug 30, 2010
71
72
73
io = __PHYSFS_createNativeIo(f, mode);
allocator.Free(f);
if (io == NULL)
Aug 21, 2002
Aug 21, 2002
74
{
Mar 9, 2012
Mar 9, 2012
75
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
Sep 5, 2010
Sep 5, 2010
76
__PHYSFS_platformStat(f, fileExists, &statbuf);
Aug 30, 2010
Aug 30, 2010
77
return NULL;
Aug 21, 2002
Aug 21, 2002
78
79
} /* if */
Aug 30, 2010
Aug 30, 2010
80
81
*fileExists = 1;
return io;
Jul 8, 2001
Jul 8, 2001
82
83
84
} /* doOpen */
Aug 30, 2010
Aug 30, 2010
85
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
86
{
Aug 30, 2010
Aug 30, 2010
87
return doOpen(opaque, fnm, 'r', exist);
Jul 8, 2001
Jul 8, 2001
88
89
90
} /* DIR_openRead */
Aug 30, 2010
Aug 30, 2010
91
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
92
{
Aug 30, 2010
Aug 30, 2010
93
return doOpen(opaque, filename, 'w', NULL);
Jul 8, 2001
Jul 8, 2001
94
95
96
} /* DIR_openWrite */
Aug 30, 2010
Aug 30, 2010
97
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
98
{
Aug 30, 2010
Aug 30, 2010
99
return doOpen(opaque, filename, 'a', NULL);
Jul 8, 2001
Jul 8, 2001
100
101
102
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
103
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
104
{
Sep 26, 2004
Sep 26, 2004
105
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
106
107
108
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
109
retval = __PHYSFS_platformDelete(f);
Mar 14, 2005
Mar 14, 2005
110
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
111
return retval;
Jul 8, 2001
Jul 8, 2001
112
113
114
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
115
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
116
{
Sep 26, 2004
Sep 26, 2004
117
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
118
119
120
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 23, 2001
Aug 23, 2001
121
retval = __PHYSFS_platformMkDir(f);
Mar 14, 2005
Mar 14, 2005
122
allocator.Free(f);
Jan 28, 2010
Jan 28, 2010
123
return retval;
Jul 8, 2001
Jul 8, 2001
124
125
126
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
127
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
128
{
Mar 14, 2005
Mar 14, 2005
129
allocator.Free(opaque);
Jul 8, 2001
Jul 8, 2001
130
131
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
132
Aug 22, 2010
Aug 22, 2010
133
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
Feb 15, 2010
Feb 15, 2010
134
135
136
137
138
139
140
141
142
143
144
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
145
146
147
148
149
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
150
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
&__PHYSFS_ArchiveInfo_DIR,
DIR_openArchive, /* openArchive() method */
DIR_enumerateFiles, /* enumerateFiles() 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
166
DIR_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
167
168
};
Jul 7, 2001
Jul 7, 2001
169
/* end of dir.c ... */