Fix typo in german translation.
[maemo-rb.git] / apps / plugins / lua / luadir.c
blobc8c21d2c65f2e3c850ad8be9be8b8c628fbba13d
1 /*
2 * Based on LuaFileSystem : http://www.keplerproject.org/luafilesystem
4 * Copyright © 2003 Kepler Project.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "plugin.h"
27 #include "rocklibc.h"
29 #include "lauxlib.h"
30 #include "luadir.h"
32 #define DIR_METATABLE "directory metatable"
33 typedef struct dir_data {
34 int closed;
35 DIR *dir;
36 } dir_data;
38 static int make_dir (lua_State *L) {
39 const char *path = luaL_checkstring (L, 1);
40 lua_pushboolean (L, !rb->mkdir(path));
41 return 1;
44 static int remove_dir (lua_State *L) {
45 const char *path = luaL_checkstring (L, 1);
46 lua_pushboolean (L, !rb->rmdir (path));
47 return 1;
51 ** Directory iterator
53 static int dir_iter (lua_State *L) {
54 struct dirent *entry;
55 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
56 luaL_argcheck (L, !d->closed, 1, "closed directory");
58 if ((entry = rb->readdir (d->dir)) != NULL) {
59 struct dirinfo info = rb->dir_get_info(d->dir, entry);
60 lua_pushstring (L, entry->d_name);
61 lua_pushboolean (L, info.attribute & ATTR_DIRECTORY);
62 return 2;
63 } else {
64 /* no more entries => close directory */
65 rb->closedir (d->dir);
66 d->closed = 1;
67 return 0;
73 ** Closes directory iterators
75 static int dir_close (lua_State *L) {
76 dir_data *d = (dir_data *)lua_touserdata (L, 1);
78 if (!d->closed && d->dir) {
79 rb->closedir (d->dir);
80 d->closed = 1;
83 return 0;
88 ** Factory of directory iterators
90 static int dir_iter_factory (lua_State *L) {
91 const char *path = luaL_checkstring (L, 1);
92 dir_data *d;
93 lua_pushcfunction (L, dir_iter);
94 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
95 d->closed = 0;
97 luaL_getmetatable (L, DIR_METATABLE);
98 lua_setmetatable (L, -2);
99 d->dir = rb->opendir (path);
100 if (d->dir == NULL)
101 luaL_error (L, "cannot open %s: %d", path, errno);
103 return 2;
108 ** Creates directory metatable.
110 static int dir_create_meta (lua_State *L) {
111 luaL_newmetatable (L, DIR_METATABLE);
112 /* set its __gc field */
113 lua_pushstring (L, "__index");
114 lua_newtable(L);
115 lua_pushstring (L, "next");
116 lua_pushcfunction (L, dir_iter);
117 lua_settable(L, -3);
118 lua_pushstring (L, "close");
119 lua_pushcfunction (L, dir_close);
120 lua_settable(L, -3);
121 lua_settable (L, -3);
122 lua_pushstring (L, "__gc");
123 lua_pushcfunction (L, dir_close);
124 lua_settable (L, -3);
125 return 1;
128 static const struct luaL_reg fslib[] = {
129 {"dir", dir_iter_factory},
130 {"mkdir", make_dir},
131 {"rmdir", remove_dir},
132 {NULL, NULL},
135 int luaopen_luadir (lua_State *L) {
136 dir_create_meta (L);
137 luaL_register (L, LUA_DIRLIBNAME, fslib);
138 return 1;