mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
...
This commit is contained in:
parent
70e8a60f8b
commit
f9ed405bf5
21
src/io.h
21
src/io.h
@ -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){
|
||||
|
1
src/vm.h
1
src/vm.h
@ -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) { \
|
||||
|
Loading…
x
Reference in New Issue
Block a user