xx 2 лет назад
Родитель
Сommit
3e86f941cf
2 измененных файлов с 132 добавлено и 0 удалено
  1. 102 0
      src/module/ini.cpp
  2. 30 0
      src/module/ini.h

+ 102 - 0
src/module/ini.cpp

@@ -0,0 +1,102 @@
+#include "ini.h"
+
+module::ini::ini()
+{
+}
+
+module::ini::~ini()
+{
+}
+
+bool module::ini::open(const std::string& filepath)
+{
+	return m_ini.open(filepath);
+}
+
+void module::ini::close()
+{
+	m_ini.close();
+}
+
+std::string module::ini::read(const std::string& node, const std::string& key, const std::string& default_value) const
+{
+	return m_ini.read(node,key,default_value);
+}
+
+bool module::ini::write(const std::string& node, const std::string& key, const std::string& value)
+{
+	return m_ini.write(node,key,value);
+}
+
+bool module::ini::del(const std::string& node, const std::string& key)
+{
+	return m_ini.del(node, key);
+}
+
+sol::table module::ini::nodes(sol::this_state s)
+{
+    sol::state_view lua(s);
+    sol::table result_table = lua.create_table();
+    int row_num = 1;
+    auto names = m_ini.names();
+    for_iter(iter, names)
+        result_table[row_num++] = *iter;
+    return result_table;
+}
+
+sol::table module::ini::keys(const std::string& node, sol::this_state s)
+{
+    sol::state_view lua(s);
+    sol::table result_table = lua.create_table();
+    int row_num = 1;
+    auto names = m_ini.keys(node);
+    for_iter(iter, names)
+        result_table[row_num++] = *iter;
+    return result_table;
+}
+bool module::ini::exist_key(const std::string& node, const std::string& key)
+{
+    return m_ini.exist_key(node, key);
+}
+
+bool module::ini::exist_node(const std::string& node)
+{
+    return m_ini.exist_name(node);
+}
+
+sol::table module::ini::table(sol::this_state s)
+{
+    sol::state_view lua(s);
+    sol::table result_table = lua.create_table();
+
+
+    auto nodes = m_ini.names();
+    for_iter(iter, nodes)
+    {
+        sol::table node_table = lua.create_table();
+        auto keys = m_ini.keys(*iter);
+        for (size_t i = 0; i < keys.size(); i++)
+        {
+            node_table[keys[i]] = m_ini.read(*iter,keys[i]);
+        }
+        result_table[*iter] = node_table;
+    }
+    return result_table;
+}
+
+void module::ini::regist(sol::state* lua)
+{
+    lua->new_usertype<module::ini>("ini",
+        "new", sol::constructors<module::ini()>(),
+        "close", &module::ini::close,
+        "del", &module::ini::del,
+        "exist_key", &module::ini::exist_key,
+        "exist_node", &module::ini::exist_node,
+        "keys", &module::ini::keys,
+        "nodes", &module::ini::nodes,
+        "open", &module::ini::open,
+        "read", &module::ini::read,
+        "table", &module::ini::table,
+        "write", &module::ini::write
+    );
+}

+ 30 - 0
src/module/ini.h

@@ -0,0 +1,30 @@
+#pragma once
+#include "sol/sol.hpp"
+#include "util/ini.h"
+namespace module
+{
+	/// <summary>
+	/// INI配置
+	/// </summary>
+	class ini {
+	public:
+		ini();
+		~ini();
+		bool open(const std::string& filepath);
+		void close();
+		std::string read(const std::string& node, const std::string& key, const std::string& default_value) const;
+		bool write(const std::string& node, const std::string& key, const std::string& value);
+		bool del(const std::string& node, const std::string& key);
+		sol::table nodes(sol::this_state s);
+		sol::table keys(const std::string& node, sol::this_state s);
+		bool exist_key(const std::string& node, const std::string& key);
+		bool exist_node(const std::string& node);
+		sol::table table(sol::this_state s);
+
+		static void regist(sol::state* lua);
+	private:
+		ylib::ini m_ini;
+	};
+
+}
+