Skip to content

Commit a71130e

Browse files
committed
Fixed some CC:T errors
1 parent 61e5d46 commit a71130e

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

craftos2-lua

resources/CCT-Test-Bootstrap.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
config.set("abortTimeout", 3000) -- to speed things up a bit
2+
config.set("standardsMode", true)
23
config.add("http_blacklist", "$private")
34
if ... == "debugger" then
45
periphemu.create("left", "debugger")

src/apis/fs.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ static std::vector<path_t> fixpath_multiple(Computer *comp, std::string path) {
123123
static std::string normalizePath(const path_t& basePath) {
124124
path_t cleanPath;
125125
for (const auto& p : basePath) {
126-
if (std::regex_match(p.native(), pathregex("^\\.\\.\\.+$"))) cleanPath /= ".";
127-
else cleanPath /= p;
126+
path_t::string_type str = p.native();
127+
str.erase(std::remove_if(str.begin(), str.end(), [](path_t::string_type::value_type c)->bool {return c == '"' || c == '*' || c == ':' || c == '<' || c == '>' || c == '?' || c == '|' || c < 32;}), str.end());
128+
if (std::regex_match(str, pathregex("^\\.\\.\\.+$"))) cleanPath /= ".";
129+
else cleanPath /= path_t(str);
128130
}
129131
cleanPath = cleanPath.lexically_normal();
130132
if (path_t::preferred_separator != (path_t::value_type)'/') {
@@ -246,15 +248,16 @@ static int fs_isReadOnly(lua_State *L) {
246248

247249
static int fs_getName(lua_State *L) {
248250
lastCFunction = __func__;
249-
pushstring(L, path_t(normalizePath(checkstring(L, 1))).filename().string());
251+
std::string retval = path_t(normalizePath(checkstring(L, 1))).filename().string();
252+
if (retval.empty()) lua_pushliteral(L, "root");
253+
else pushstring(L, retval);
250254
return 1;
251255
}
252256

253257
static int fs_getDrive(lua_State *L) {
254258
lastCFunction = __func__;
255259
std::string retval;
256-
std::string str = checkstring(L, 1);
257-
fixpath_mkdir(get_comp(L), str + "/a", false, &retval);
260+
if (fixpath(get_comp(L), checkstring(L, 1), true, true, &retval).empty()) return 0;
258261
lua_pushstring(L, retval.c_str());
259262
return 1;
260263
}

src/apis/handles/fs_handle.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ int fs_handle_readAll(lua_State *L) {
7474
lua_pushliteral(L, "");
7575
return 1;
7676
}
77-
if (!fp->good()) return 0;
77+
if (fp->bad() || fp->fail()) return 0;
7878
const long pos = (long)fp->tellg();
7979
fp->seekg(0, std::ios::end);
80-
if (!fp->good()) return 0;
80+
if (fp->bad() || fp->fail()) return 0;
8181
long size = (long)fp->tellg() - pos;
8282
char * retval = new char[size + 1];
8383
memset(retval, 0, size + 1);
@@ -170,8 +170,12 @@ int fs_handle_readAllByte(lua_State *L) {
170170
lastCFunction = __func__;
171171
std::iostream * fp = *(std::iostream**)lua_touserdata(L, lua_upvalueindex(1));
172172
if (fp == NULL) return luaL_error(L, "attempt to use a closed file");
173-
if (fp->eof()) return 0;
174-
if (!fp->good()) return luaL_error(L, "Could not read file");
173+
if (fp->eof()) {
174+
if (fp->tellg() < 1) return 0;
175+
lua_pushliteral(L, "");
176+
return 1;
177+
}
178+
if (fp->bad() || fp->fail()) return luaL_error(L, "Could not read file");
175179
std::streampos pos = fp->tellg();
176180
fp->seekg(0, std::ios_base::end);
177181
size_t size = fp->tellg() - pos;

0 commit comments

Comments
 (0)