ithewei 7 years ago
parent
commit
9b948f676c
2 changed files with 18 additions and 9 deletions
  1. 7 0
      iniparser.cpp
  2. 11 9
      main.cpp.tmpl

+ 7 - 0
iniparser.cpp

@@ -199,6 +199,8 @@ int IniParser::SaveAs(const char* filepath) {
 }
 
 string IniParser::GetValue(const string& key, const string& section) {
+    if (root_ == NULL)  return "";
+
     IniNode* pSection = root_;
     if (section.length() != 0) {
         pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
@@ -212,6 +214,10 @@ string IniParser::GetValue(const string& key, const string& section) {
 }
 
 void IniParser::SetValue(const string& key, const string& value, const string& section) {
+    if (root_ == NULL) {
+        root_ = new IniNode;
+    }
+
     IniNode* pSection = root_;
     if (section.length() != 0) {
         pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
@@ -232,3 +238,4 @@ void IniParser::SetValue(const string& key, const string& value, const string& s
     }
     pKV->value = value;
 }
+

+ 11 - 9
main.cpp.tmpl

@@ -82,7 +82,7 @@ bool parse_command_line(int argc, char** argv) {
 int main(int argc, char** argv) {
     string strCmd((const char*)argv[0]);
     g_arg.cmd = filename(strCmd);
-    g_arg.confile = g_arg.cmd + ".conf";
+    //g_arg.confile = g_arg.cmd + ".conf";
     g_arg.logfile = g_arg.cmd + ".log";
     g_arg.port = 0;
     g_arg.daemon = 0;
@@ -97,18 +97,20 @@ int main(int argc, char** argv) {
     }
 
     IniParser ini;
-    int iRet = ini.LoadFromFile(g_arg.confile.c_str());
-    if (iRet != 0) {
-        printf("Load confile [%s] failed: %d\n", g_arg.confile.c_str(), iRet);
-        pexit(-20);
+    if (!g_arg.confile.empty()) {
+        int iRet = ini.LoadFromFile(g_arg.confile.c_str());
+        if (iRet != 0) {
+            printf("Load confile [%s] failed: %d\n", g_arg.confile.c_str(), iRet);
+            pexit(-20);
+        }
     }
 
     if (g_arg.port == 0) {
         g_arg.port = atoi(ini.GetValue("port").c_str());
-        if (g_arg.port == 0) {
-            puts("Please config listen port!");
-            pexit(-30);
-        }
+    }
+    if (g_arg.port == 0) {
+        puts("Please config listen port!");
+        pexit(-30);
     }
 
 #ifdef __unix__