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
188
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "lua/lua_filesystem.hpp"
#include <string>
#include <cstring>
#include "lauxlib.h"
namespace lua {
[[maybe_unused]] static constexpr char kTag[] = "lua_fs";
static constexpr char kFileEntryMetatable[] = "fs_file_entry";
static constexpr char kFileIteratorMetatable[] = "fs_iterator";
// Todo: Use std::pmr::string for paths/dirs
struct LuaFileEntry {
bool isHidden;
bool isDirectory;
bool isTrack;
std::string path;
std::string name;
};
static auto push_lua_file_entry(lua_State* L, const lua::FileEntry& r) -> void {
lua::FileEntry** entry = reinterpret_cast<lua::FileEntry**>(
lua_newuserdata(L, sizeof(uintptr_t)));
*entry = new lua::FileEntry(r);
luaL_setmetatable(L, kFileEntryMetatable);
}
auto check_file_entry(lua_State* L, int stack_pos) -> lua::FileEntry* {
lua::FileEntry* entry = *reinterpret_cast<lua::FileEntry**>(
luaL_checkudata(L, stack_pos, kFileEntryMetatable));
return entry;
}
auto check_file_iterator(lua_State* L, int stack_pos) -> lua::FileIterator* {
lua::FileIterator* it = *reinterpret_cast<lua::FileIterator**>(
luaL_checkudata(L, stack_pos, kFileIteratorMetatable));
return it;
}
static auto push_iterator(lua_State* state, const lua::FileIterator& it)
-> void {
lua::FileIterator** data = reinterpret_cast<lua::FileIterator**>(
lua_newuserdata(state, sizeof(uintptr_t)));
*data = new lua::FileIterator(it);
luaL_setmetatable(state, kFileIteratorMetatable);
}
static auto fs_iterate_prev(lua_State* state) -> int {
lua::FileIterator* it = check_file_iterator(state, 1);
it->prev();
std::optional<lua::FileEntry> res = it->value();
if (res) {
push_lua_file_entry(state, *res);
} else {
lua_pushnil(state);
}
return 1;
}
static auto fs_iterate(lua_State* state) -> int {
lua::FileIterator* it = check_file_iterator(state, 1);
it->next();
std::optional<lua::FileEntry> res = it->value();
if (res) {
push_lua_file_entry(state, *res);
} else {
lua_pushnil(state);
}
return 1;
}
static auto fs_iterator_clone(lua_State* state) -> int {
lua::FileIterator* it = check_file_iterator(state, 1);
push_iterator(state, *it);
return 1;
}
static auto fs_iterator_value(lua_State* state) -> int {
lua::FileIterator* it = check_file_iterator(state, 1);
std::optional<lua::FileEntry> res = it->value();
if (res) {
push_lua_file_entry(state, *res);
} else {
lua_pushnil(state);
}
return 1;
}
static auto fs_iterator_gc(lua_State* state) -> int {
lua::FileIterator* it = check_file_iterator(state, 1);
delete it;
return 0;
}
static const struct luaL_Reg kFileIteratorFuncs[] = {{"next", fs_iterate},
{"prev", fs_iterate_prev},
{"clone", fs_iterator_clone},
{"value", fs_iterator_value},
{"__call", fs_iterate},
{"__gc", fs_iterator_gc},
{NULL, NULL}};
static auto file_entry_path(lua_State* state) -> int {
lua::FileEntry* entry = check_file_entry(state, 1);
lua_pushlstring(state, entry->filepath.c_str(), entry->filepath.size());
return 1;
}
static auto file_entry_is_dir(lua_State* state) -> int {
lua::FileEntry* entry = check_file_entry(state, 1);
lua_pushboolean(state, entry->isDirectory);
return 1;
}
static auto file_entry_is_hidden(lua_State* state) -> int {
lua::FileEntry* entry = check_file_entry(state, 1);
lua_pushboolean(state, entry->isHidden);
return 1;
}
static auto file_entry_name(lua_State* state) -> int {
lua::FileEntry* entry = check_file_entry(state, 1);
lua_pushlstring(state, entry->name.c_str(), entry->name.size());
return 1;
}
static auto file_entry_gc(lua_State* state) -> int {
lua::FileEntry* entry = check_file_entry(state, 1);
delete entry;
return 1;
}
static const struct luaL_Reg kFileEntryFuncs[] = {{"filepath", file_entry_path},
{"name", file_entry_name},
{"is_directory", file_entry_is_dir},
{"is_hidden", file_entry_is_hidden},
{"__tostring", file_entry_name},
{"__gc", file_entry_gc},
{NULL, NULL}};
static auto fs_new_iterator(lua_State* state) -> int {
// Takes a filepath as a string and returns a new FileIterator
// on that directory
std::string filepath = luaL_checkstring(state, -1);
lua::FileIterator iter(filepath, false);
push_iterator(state, iter);
return 1;
}
static const struct luaL_Reg kFilesystemFuncs[] = {{"iterator", fs_new_iterator},
{NULL, NULL}};
static auto lua_filesystem(lua_State* state) -> int {
luaL_newmetatable(state, kFileIteratorMetatable);
lua_pushliteral(state, "__index");
lua_pushvalue(state, -2);
lua_settable(state, -3); // metatable.__index = metatable
luaL_setfuncs(state, kFileIteratorFuncs, 0);
luaL_newmetatable(state, kFileEntryMetatable);
lua_pushliteral(state, "__index");
lua_pushvalue(state, -2);
lua_settable(state, -3); // metatable.__index = metatable
luaL_setfuncs(state, kFileEntryFuncs, 0);
luaL_newlib(state, kFilesystemFuncs);
return 1;
}
auto RegisterFileSystemModule(lua_State* s) -> void {
luaL_requiref(s, "filesystem", lua_filesystem, true);
lua_pop(s, 1);
}
} // namespace lua
|