This commit is contained in:
blueloveTH 2023-04-30 22:08:42 +08:00
parent 70e8a60f8b
commit f9ed405bf5
2 changed files with 12 additions and 10 deletions

View File

@ -10,6 +10,17 @@
#include <filesystem>
namespace pkpy{
inline int _ = set_read_file_cwd([](const Str& name){
std::filesystem::path path(name.sv());
bool exists = std::filesystem::exists(path);
if(!exists) return Bytes();
std::ifstream ifs(path, std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(ifs), {});
ifs.close();
return Bytes(std::move(buffer));
});
struct FileIO {
PY_CLASS(FileIO, io, FileIO)
@ -83,16 +94,6 @@ struct FileIO {
};
inline void add_module_io(VM* vm){
_read_file_cwd = [](const Str& name){
std::filesystem::path path(name.sv());
bool exists = std::filesystem::exists(path);
if(!exists) return Bytes();
std::ifstream ifs(path, std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(ifs), {});
ifs.close();
return Bytes(std::move(buffer));
};
PyObject* mod = vm->new_module("io");
FileIO::register_class(vm, mod);
vm->bind_builtin_func<2>("open", [](VM* vm, ArgsView args){

View File

@ -26,6 +26,7 @@ namespace pkpy{
typedef Bytes (*ReadFileCwdFunc)(const Str& name);
inline ReadFileCwdFunc _read_file_cwd = [](const Str& name) { return Bytes(); };
inline int set_read_file_cwd(ReadFileCwdFunc func) { _read_file_cwd = func; return 0; }
#define DEF_NATIVE_2(ctype, ptype) \
template<> inline ctype py_cast<ctype>(VM* vm, PyObject* obj) { \