Răsfoiți Sursa

add GetKeys for iniparser (#245)

Co-authored-by: zhenshf <zhenshf@ehualu.com>
spiderpig 3 ani în urmă
părinte
comite
4793f7275b
2 a modificat fișierele cu 22 adăugiri și 2 ștergeri
  1. 19 1
      cpputil/iniparser.cpp
  2. 3 1
      cpputil/iniparser.h

+ 19 - 1
cpputil/iniparser.cpp

@@ -1,6 +1,5 @@
 #include "iniparser.h"
 
-#include <list>
 #include <sstream>
 
 #include "hdef.h"
@@ -286,6 +285,25 @@ int IniParser::SaveAs(const char* filepath) {
     return 0;
 }
 
+std::list<std::string> IniParser::GetKeys(const std::string& section)
+{
+    std::list<std::string> ret;
+    if (root_ == NULL) return std::move(ret);
+
+    IniNode* pSection = root_;
+    if (section.length() != 0) {
+        pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
+        if (pSection == NULL) return std::move(ret);
+    }
+
+    for (auto pNode : pSection->children) {
+        if (pNode->type == IniNode::INI_NODE_TYPE_KEY_VALUE) {
+            ret.push_back(pNode->label);
+        }
+    }
+    return std::move(ret);
+}
+
 std::string IniParser::GetValue(const std::string& key, const std::string& section) {
     if (root_ == NULL)  return "";
 

+ 3 - 1
cpputil/iniparser.h

@@ -2,6 +2,7 @@
 #define HV_INI_PARSER_H_
 
 #include <string>
+#include <list>
 
 #include "hexport.h"
 
@@ -24,7 +25,8 @@ public:
     std::string DumpString();
     int Save();
     int SaveAs(const char* filepath);
-
+    
+    std::list<std::string> GetKeys(const std::string& section = "");
     std::string GetValue(const std::string& key, const std::string& section = "");
     void        SetValue(const std::string& key, const std::string& value, const std::string& section = "");