Skip to content

Commit

Permalink
Removed binary mode; added r+/w+ file open modes
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Nov 14, 2023
1 parent 203c278 commit c5690ed
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 226 deletions.
2 changes: 1 addition & 1 deletion craftos2-lua
106 changes: 53 additions & 53 deletions src/apis/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,14 @@ static int fs_open(lua_State *L) {
lastCFunction = __func__;
Computer * computer = get_comp(L);
const char * mode = luaL_checkstring(L, 2);
if ((mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') || (!(mode[1] == 'b' && mode[2] == '\0') && mode[1] != '\0')) luaL_error(L, "%s: Unsupported mode", mode);
if (
(mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') ||
(
!(mode[1] == '+' && mode[2] == 'b' && mode[3] == '\0') &&
!(mode[1] == '+' && mode[2] == '\0') &&
!(mode[1] == 'b' && mode[2] == '\0') &&
mode[1] != '\0'
)) luaL_error(L, "%s: Unsupported mode", mode);
std::string str = checkstring(L, 1);
const path_t path = mode[0] == 'r' ? fixpath(get_comp(L), str, true) : fixpath_mkdir(get_comp(L), str);
if (path.empty()) {
Expand Down Expand Up @@ -505,14 +512,21 @@ static int fs_open(lua_State *L) {
std::fstream ** fp = (std::fstream**)lua_newuserdata(L, sizeof(std::fstream*));
fpid = lua_gettop(L);
std::ios::openmode flags = std::ios::binary;
if (strchr(mode, 'r')) flags |= std::ios::in;
else if (strchr(mode, 'w')) flags |= std::ios::out | std::ios::trunc;
else if (strchr(mode, 'a')) flags |= std::ios::in | std::ios::out | std::ios::ate;
if (strchr(mode, 'r')) {
flags |= std::ios::in;
if (strchr(mode, '+')) flags |= std::ios::out;
} else if (strchr(mode, 'w')) {
flags |= std::ios::out | std::ios::trunc;
if (strchr(mode, '+')) flags |= std::ios::in;
} else if (strchr(mode, 'a')) {
flags |= std::ios::in | std::ios::out | std::ios::ate;
if (strchr(mode, '+')) flags |= std::ios::in;
}
*fp = new std::fstream(path, flags);
if (!(*fp)->is_open()) {
bool ok = false;
if (strchr(mode, 'a')) {
(*fp)->open(path, std::ios::out | std::ios::trunc);
(*fp)->open(path, (flags & ~std::ios::ate) | std::ios::trunc);
ok = (*fp)->is_open();
}
if (!ok) {
Expand Down Expand Up @@ -540,42 +554,24 @@ static int fs_open(lua_State *L) {
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_close, 1);
lua_settable(L, -3);
if (strcmp(mode, "r") == 0) {
lua_pushstring(L, "readAll");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_readAll, 1);
lua_settable(L, -3);

lua_pushstring(L, "readLine");
lua_pushvalue(L, fpid);
lua_pushboolean(L, false);
lua_pushcclosure(L, fs_handle_readLine, 2);
lua_settable(L, -3);

lua_pushstring(L, "read");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_readChar, 1);
lua_settable(L, -3);
} else if (strcmp(mode, "w") == 0 || strcmp(mode, "a") == 0) {
lua_pushstring(L, "write");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_writeString, 1);
lua_settable(L, -3);

lua_pushstring(L, "writeLine");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_writeLine, 1);
lua_settable(L, -3);
lua_pushstring(L, "seek");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_seek, 1);
lua_settable(L, -3);

lua_pushstring(L, "flush");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_flush, 1);
lua_settable(L, -3);
} else if (strcmp(mode, "rb") == 0) {
lua_pushstring(L, "read");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_readByte, 1);
lua_settable(L, -3);
if (mode[0] == 'r' || strchr(mode, '+')) {
if (strchr(mode, 'b')) {
lua_pushstring(L, "read");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_readByte, 1);
lua_settable(L, -3);
} else {
lua_pushstring(L, "read");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_readChar, 1);
lua_settable(L, -3);
}

lua_pushstring(L, "readAll");
lua_pushvalue(L, fpid);
Expand All @@ -584,30 +580,34 @@ static int fs_open(lua_State *L) {

lua_pushstring(L, "readLine");
lua_pushvalue(L, fpid);
lua_pushboolean(L, true);
lua_pushcclosure(L, fs_handle_readLine, 2);
lua_pushcclosure(L, fs_handle_readLine, 1);
lua_settable(L, -3);
}

lua_pushstring(L, "seek");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_seek, 1);
lua_settable(L, -3);
} else if (strcmp(mode, "wb") == 0 || strcmp(mode, "ab") == 0) {
lua_pushstring(L, "write");
if (mode[0] == 'w' || mode[0] == 'a' || strchr(mode, '+')) {
if (strchr(mode, 'b')) {
lua_pushstring(L, "write");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_writeByte, 1);
lua_settable(L, -3);
} else {
lua_pushstring(L, "write");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_writeString, 1);
lua_settable(L, -3);
}

lua_pushstring(L, "writeLine");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_writeByte, 1);
lua_pushcclosure(L, fs_handle_writeLine, 1);
lua_settable(L, -3);

lua_pushstring(L, "flush");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_flush, 1);
lua_settable(L, -3);

lua_pushstring(L, "seek");
lua_pushvalue(L, fpid);
lua_pushcclosure(L, fs_handle_seek, 1);
lua_settable(L, -3);
}

computer->files_open++;
return 1;
}
Expand Down
73 changes: 21 additions & 52 deletions src/apis/handles/fs_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <codecvt>
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include "fs_handle.hpp"
#include "../../util.hpp"
Expand Down Expand Up @@ -102,10 +100,8 @@ int fs_handle_readLine(lua_State *L) {
std::string retval;
std::getline(*fp, retval);
if (retval.empty() && fp->eof()) return 0;
if (*retval.rbegin() == '\r' && !lua_toboolean(L, lua_upvalueindex(2))) retval = retval.substr(0, retval.size()-1);
if (lua_toboolean(L, 1) && fp->good()) retval += '\n';
const std::string out = lua_toboolean(L, lua_upvalueindex(2)) ? retval : makeASCIISafe(retval.c_str(), retval.size());
lua_pushlstring(L, out.c_str(), out.length());
lua_pushlstring(L, retval.c_str(), retval.length());
return 1;
}

Expand All @@ -115,43 +111,24 @@ int fs_handle_readChar(lua_State *L) {
if (fp == NULL) return luaL_error(L, "attempt to use a closed file");
if (fp->eof()) return 0;
if (!fp->good()) luaL_error(L, "Could not read file");
std::string retval;
for (int i = 0; i < luaL_optinteger(L, 1, 1) && !fp->eof(); i++) {
uint32_t codepoint;
const int c = fp->get();
if (c == EOF) break;
else if (c > 0x7F) {
if (c & 64) {
const int c2 = fp->get();
if (c2 == EOF) {retval += '?'; break;}
else if (c2 < 0x80 || c2 & 64) codepoint = 1U<<31;
else if (c & 32) {
const int c3 = fp->get();
if (c3 == EOF) {retval += '?'; break;}
else if (c3 < 0x80 || c3 & 64) codepoint = 1U<<31;
else if (c & 16) {
if (c & 8) codepoint = 1U<<31;
else {
const int c4 = fp->get();
if (c4 == EOF) {retval += '?'; break;}
else if (c4 < 0x80 || c4 & 64) codepoint = 1U<<31;
else codepoint = ((c & 0x7) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F);
}
} else codepoint = ((c & 0xF) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
} else codepoint = ((c & 0x1F) << 6) | (c2 & 0x3F);
} else codepoint = 1U<<31;
} else codepoint = (unsigned char)c;
if (codepoint > 255) retval += '?';
else {
if (codepoint == '\r') {
const int nextc = fp->get();
if (nextc == '\n') codepoint = nextc;
else fp->putback((char)nextc);
}
retval += (char)codepoint;
if (lua_isnumber(L, 1)) {
const size_t s = lua_tointeger(L, 1);
if (s == 0) {
if (fp->peek() == EOF || fp->eof()) return 0;
lua_pushstring(L, "");
return 1;
}
char* retval = new char[s];
fp->read(retval, s);
const size_t actual = fp->gcount();
if (actual == 0) {delete[] retval; return 0;}
lua_pushlstring(L, retval, actual);
delete[] retval;
} else {
const int retval = fp->get();
if (retval == EOF || fp->eof()) return 0;
lua_pushlstring(L, (const char *)&retval, 1);
}
lua_pushlstring(L, retval.c_str(), retval.length());
return 1;
}

Expand Down Expand Up @@ -208,12 +185,8 @@ int fs_handle_writeString(lua_State *L) {
if (lua_isnoneornil(L, 1)) return 0;
else if (!lua_isstring(L, 1) && !lua_isnumber(L, 1)) luaL_error(L, "bad argument #1 (string expected, got %s)", lua_typename(L, lua_type(L, 1)));
if (fp->fail()) luaL_error(L, "Could not write file");
std::string str(lua_tostring(L, 1), lua_rawlen(L, 1));
std::wstring wstr;
for (unsigned char c : str) wstr += (wchar_t)c;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
const std::string newstr = converter.to_bytes(wstr);
fp->write(newstr.c_str(), newstr.size());
size_t sz = 0;
fp->write(lua_tolstring(L, 1, &sz), sz);
return 0;
}

Expand All @@ -224,12 +197,8 @@ int fs_handle_writeLine(lua_State *L) {
if (lua_isnoneornil(L, 1)) return 0;
else if (!lua_isstring(L, 1) && !lua_isnumber(L, 1)) luaL_error(L, "bad argument #1 (string expected, got %s)", lua_typename(L, lua_type(L, 1)));
if (fp->fail()) luaL_error(L, "Could not write file");
std::string str(lua_tostring(L, 1), lua_rawlen(L, 1));
std::wstring wstr;
for (unsigned char c : str) wstr += (wchar_t)c;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
const std::string newstr = converter.to_bytes(wstr);
fp->write(newstr.c_str(), newstr.size());
size_t sz = 0;
fp->write(lua_tolstring(L, 1, &sz), sz);
fp->put('\n');
return 0;
}
Expand Down
14 changes: 1 addition & 13 deletions src/apis/handles/http_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#ifndef __EMSCRIPTEN__
#include <cstdlib>
#include <codecvt>
#include <locale>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPServerResponse.h>
Expand Down Expand Up @@ -70,17 +68,7 @@ int http_handle_readAll(lua_State *L) {
ret.append(buffer, sizeof(buffer));
ret.append(buffer, handle->stream->gcount());
ret.erase(std::remove(ret.begin(), ret.end(), '\r'), ret.end());
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wstr;
try {wstr = converter.from_bytes(ret.c_str(), ret.c_str() + ret.length());}
catch (std::exception & e) {
fprintf(stderr, "http_handle_readAll: Error decoding UTF-8: %s\n", e.what());
lua_pushlstring(L, ret.c_str(), ret.length());
return 1;
}
std::string out;
for (wchar_t c : wstr) {if (c < 256) out += (char)c; else out += '?';}
lua_pushlstring(L, out.c_str(), out.length());
lua_pushlstring(L, ret.c_str(), ret.length());
return 1;
}

Expand Down
Loading

0 comments on commit c5690ed

Please sign in to comment.