2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #define _FILE_OFFSET_BITS 64
21 #define _LARGEFILE_SOURCE
22 #define _LARGEFILE64_SOURCE
45 static struct file *file_list;
48 file_create(char *name)
51 struct file *file= g_new0(struct file,1);
55 file->fd=open(name, O_RDONLY|O_LARGEFILE | O_BINARY);
60 fstat(file->fd, &stat);
61 file->size=stat.st_size;
62 file->name = g_strdup(name);
63 g_assert(file != NULL);
69 int file_is_dir(char *name)
72 if (! stat(name, &buf)) {
73 return S_ISDIR(buf.st_mode);
79 int file_mkdir(char *name, int pflag)
81 char buffer[strlen(name)+1];
84 dbg(1,"enter %s %d\n",name,pflag);
86 if (file_is_dir(name))
88 return mkdir(name, 0777);
92 while (next=strchr(next, '/')) {
95 ret=file_mkdir(buffer, 0);
101 return file_mkdir(buffer, 0);
105 file_mmap(struct file *file)
108 file->begin = (char*)mmap_readonly_win32( file->name, &file->map_handle, &file->map_file );
110 file->begin=mmap(NULL, file->size, PROT_READ|PROT_WRITE, MAP_PRIVATE, file->fd, 0);
111 g_assert(file->begin != NULL);
112 if (file->begin == (void *)0xffffffff) {
117 g_assert(file->begin != (void *)0xffffffff);
118 file->end=file->begin+file->size;
124 file_data_read(struct file *file, long long offset, int size)
128 return file->begin+offset;
130 lseek(file->fd, offset, SEEK_SET);
131 if (read(file->fd, ret, size) != size) {
140 uncompress_int(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
145 stream.next_in = (Bytef*)source;
146 stream.avail_in = (uInt)sourceLen;
147 stream.next_out = dest;
148 stream.avail_out = (uInt)*destLen;
150 stream.zalloc = (alloc_func)0;
151 stream.zfree = (free_func)0;
153 err = inflateInit2(&stream, -MAX_WBITS);
154 if (err != Z_OK) return err;
156 err = inflate(&stream, Z_FINISH);
157 if (err != Z_STREAM_END) {
159 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
163 *destLen = stream.total_out;
165 err = inflateEnd(&stream);
170 file_data_read_compressed(struct file *file, long long offset, int size, int size_uncomp)
174 uLongf destLen=size_uncomp;
176 ret=g_malloc(size_uncomp);
177 lseek(file->fd, offset, SEEK_SET);
178 if (read(file->fd, buffer, size) != size) {
182 if (uncompress_int(ret, &destLen, (Bytef *)buffer, size) != Z_OK) {
183 dbg(0,"uncompress failed\n");
192 file_data_free(struct file *file, unsigned char *data)
194 if (file->begin && data >= file->begin && data < file->end)
200 file_exists(char *name)
203 if (! stat(name, &buf))
209 file_remap_readonly(struct file *f)
214 munmap(f->begin, f->size);
215 begin=mmap(f->begin, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
216 if (f->begin != begin)
217 printf("remap failed\n");
222 file_remap_readonly_all(void)
224 struct file *f=file_list;
227 while (f && limit-- > 0) {
228 file_remap_readonly(f);
234 file_unmap(struct file *f)
237 mmap_unmap_win32( f->begin, f->map_handle , f->map_file );
239 munmap(f->begin, f->size);
246 struct file *f=file_list;
249 while (f && limit-- > 0) {
258 file_opendir(char *dir)
264 file_readdir(void *hnd)
275 file_closedir(void *hnd)
281 file_create_caseinsensitive(char *name)
283 char dirname[strlen(name)+1];
289 ret=file_create(name);
293 strcpy(dirname, name);
294 p=dirname+strlen(name);
295 while (p > dirname) {
301 d=file_opendir(dirname);
304 while ((filename=file_readdir(d))) {
305 if (!strcasecmp(filename, p)) {
307 ret=file_create(dirname);
318 file_destroy(struct file *f)
322 if ( f->begin != NULL )
331 struct file_wordexp {
336 struct file_wordexp *
337 file_wordexp_new(const char *pattern)
339 struct file_wordexp *ret=g_new0(struct file_wordexp, 1);
341 ret->err=wordexp(pattern, &ret->we, 0);
343 dbg(0,"wordexp('%s') returned %d\n", pattern, ret->err);
348 file_wordexp_get_count(struct file_wordexp *wexp)
350 return wexp->we.we_wordc;
354 file_wordexp_get_array(struct file_wordexp *wexp)
356 return wexp->we.we_wordv;
360 file_wordexp_destroy(struct file_wordexp *wexp)
369 file_get_param(struct file *file, struct param_list *param, int count)
372 param_add_string("Filename", file->name, ¶m, &count);
373 param_add_hex("Size", file->size, ¶m, &count);