/
dir.c
187 lines (162 loc) · 3.92 KB
/
dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* nclua.dir -- Directory functions.
Copyright (C) 2013-2018 PUC-Rio/Laboratorio TeleMidia
This file is part of NCLua.
NCLua is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
NCLua is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with NCLua. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "aux-glib.h"
#include "aux-lua.h"
/* Registry key for the dir metatable. */
#define DIR "nclua.event.dir"
/* Dir object data. */
typedef struct _dir_t
{
GDir *gdir;
} dir_t;
/* Checks if the object at the given index is dir. */
#define dir_check(L, index)\
(dir_t *) luaL_checkudata (L, index, DIR)
/*-
* dir.__gc (dir:userdata)
*
* Destroys the given dir object.
*/
static int
__l_dir_gc (lua_State *L)
{
dir_t *dir;
dir = dir_check (L, 1);
if (dir->gdir != NULL)
{
g_dir_close (dir->gdir);
dir->gdir = NULL;
}
return 0;
}
/*-
* dir.dir (path:string) -> iter:function
*
* Returns an iterator function that each time it is called returns a
* directory entry's name as a string. If PATH is not a directory or if it
* cannot be read, raises an error.
*/
static int
l_dir_dir_it_closure (lua_State *L)
{
dir_t *dir;
const char *entry;
luax_pushupvalue (L, 1);
dir = (dir_t *) lua_touserdata (L, -1);
if (unlikely (dir->gdir == NULL))
{
lua_pushnil (L);
return 1;
}
entry = g_dir_read_name (dir->gdir);
if (entry == NULL)
{
g_dir_close (dir->gdir);
dir->gdir = NULL;
lua_pushnil (L);
return 1;
}
lua_pushstring (L, entry);
return 1;
}
static int
l_dir_dir (lua_State *L)
{
const char *path;
GDir *gdir;
GError *error;
dir_t *dir;
path = luaL_checkstring (L, 1);
error = NULL;
gdir = g_dir_open (path, 0, &error);
if (unlikely (gdir == NULL))
{
g_assert_nonnull (error);
lua_pushfstring (L, "%s", error->message);
g_error_free (error);
return lua_error (L);
}
dir = (dir_t *) lua_newuserdata (L, sizeof (*dir));
g_assert_nonnull (dir);
dir->gdir = gdir;
lua_pushcclosure (L, l_dir_dir_it_closure, 1);
return 1;
}
/*-
* dir.test (path, [query:string]) -> status:boolean
*
* Returns true if path satisfies the given query.
*
* The following query strings are supported:
* directory - path is a directory;
* executable - path is an executable file;
* exists - path can be read;
* regular - path is a regular file.
* symlink - path is a symbolic link;
*
* The default query is 'exists'.
*/
static int
l_dir_test (lua_State *L)
{
static const char *query_list[] = {
"directory",
"executable",
"exists",
"regular",
"symlink",
NULL
};
const char *path;
GFileTest test = G_FILE_TEST_EXISTS;
path = luaL_checkstring (L, 1);
switch (luaL_checkoption (L, 2, "exists", query_list))
{
case 0:
test = G_FILE_TEST_IS_DIR;
break;
case 1:
test = G_FILE_TEST_IS_EXECUTABLE;
break;
case 2:
test = G_FILE_TEST_EXISTS;
break;
case 3:
test = G_FILE_TEST_IS_REGULAR;
break;
case 4:
test = G_FILE_TEST_IS_SYMLINK;
break;
default:
g_assert_not_reached ();
}
lua_pushboolean (L, g_file_test (path, test));
return 1;
}
static const struct luaL_Reg funcs[] = {
{"__gc", __l_dir_gc},
{"dir", l_dir_dir},
{"test", l_dir_test},
{NULL, NULL}
};
int luaopen_nclua_dir (lua_State *L);
int
luaopen_nclua_dir (lua_State *L)
{
G_TYPE_INIT_WRAPPER ();
luax_newmetatable (L, DIR);
luaL_setfuncs (L, funcs, 0);
return 1;
}