2 * Copyright (C) 2003-2010 Neverball authors
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
26 * This file implements the low-level virtual file system routines
27 * using the PhysicsFS 1.0 API.
30 /*---------------------------------------------------------------------------*/
37 int fs_init(const char *argv0)
39 if (PHYSFS_init(argv0))
41 PHYSFS_permitSymbolicLinks(1);
50 return PHYSFS_deinit();
53 const char *fs_error(void)
55 return PHYSFS_getLastError();
58 /*---------------------------------------------------------------------------*/
60 const char *fs_base_dir(void)
62 return PHYSFS_getBaseDir();
65 int fs_add_path(const char *path)
67 return PHYSFS_addToSearchPath(path, 0);
70 int fs_set_write_dir(const char *path)
72 return PHYSFS_setWriteDir(path);
75 const char *fs_get_write_dir(void)
77 return PHYSFS_getWriteDir();
80 /*---------------------------------------------------------------------------*/
82 static List list_files(const char *path)
87 if ((files = PHYSFS_enumerateFiles(path)))
89 for (file = files; *file; file++)
90 list = list_cons(strdup(*file), list);
92 PHYSFS_freeList(files);
98 static void free_files(List list)
103 list = list_rest(list);
107 Array fs_dir_scan(const char *path, int (*filter)(struct dir_item *))
109 return dir_scan(path, filter, list_files, free_files);
112 void fs_dir_free(Array items)
117 /*---------------------------------------------------------------------------*/
119 fs_file fs_open(const char *path, const char *mode)
123 assert((mode[0] == 'r' && !mode[1]) ||
124 (mode[0] == 'w' && (!mode[1] || mode[1] == '+')));
126 if ((fh = malloc(sizeof (*fh))))
131 fh->handle = PHYSFS_openRead(path);
135 fh->handle = (mode[1] == '+' ?
136 PHYSFS_openAppend(path) :
137 PHYSFS_openWrite(path));
143 PHYSFS_setBuffer(fh->handle, 0x2000);
155 int fs_close(fs_file fh)
157 if (PHYSFS_close(fh->handle))
165 /*----------------------------------------------------------------------------*/
167 int fs_mkdir(const char *path)
169 return PHYSFS_mkdir(path);
172 int fs_exists(const char *path)
174 return PHYSFS_exists(path);
177 int fs_remove(const char *path)
179 return PHYSFS_delete(path);
182 /*---------------------------------------------------------------------------*/
184 int fs_read(void *data, int size, int count, fs_file fh)
186 return PHYSFS_read(fh->handle, data, size, count);
189 int fs_write(const void *data, int size, int count, fs_file fh)
191 return PHYSFS_write(fh->handle, data, size, count);
194 int fs_flush(fs_file fh)
196 return PHYSFS_flush(fh->handle);
199 long fs_tell(fs_file fh)
201 return PHYSFS_tell(fh->handle);
204 int fs_seek(fs_file fh, long offset, int whence)
206 PHYSFS_uint64 pos = 0;
207 PHYSFS_sint64 cur = PHYSFS_tell(fh->handle);
208 PHYSFS_sint64 len = PHYSFS_fileLength(fh->handle);
229 return PHYSFS_seek(fh->handle, pos);
232 int fs_eof(fs_file fh)
234 return PHYSFS_eof(fh->handle);
237 int fs_length(fs_file fh)
239 return PHYSFS_fileLength(fh->handle);
242 /*---------------------------------------------------------------------------*/