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.
25 * HACK: MinGW provides numerous POSIX extensions to MSVCRT, including
26 * dirent.h, so parasti ever so lazily has not bothered to port the
27 * code below to FindFirstFile et al.
30 List dir_list_files(const char *path)
35 if ((dir = opendir(path)))
39 while ((ent = readdir(dir)))
41 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
44 files = list_cons(strdup(ent->d_name), files);
53 void dir_list_free(List files)
58 files = list_rest(files);
62 static struct dir_item *add_item(Array items, const char *dir, const char *name)
64 struct dir_item *item = array_add(items);
66 item->path = path_join(dir, name);
72 static void del_item(Array items)
74 struct dir_item *item = array_get(items, array_len(items) - 1);
76 free((void *) item->path);
82 Array dir_scan(const char *path,
83 int (*filter) (struct dir_item *),
84 List (*list_files)(const char *),
85 void (*free_files)(List))
90 assert((list_files && free_files) || (!list_files || !free_files));
92 if (!list_files) list_files = dir_list_files;
93 if (!free_files) free_files = dir_list_free;
95 items = array_new(sizeof (struct dir_item));
97 if ((files = list_files(path)))
99 for (file = files; file; file = file->next)
101 struct dir_item *item;
103 item = add_item(items, path, file->data);
105 if (filter && !filter(item))
115 void dir_free(Array items)
117 while (array_len(items))
123 int dir_exists(const char *path)
127 if ((dir = opendir(path)))