1
0
ithewei 6 жил өмнө
parent
commit
fa864650c4

+ 1 - 1
examples/httpd.cpp

@@ -109,7 +109,7 @@ int parse_confile(const char* confile) {
         port = atoi(szPort);
     }
     if (port == 0) {
-        port = atoi(ini.GetValue("port").c_str());
+        port = ini.Get<int>("port");
     }
     if (port == 0) {
         printf("Please config listen port!\n");

+ 1 - 1
main.cpp.tmpl

@@ -150,7 +150,7 @@ int parse_confile(const char* confile) {
         port = atoi(szPort);
     }
     if (port == 0) {
-        port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
+        port = g_conf_ctx.parser->Get<int>("port");
     }
     if (port == 0) {
         printf("Please config listen port!\n");

+ 33 - 0
utils/iniparser.cpp

@@ -238,3 +238,36 @@ void IniParser::SetValue(const string& key, const string& value, const string& s
     pKV->value = value;
 }
 
+template<>
+bool IniParser::Get(const string& key, const string& section, bool defvalue) {
+    string str = GetValue(key, section);
+    return str.empty() ? defvalue : getboolean(str.c_str());
+}
+
+template<>
+int IniParser::Get(const string& key, const string& section, int defvalue) {
+    string str = GetValue(key, section);
+    return str.empty() ? defvalue : atoi(str.c_str());
+}
+
+template<>
+float IniParser::Get(const string& key, const string& section, float defvalue) {
+    string str = GetValue(key, section);
+    return str.empty() ? defvalue : atof(str.c_str());
+}
+
+template<>
+void IniParser::Set(const string& key, const bool& value, const string& section) {
+    SetValue(key, value ? "true" : "false", section);
+}
+
+template<>
+void IniParser::Set(const string& key, const int& value, const string& section) {
+    SetValue(key, asprintf("%d", value), section);
+}
+
+template<>
+void IniParser::Set(const string& key, const float& value, const string& section) {
+    SetValue(key, asprintf("%f", value), section);
+}
+

+ 33 - 21
utils/iniparser.h

@@ -1,11 +1,12 @@
 #ifndef HW_INI_PARSER_H_
 #define HW_INI_PARSER_H_
 
-#include <list>
-#include <string>
-
 #include "hdef.h"
 #include "hstring.h"
+#include "hbase.h"
+
+#include <list>
+#include <string>
 
 #define DEFAULT_INI_COMMENT "#"
 #define DEFAULT_INI_DELIM   "="
@@ -20,10 +21,6 @@ key = value # span
 # div
 ***********************************/
 
-// class IniComment;
-// class IniSection;
-// class IniKeyValue;
-// for simplicity, we add a member value.
 class IniNode {
  public:
     enum Type {
@@ -34,7 +31,7 @@ class IniNode {
         INI_NODE_TYPE_DIV,
         INI_NODE_TYPE_SPAN,
     } type;
-    string  label;
+    string  label; // section|key|comment
     string  value;
     std::list<IniNode*>    children;
 
@@ -69,21 +66,28 @@ class IniNode {
     }
 };
 
-// class IniComment : public IniNode {
-// public:
-//     string comment;
-// };
+class IniSection : public IniNode {
+public:
+    IniSection() : IniNode(), section(label) {
+        type = INI_NODE_TYPE_SECTION;
+    }
+    string &section;
+};
 
-// class IniSection : public IniNode {
-// public:
-//     string section;
-// };
+class IniKeyValue : public IniNode {
+public:
+    IniKeyValue() : IniNode(), key(label) {
+        type = INI_NODE_TYPE_KEY_VALUE;
+    }
+    string &key;
+};
 
-// class IniKeyValue : public IniNode {
-// public:
-//     string key;
-//     string value;
-// };
+class IniComment : public IniNode {
+public:
+    IniComment() : IniNode(), comment(label) {
+    }
+    string &comment;
+};
 
 class IniParser {
  public:
@@ -105,6 +109,14 @@ class IniParser {
     string GetValue(const string& key, const string& section = "");
     void   SetValue(const string& key, const string& value, const string& section = "");
 
+    // T = [bool, int, float]
+    template<typename T>
+    T Get(const string& key, const string& section = "", const T defvalue = 0);
+
+    // T = [bool, int, float]
+    template<typename T>
+    void Set(const string& key, const T& value, const string& section = "");
+
  private:
     string  _comment;
     string  _delim;