io.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "pocketpy/io.h"
  2. #include "pocketpy/common.h"
  3. namespace pkpy{
  4. static FILE* io_fopen(const char* name, const char* mode){
  5. #if _WIN32
  6. FILE* fp;
  7. errno_t err = fopen_s(&fp, name, mode);
  8. if(err != 0) return nullptr;
  9. return fp;
  10. #else
  11. return fopen(name, mode);
  12. #endif
  13. }
  14. static size_t io_fread(void* buffer, size_t size, size_t count, FILE* fp){
  15. #if _WIN32
  16. return fread_s(buffer, std::numeric_limits<size_t>::max(), size, count, fp);
  17. #else
  18. return fread(buffer, size, count, fp);
  19. #endif
  20. }
  21. Bytes _default_import_handler(const Str& name){
  22. #if PK_ENABLE_OS
  23. std::filesystem::path path(name.sv());
  24. bool exists = std::filesystem::exists(path);
  25. if(!exists) return Bytes();
  26. std::string cname = name.str();
  27. FILE* fp = io_fopen(cname.c_str(), "rb");
  28. if(!fp) return Bytes();
  29. fseek(fp, 0, SEEK_END);
  30. std::vector<char> buffer(ftell(fp));
  31. fseek(fp, 0, SEEK_SET);
  32. size_t sz = io_fread(buffer.data(), 1, buffer.size(), fp);
  33. PK_UNUSED(sz);
  34. fclose(fp);
  35. return Bytes(std::move(buffer));
  36. #else
  37. return Bytes();
  38. #endif
  39. };
  40. #if PK_ENABLE_OS
  41. void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){
  42. vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
  43. return VAR_T(FileIO,
  44. vm, CAST(Str&, args[1]).str(), CAST(Str&, args[2]).str()
  45. );
  46. });
  47. vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){
  48. FileIO& io = CAST(FileIO&, args[0]);
  49. fseek(io.fp, 0, SEEK_END);
  50. std::vector<char> buffer(ftell(io.fp));
  51. fseek(io.fp, 0, SEEK_SET);
  52. size_t sz = io_fread(buffer.data(), 1, buffer.size(), io.fp);
  53. PK_UNUSED(sz);
  54. Bytes b(std::move(buffer));
  55. if(io.is_text()) return VAR(b.str());
  56. return VAR(std::move(b));
  57. });
  58. vm->bind_method<1>(type, "write", [](VM* vm, ArgsView args){
  59. FileIO& io = CAST(FileIO&, args[0]);
  60. if(io.is_text()){
  61. Str& s = CAST(Str&, args[1]);
  62. fwrite(s.data, 1, s.length(), io.fp);
  63. }else{
  64. Bytes& buffer = CAST(Bytes&, args[1]);
  65. fwrite(buffer.data(), 1, buffer.size(), io.fp);
  66. }
  67. return vm->None;
  68. });
  69. vm->bind_method<0>(type, "close", [](VM* vm, ArgsView args){
  70. FileIO& io = CAST(FileIO&, args[0]);
  71. io.close();
  72. return vm->None;
  73. });
  74. vm->bind_method<0>(type, "__exit__", [](VM* vm, ArgsView args){
  75. FileIO& io = CAST(FileIO&, args[0]);
  76. io.close();
  77. return vm->None;
  78. });
  79. vm->bind_method<0>(type, "__enter__", PK_LAMBDA(vm->None));
  80. }
  81. FileIO::FileIO(VM* vm, std::string file, std::string mode): file(file), mode(mode) {
  82. fp = io_fopen(file.c_str(), mode.c_str());
  83. if(!fp) vm->IOError(strerror(errno));
  84. }
  85. void FileIO::close(){
  86. if(fp == nullptr) return;
  87. fclose(fp);
  88. fp = nullptr;
  89. }
  90. #endif
  91. void add_module_io(VM* vm){
  92. #if PK_ENABLE_OS
  93. PyObject* mod = vm->new_module("io");
  94. FileIO::register_class(vm, mod);
  95. vm->bind(vm->builtins, "open(path, mode='r')", [](VM* vm, ArgsView args){
  96. PK_LOCAL_STATIC StrName m_io("io");
  97. PK_LOCAL_STATIC StrName m_FileIO("FileIO");
  98. return vm->call(vm->_modules[m_io]->attr(m_FileIO), args[0], args[1]);
  99. });
  100. #endif
  101. }
  102. void add_module_os(VM* vm){
  103. #if PK_ENABLE_OS
  104. PyObject* mod = vm->new_module("os");
  105. PyObject* path_obj = vm->heap.gcnew<DummyInstance>(vm->tp_object);
  106. mod->attr().set("path", path_obj);
  107. // Working directory is shared by all VMs!!
  108. vm->bind_func<0>(mod, "getcwd", [](VM* vm, ArgsView args){
  109. return VAR(std::filesystem::current_path().string());
  110. });
  111. vm->bind_func<1>(mod, "chdir", [](VM* vm, ArgsView args){
  112. std::filesystem::path path(CAST(Str&, args[0]).sv());
  113. std::filesystem::current_path(path);
  114. return vm->None;
  115. });
  116. // system
  117. vm->bind_func<1>(mod, "system", [](VM* vm, ArgsView args){
  118. std::string cmd = CAST(Str&, args[0]).str();
  119. int ret = system(cmd.c_str());
  120. return VAR(ret);
  121. });
  122. vm->bind_func<1>(mod, "listdir", [](VM* vm, ArgsView args){
  123. std::filesystem::path path(CAST(Str&, args[0]).sv());
  124. std::filesystem::directory_iterator di;
  125. try{
  126. di = std::filesystem::directory_iterator(path);
  127. }catch(std::filesystem::filesystem_error& e){
  128. std::string msg = e.what();
  129. auto pos = msg.find_last_of(":");
  130. if(pos != std::string::npos) msg = msg.substr(pos + 1);
  131. vm->IOError(Str(msg).lstrip());
  132. }
  133. List ret;
  134. for(auto& p: di) ret.push_back(VAR(p.path().filename().string()));
  135. return VAR(ret);
  136. });
  137. vm->bind_func<1>(mod, "remove", [](VM* vm, ArgsView args){
  138. std::filesystem::path path(CAST(Str&, args[0]).sv());
  139. bool ok = std::filesystem::remove(path);
  140. if(!ok) vm->IOError("operation failed");
  141. return vm->None;
  142. });
  143. vm->bind_func<1>(mod, "mkdir", [](VM* vm, ArgsView args){
  144. std::filesystem::path path(CAST(Str&, args[0]).sv());
  145. bool ok = std::filesystem::create_directory(path);
  146. if(!ok) vm->IOError("operation failed");
  147. return vm->None;
  148. });
  149. vm->bind_func<1>(mod, "rmdir", [](VM* vm, ArgsView args){
  150. std::filesystem::path path(CAST(Str&, args[0]).sv());
  151. bool ok = std::filesystem::remove(path);
  152. if(!ok) vm->IOError("operation failed");
  153. return vm->None;
  154. });
  155. vm->bind_func<-1>(path_obj, "join", [](VM* vm, ArgsView args){
  156. std::filesystem::path path;
  157. for(int i=0; i<args.size(); i++){
  158. path /= CAST(Str&, args[i]).sv();
  159. }
  160. return VAR(path.string());
  161. });
  162. vm->bind_func<1>(path_obj, "exists", [](VM* vm, ArgsView args){
  163. std::filesystem::path path(CAST(Str&, args[0]).sv());
  164. bool exists = std::filesystem::exists(path);
  165. return VAR(exists);
  166. });
  167. vm->bind_func<1>(path_obj, "basename", [](VM* vm, ArgsView args){
  168. std::filesystem::path path(CAST(Str&, args[0]).sv());
  169. return VAR(path.filename().string());
  170. });
  171. #endif
  172. }
  173. } // namespace pkpy