Add missing credit for Esperanto translation
[neverball.git] / share / fs_physfs.c
blob7e1183988cf0fabb1cd3d4d0f44fb74bd3fe0d87
1 /*
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.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <assert.h>
18 #include <physfs.h>
20 #include "fs.h"
21 #include "dir.h"
22 #include "array.h"
23 #include "common.h"
26 * This file implements the low-level virtual file system routines
27 * using the PhysicsFS 1.0 API.
30 /*---------------------------------------------------------------------------*/
32 struct fs_file_s
34 PHYSFS_file *handle;
37 int fs_init(const char *argv0)
39 if (PHYSFS_init(argv0))
41 PHYSFS_permitSymbolicLinks(1);
42 return 1;
45 return 0;
48 int fs_quit(void)
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)
84 List list = NULL;
85 char **files, **file;
87 if ((files = PHYSFS_enumerateFiles(path)))
89 for (file = files; *file; file++)
90 list = list_cons(strdup(*file), list);
92 PHYSFS_freeList(files);
95 return list;
98 static void free_files(List list)
100 while (list)
102 free(list->data);
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)
114 dir_free(items);
117 /*---------------------------------------------------------------------------*/
119 fs_file fs_open(const char *path, const char *mode)
121 fs_file fh;
123 assert((mode[0] == 'r' && !mode[1]) ||
124 (mode[0] == 'w' && (!mode[1] || mode[1] == '+')));
126 if ((fh = malloc(sizeof (*fh))))
128 switch (mode[0])
130 case 'r':
131 fh->handle = PHYSFS_openRead(path);
132 break;
134 case 'w':
135 fh->handle = (mode[1] == '+' ?
136 PHYSFS_openAppend(path) :
137 PHYSFS_openWrite(path));
138 break;
141 if (fh->handle)
143 PHYSFS_setBuffer(fh->handle, 0x2000);
145 else
147 free(fh);
148 fh = NULL;
152 return fh;
155 int fs_close(fs_file fh)
157 if (PHYSFS_close(fh->handle))
159 free(fh);
160 return 1;
162 return 0;
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);
210 switch (whence)
212 case SEEK_SET:
213 pos = offset;
214 break;
216 case SEEK_CUR:
217 if (cur < 0)
218 return -1;
219 pos = cur + offset;
220 break;
222 case SEEK_END:
223 if (len < 0)
224 return -1;
225 pos = len + offset;
226 break;
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 /*---------------------------------------------------------------------------*/