-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncm.cpp
192 lines (144 loc) · 3.96 KB
/
ncm.cpp
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
189
190
191
192
#include <iostream>
#include <fstream>
#include <filesystem>
#include <cstdint>
#include <string>
using std::string;
#if 0
#define FMT_HEADER_ONLY
#include "fmt/core.h"
#endif
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
namespace wfsint {
std::string int_to_hex64(std::uint64_t n) {
#if 1
std::ostringstream os;
os << "0x" << std::setbase(16) << std::setw(16) << std::setfill('0') << n << std::flush;
return std::move(os.str());
#else
const char* digits = "0123456789abcdef";
std::string s("0x0123456789abcdef");
for (int i = 0; i < 16; ++i) {
std::uint64_t q = n / 16;
std::uint64_t r = n % 16;
s.at(15+2-i) = digits[r];
n = q;
}
return std::move(s);
#endif
}
} // namespace wfsint
namespace wfslua {
using namespace wfsint;
int ncm_fs_space(lua_State* L) {
const char *dn = lua_tostring(L, 1);
if (dn == 0) return 0;
std::error_code ec;
const std::filesystem::space_info si = std::filesystem::space(dn, ec);
if (ec.value() != 0) return 0;
lua_newtable(L);
lua_pushstring(L, "capacity");
lua_pushinteger(L, static_cast<lua_Integer>(si.capacity));
lua_settable(L, -3);
lua_pushstring(L, "free");
lua_pushinteger(L, static_cast<lua_Integer>(si.free));
lua_settable(L, -3);
lua_pushstring(L, "available");
lua_pushinteger(L, static_cast<lua_Integer>(si.available));
lua_settable(L, -3);
return 1;
}
int ncm_fs_hash_value(lua_State* L) {
using std::filesystem::hash_value;
using std::filesystem::path;
const char *pn = lua_tostring(L, 1); // any path.
if (pn == 0) return 0;
const std::size_t h = hash_value(path(pn));
lua_pushstring(L, int_to_hex64(h).c_str());
return 1;
}
int ncm_fs_file_size(lua_State* L) {
using std::filesystem::file_size;
using std::filesystem::path;
const char *pn = lua_tostring(L, 1); // any path.
if (pn == 0) return 0;
const std::size_t l = file_size(path(pn));
lua_pushinteger(L, l);
return 1;
}
int ncm_fs_has_root_path(lua_State* L) {
using std::filesystem::path;
const char *pn = lua_tostring(L, 1); // any path.
if (pn == 0) return 0;
const bool l = path(pn).has_root_path();
lua_pushinteger(L, l);
return 1;
}
int ncm_fs_root_path(lua_State* L) {
using std::filesystem::path;
const char *pn = lua_tostring(L, 1); // any path.
if (pn == 0) return 0;
const string s = path(pn).root_path();
lua_pushstring(L, s.c_str());
return 1;
}
int ncm_fs_filename(lua_State* L) {
using std::filesystem::path;
const char *pn = lua_tostring(L, 1); // any path.
if (pn == 0) return 0;
const string s = path(pn).filename();
lua_pushstring(L, s.c_str());
return 1;
}
int ncm_int_to_hex64(lua_State* L) {
lua_Integer n = lua_tointeger(L, 1);
lua_pushstring(L, int_to_hex64(n).c_str());
return 1;
}
int ncm_create_new_file(lua_State* L) {
char buf[4096];
const char* block_fn = lua_tostring(L, 1);
std::size_t csz = lua_tointeger(L, 2);
// TODO: error if the file exists.
std::error_code ec;
const auto not_found_status =
std::filesystem::file_status(std::filesystem::file_type::not_found);
const auto fstatus = std::filesystem::status(block_fn, ec);
if (fstatus != not_found_status) return 0;
auto ofs = std::ofstream(block_fn);
std::size_t n = 0;
while (n < csz) {
std::size_t w = csz - n;
if (w > sizeof buf) {
w = sizeof buf;
}
if (!ofs) break;
ofs.write(buf, w);
if (!ofs) break;
n += w;
}
ofs.close();
lua_pushinteger(L, n);
return 1;
}
const struct luaL_Reg regns [] = {
{"fs_space", ncm_fs_space},
{"fs_hash_value", ncm_fs_hash_value},
{"fs_file_size", ncm_fs_file_size},
{"fs_has_root_path", ncm_fs_has_root_path},
{"fs_root_path", ncm_fs_root_path},
{"fs_filename", ncm_fs_filename},
{"int_to_hex64", ncm_int_to_hex64},
{"create_new_file", ncm_create_new_file},
{NULL, NULL} /* sentinel */
};
} // namespace wfslua
extern "C" int luaopen_ncmcpp (lua_State *L) {
lua_newtable(L);
luaL_setfuncs(L, wfslua::regns, 0);
return 1;
}