ithewei 7 年之前
父节点
当前提交
e398df8968
共有 5 个文件被更改,包括 71 次插入24 次删除
  1. 9 3
      .gitignore
  2. 10 4
      Makefile
  3. 2 1
      README.md
  4. 4 3
      h.h
  5. 46 13
      main.cpp.tmpl

+ 9 - 3
.gitignore

@@ -1,17 +1,23 @@
 # cache
 *.~
 *.bk
-*.org
+*.bak
 *.old
+*.new
 
 # IDE
 .vs
 .vscode
+tags
+cscope*
+.ycm*
 
 # output
 *.o
-bin
 *.log
+bin
+lib
+dist
 
 # test
-test
+test

+ 10 - 4
Makefile

@@ -15,7 +15,8 @@ LIBDIR = lib
 SRCDIR = src
 BINDIR = bin
 DEPDIR = 3rd
-CONFDIR = etc
+CONFDIR = conf
+DISTDIR = dist
 
 TARGET = test
 ifeq ($(OS),Windows_NT)
@@ -54,18 +55,23 @@ default: all
 all: prepare $(TARGET)
 
 prepare:
-	$(MKDIR) $(BINDIR)
+	$(MKDIR) $(BINDIR) $(LIBDIR)
 
 $(TARGET): $(OBJS)
-	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $^ -o $(BINDIR)/$@ $(LDFLAGS)
+	$(CXX) $^ -o $(BINDIR)/$@ $(LDFLAGS)
+	#$(CXX) $(CXXFLAGS) $^ -o $(LIBDIR)/$@ $(LDFLAGS)
+	#$(AR)  $(ARFLAGS)  $^ -o $(LIBDIR)/$@
 
 clean:
 	$(RM) $(OBJS)
 	$(RM) $(BINDIR)
+	$(RM) $(LIBDIR)
 
 install:
 
 uninstall:
 
-.PHONY: default all prepare clean install uninstall
+dist:
+
+.PHONY: default all prepare clean install uninstall dist
 

+ 2 - 1
README.md

@@ -37,4 +37,5 @@ hw 是一套跨平台c++工具集,类名以H开头
 ## other
 
 - Makefile: 通用Makefile模板
-- main.cpp.tmp: 通用main.cpp模板  
+- main.cpp.tmp: 通用main.cpp模板
+

+ 4 - 3
h.h

@@ -1,9 +1,9 @@
 #ifndef HW_H_
 #define HW_H_
 
-/*
-# copyright 2018 HeWei, all rights reserved.
-*/
+/**
+ * @copyright 2018 HeWei, all rights reserved.
+ */
 
 // platform
 #include "hplatform.h"
@@ -37,3 +37,4 @@
 #endif
 
 #endif  // HW_H_
+

+ 46 - 13
main.cpp.tmpl

@@ -6,21 +6,26 @@
 
 void pexit(int code) {
     printf("Exit with code %d.\n", code);
-    hlogi("Exit with code %d.", code);
     exit(code);
 }
 
 typedef struct arg_s {
-    string cmd;
-    string conf; // -c
-} arg_t ;
+    string  cmd;
+    string  confile;    // -c
+    string  logfile;
+    int     port;       // -p
+    int     daemon;     // -d
+} arg_t;
 arg_t g_arg;
 
-const char* options = "hvc:";
-const char* options_descr = "Options:\n\
+const char* options = "hvc:p:d";
+const char* options_descr = "\
+Options:\n\
 -h                      Print help\n\
 -v                      Print version\n\
--c conffile             Configure file\n\
+-c confile              Configure file\n\
+-p port                 Listen port\n\
+-d                      Daemon\n\
 ";
 
 string version() {
@@ -52,11 +57,19 @@ bool parse_command_line(int argc, char** argv) {
             }
             break;
             case 'c': {
-                g_arg.conf = optarg;
+                g_arg.confile = optarg;
+            }
+            break;
+            case 'p': {
+                g_arg.port = atoi(optarg);
+            }
+            break;
+            case 'd': {
+                g_arg.daemon = 1;
             }
             break;
             default: {
-                printf("Unrecognized options!\n");
+                printf("Unrecognized option!");
                 pexit(-10);
             }
             break;
@@ -69,18 +82,38 @@ 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.logfile = g_arg.cmd + ".log";
+    g_arg.port = 0;
+    g_arg.daemon = 0;
 
-    string strLog = g_arg.cmd + ".log";
-    hlog_set_file(strLog.c_str());
+    hlog_set_file(g_arg.logfile.c_str());
     hlog_set_level(LOG_LEVEL_INFO);
     hlogi("%s", version().c_str());
 
     if (!parse_command_line(argc, argv)) {
-        printf("Command line parse error, please use -h to get help.\n");
+        printf("Parse command line error, please use -h to get help.\n");
         pexit(-10);
     }
 
-    // ...
+    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);
+        return iRet;
+    }
+
+    if (g_arg.port == 0) {
+        printf("Please config port!\n");
+        pexit(-30);
+    }
+
+#ifdef __unix__
+    if (g_arg.daemon) {
+        daemon(1, 1);
+    }
+#endif
 
     pexit(0);
 }
+