io.cpp 5.5 KB

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