1
0
blueloveTH 2 жил өмнө
parent
commit
bc559e98fe
2 өөрчлөгдсөн 44 нэмэгдсэн , 14 устгасан
  1. 5 0
      include/pocketpy/config.h
  2. 39 14
      src/io.cpp

+ 5 - 0
include/pocketpy/config.h

@@ -9,10 +9,15 @@
 /*************** feature settings ***************/
 
 // Whether to compile os-related modules or not
+#ifndef PK_ENABLE_OS                // can be overrided by cmake
 #define PK_ENABLE_OS                1
+#endif
+
 // Enable this if you are working with multi-threading (experimental)
 // This triggers necessary locks to make the VM thread-safe
+#ifndef PK_ENABLE_THREAD            // can be overrided by cmake
 #define PK_ENABLE_THREAD            0
+#endif
 
 // Enable this for `vm->_ceval_on_step`
 #define PK_ENABLE_CEVAL_CALLBACK    0

+ 39 - 14
src/io.cpp

@@ -3,20 +3,45 @@
 
 namespace pkpy{
 
+static FILE* io_fopen(const char* name, const char* mode){
+#if _WIN32
+    FILE* fp;
+    errno_t err = fopen_s(&fp, name, mode);
+    if(err != 0) return nullptr;
+    return fp;
+#else
+    return fopen(name, mode);
+#endif
+}
+
+static size_t io_fread(void* buffer, size_t size, size_t count, FILE* fp){
+#if _WIN32
+    return fread_s(buffer, size, size, count, fp);
+#else
+    return fread(buffer, size, count, fp);
+#endif
+}
+
+#define io_fclose fclose
+#define io_ftell ftell
+#define io_fseek fseek
+#define io_fwrite fwrite
+
+
 Bytes _default_import_handler(const Str& name){
 #if PK_ENABLE_OS
     std::filesystem::path path(name.sv());
     bool exists = std::filesystem::exists(path);
     if(!exists) return Bytes();
     std::string cname = name.str();
-    FILE* fp = fopen(cname.c_str(), "rb");
+    FILE* fp = io_fopen(cname.c_str(), "rb");
     if(!fp) return Bytes();
-    fseek(fp, 0, SEEK_END);
-    std::vector<char> buffer(ftell(fp));
-    fseek(fp, 0, SEEK_SET);
-    size_t sz = fread(buffer.data(), 1, buffer.size(), fp);
+    io_fseek(fp, 0, SEEK_END);
+    std::vector<char> buffer(io_ftell(fp));
+    io_fseek(fp, 0, SEEK_SET);
+    size_t sz = io_fread(buffer.data(), 1, buffer.size(), fp);
     PK_UNUSED(sz);
-    fclose(fp);
+    io_fclose(fp);
     return Bytes(std::move(buffer));
 #else
     return Bytes();
@@ -34,10 +59,10 @@ Bytes _default_import_handler(const Str& name){
 
         vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){
             FileIO& io = CAST(FileIO&, args[0]);
-            fseek(io.fp, 0, SEEK_END);
-            std::vector<char> buffer(ftell(io.fp));
-            fseek(io.fp, 0, SEEK_SET);
-            size_t sz = fread(buffer.data(), 1, buffer.size(), io.fp);
+            io_fseek(io.fp, 0, SEEK_END);
+            std::vector<char> buffer(io_ftell(io.fp));
+            io_fseek(io.fp, 0, SEEK_SET);
+            size_t sz = io_fread(buffer.data(), 1, buffer.size(), io.fp);
             PK_UNUSED(sz);
             Bytes b(std::move(buffer));
             if(io.is_text()) return VAR(b.str());
@@ -48,10 +73,10 @@ Bytes _default_import_handler(const Str& name){
             FileIO& io = CAST(FileIO&, args[0]);
             if(io.is_text()){
                 Str& s = CAST(Str&, args[1]);
-                fwrite(s.data, 1, s.length(), io.fp);
+                io_fwrite(s.data, 1, s.length(), io.fp);
             }else{
                 Bytes& buffer = CAST(Bytes&, args[1]);
-                fwrite(buffer.data(), 1, buffer.size(), io.fp);
+                io_fwrite(buffer.data(), 1, buffer.size(), io.fp);
             }
             return vm->None;
         });
@@ -72,13 +97,13 @@ Bytes _default_import_handler(const Str& name){
     }
 
     FileIO::FileIO(VM* vm, std::string file, std::string mode): file(file), mode(mode) {
-        fp = fopen(file.c_str(), mode.c_str());
+        fp = io_fopen(file.c_str(), mode.c_str());
         if(!fp) vm->IOError(strerror(errno));
     }
 
     void FileIO::close(){
         if(fp == nullptr) return;
-        fclose(fp);
+        io_fclose(fp);
         fp = nullptr;
     }