filesystem.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // MIT License
  2. // Copyright(c) 2024 FastWeb - fwlua.com - nianhua
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files(the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions :
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ## Additional Terms for Commercial Use
  19. // This software is licensed for personal, educational, and non - commercial use.
  20. // For commercial use or use within a company, organization, or institution, a
  21. // separate commercial license is required.To obtain a commercial license,
  22. // please contact
  23. // EMail:1585346868@qq.com
  24. // Mobile:17367918735
  25. // QQ:1585346868
  26. #include "filesystem.h"
  27. #include "util/file.h"
  28. sol::table module::filesystem::list(const std::string& dirpath, const std::string& regex, sol::this_state s)
  29. {
  30. auto map = ylib::file::traverse(dirpath, regex);
  31. sol::state_view lua(s);
  32. sol::table result_table = lua.create_table();
  33. int row_num = 1;
  34. for_iter(iter, map)
  35. {
  36. sol::table row = lua.create_table();
  37. row["file"] = iter->first;
  38. row["type"] = (int)iter->second;
  39. result_table[row_num++] = row;
  40. }
  41. return result_table;
  42. }
  43. void module::filesystem::copy_dir(const std::string& src_dir, const std::string& dst_dir)
  44. {
  45. ylib::file::copy_dir(src_dir, dst_dir);
  46. }
  47. bool module::filesystem::create_dir(const std::string& dirpath)
  48. {
  49. return ylib::file::create_dir(dirpath, true);
  50. }
  51. sol::object module::filesystem::read(const std::string& filepath, sol::this_state s)
  52. {
  53. sol::object result;
  54. ylib::buffer data;
  55. if (ylib::file::read(filepath, data) == false)
  56. result = sol::make_object(s, data.to_string());
  57. else
  58. result = sol::make_object(s, sol::nil);
  59. return result;
  60. }
  61. bool module::filesystem::write(const std::string& filepath, const std::string& data)
  62. {
  63. return ylib::file::write(filepath, data);
  64. }
  65. bool module::filesystem::remove(const std::string& filepath)
  66. {
  67. return ylib::file::remove(filepath);
  68. }
  69. bool module::filesystem::remove_dir(const std::string& dirpath, bool recycle)
  70. {
  71. #ifdef _WIN32
  72. return ylib::file::remove_dir(dirpath, recycle);
  73. #else
  74. return ylib::file::remove_dir(dirpath);
  75. #endif
  76. }
  77. bool module::filesystem::exist(const std::string& filepath)
  78. {
  79. return ylib::file::exist(filepath);
  80. }
  81. bool module::filesystem::exist_dir(const std::string& dirpath)
  82. {
  83. return ylib::file::exist_dir(dirpath);
  84. }
  85. int64 module::filesystem::size(const std::string& filepath)
  86. {
  87. return ylib::file::size(filepath);
  88. }
  89. bool module::filesystem::copy(const std::string& src, const std::string& dst)
  90. {
  91. return ylib::file::copy(src, dst);
  92. }
  93. timestamp module::filesystem::last_write_time(const std::string& filepath)
  94. {
  95. return ylib::file::last_write_time(filepath);
  96. }
  97. void module::filesystem::regist(sol::state* lua)
  98. {
  99. (*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE;
  100. (*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY;
  101. lua->new_usertype<module::filesystem>("fs",
  102. "list", &module::filesystem::list,
  103. "copy_dir", &module::filesystem::copy_dir,
  104. "create_dir", &module::filesystem::create_dir,
  105. "read", module::filesystem::read,
  106. "write", module::filesystem::write,
  107. "remove", module::filesystem::remove,
  108. "remove_dir", module::filesystem::remove_dir,
  109. "exist", module::filesystem::exist,
  110. "exist_dir", module::filesystem::exist_dir,
  111. "size", module::filesystem::size,
  112. "copy", module::filesystem::copy,
  113. "last_write_time", module::filesystem::last_write_time
  114. );
  115. }