ini.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // QQ:1585346868
  25. #include "ini.h"
  26. module::ini::ini()
  27. {
  28. }
  29. module::ini::~ini()
  30. {
  31. }
  32. bool module::ini::open(const std::string& filepath)
  33. {
  34. return m_ini.open(filepath);
  35. }
  36. void module::ini::close()
  37. {
  38. m_ini.close();
  39. }
  40. std::string module::ini::read(const std::string& node, const std::string& key, const std::string& default_value) const
  41. {
  42. return m_ini.read(node,key,default_value);
  43. }
  44. bool module::ini::write(const std::string& node, const std::string& key, const std::string& value)
  45. {
  46. return m_ini.write(node,key,value);
  47. }
  48. bool module::ini::del(const std::string& node, const std::string& key)
  49. {
  50. return m_ini.del(node, key);
  51. }
  52. sol::table module::ini::nodes(sol::this_state s)
  53. {
  54. sol::state_view lua(s);
  55. sol::table result_table = lua.create_table();
  56. int row_num = 1;
  57. auto names = m_ini.names();
  58. for_iter(iter, names)
  59. result_table[row_num++] = *iter;
  60. return result_table;
  61. }
  62. sol::table module::ini::keys(const std::string& node, sol::this_state s)
  63. {
  64. sol::state_view lua(s);
  65. sol::table result_table = lua.create_table();
  66. int row_num = 1;
  67. auto names = m_ini.keys(node);
  68. for_iter(iter, names)
  69. result_table[row_num++] = *iter;
  70. return result_table;
  71. }
  72. bool module::ini::exist_key(const std::string& node, const std::string& key)
  73. {
  74. return m_ini.exist_key(node, key);
  75. }
  76. bool module::ini::exist_node(const std::string& node)
  77. {
  78. return m_ini.exist_name(node);
  79. }
  80. sol::table module::ini::table(sol::this_state s)
  81. {
  82. sol::state_view lua(s);
  83. sol::table result_table = lua.create_table();
  84. auto nodes = m_ini.names();
  85. for_iter(iter, nodes)
  86. {
  87. sol::table node_table = lua.create_table();
  88. auto keys = m_ini.keys(*iter);
  89. for (size_t i = 0; i < keys.size(); i++)
  90. {
  91. node_table[keys[i]] = m_ini.read(*iter,keys[i]);
  92. }
  93. result_table[*iter] = node_table;
  94. }
  95. return result_table;
  96. }
  97. void module::ini::regist(sol::state* lua)
  98. {
  99. lua->new_usertype<module::ini>("ini",
  100. "new", sol::constructors<module::ini()>(),
  101. "close", &module::ini::close,
  102. "del", &module::ini::del,
  103. "exist_key", &module::ini::exist_key,
  104. "exist_node", &module::ini::exist_node,
  105. "keys", &module::ini::keys,
  106. "nodes", &module::ini::nodes,
  107. "open", &module::ini::open,
  108. "read", &module::ini::read,
  109. "table", &module::ini::table,
  110. "write", &module::ini::write
  111. );
  112. }