ithewei 7 years ago
parent
commit
44833237ea
31 changed files with 415 additions and 384 deletions
  1. 16 14
      h.h
  2. 17 16
      hbuf.h
  3. 8 8
      hbytearray.h
  4. 15 15
      hdef.h
  5. 15 15
      hendian.h
  6. 9 8
      herr.cpp
  7. 4 4
      herr.h
  8. 5 5
      hfile.h
  9. 8 8
      hframe.cpp
  10. 18 18
      hframe.h
  11. 14 11
      hgl.h
  12. 17 17
      hgui.h
  13. 9 6
      hlog.cpp
  14. 3 3
      hlog.h
  15. 8 13
      hmutex.h
  16. 44 26
      hobj.h
  17. 5 5
      hplatform.h
  18. 23 22
      hscope.h
  19. 33 33
      hstring.cpp
  20. 3 3
      hstring.h
  21. 16 14
      htable.cpp
  22. 14 15
      htable.h
  23. 20 21
      hthread.h
  24. 21 20
      hthreadpool.h
  25. 18 17
      htime.cpp
  26. 8 7
      htime.h
  27. 12 11
      hvar.h
  28. 10 8
      hversion.h
  29. 6 6
      iniparser.cpp
  30. 9 8
      iniparser.h
  31. 7 7
      singleton.h

+ 16 - 14
h.h

@@ -1,5 +1,9 @@
-#ifndef H_H
-#define H_H
+#ifndef HW_H_
+#define HW_H_
+
+/*
+# copyright 2018 HeWei, all rights reserved.
+*/
 
 // platform
 #include "hplatform.h"
@@ -7,31 +11,29 @@
 // c
 #include "hversion.h"
 #include "hdef.h"
-#include "htime.h"
-#include "hlog.h"
 #include "herr.h"
+#include "hlog.h"
+#include "htime.h"
 
 // cpp
 #ifdef __cplusplus
 #include "hstring.h"
+#include "hbuf.h"
+#include "hfile.h"
+#include "hbytearray.h"
+
+#include "hscope.h"
+#include "hmutex.h"
 #include "hthread.h"
 #include "hthreadpool.h"
-#include "hmutex.h"
-#include "hscope.h"
-#include "singleton.h"
 
 #include "hvar.h"
 #include "hobj.h"
 
-#include "hbuf.h"
-#include "hbytearray.h"
-#include "hfile.h"
 #include "hgui.h"
 
+#include "singleton.h"
 #include "iniparser.h"
-
-#include "hframe.h"
-#include "htable.h"
 #endif
 
-#endif // H_H
+#endif  // HW_H_

+ 17 - 16
hbuf.h

@@ -1,10 +1,11 @@
-#ifndef H_BUF_H
-#define H_BUF_H
+#ifndef HW_BUF_H_
+#define HW_BUF_H_
 
-#include "hdef.h"
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <mutex>
+
+#include "hdef.h"
 
 typedef struct hbuf_s {
     uint8* base;
@@ -56,10 +57,10 @@ typedef struct hbuf_s {
     bool isNull() {
         return base == NULL || len == 0;
     }
-}hbuf_t;
+} hbuf_t;
 
 class HBuf : public hbuf_t {
-public:
+ public:
     HBuf() : hbuf_t() {}
     HBuf(size_t cap) {init(cap);}
     HBuf(void* data, size_t len) {
@@ -70,7 +71,7 @@ public:
 
 // VL: Variable-Length
 class HVLBuf : public HBuf {
-public:
+ public:
     HVLBuf() : HBuf() {_offset = _size = 0;}
     HVLBuf(size_t cap) : HBuf(cap) {_offset = _size = 0;}
     HVLBuf(void* data, size_t len) : HBuf(data, len) {_offset = 0; _size = len;}
@@ -84,7 +85,7 @@ public:
             this->len = MAX(this->len, len)*2;
             base = (uint8*)realloc(base, this->len);
         }
-        
+
         if (_offset < len) {
             // move => end
             memmove(base+this->len-_size, data(), _size);
@@ -100,7 +101,7 @@ public:
         if (len > this->len - _size) {
             this->len = MAX(this->len, len)*2;
             base = (uint8*)realloc(base, this->len);
-        }else if (len > this->len - _offset - _size) {
+        } else if (len > this->len - _offset - _size) {
             // move => start
             memmove(base, data(), _size);
             _offset = 0;
@@ -111,7 +112,7 @@ public:
 
     void pop_front(void* ptr, size_t len) {
         if (len <= _size) {
-            if (ptr){
+            if (ptr) {
                 memcpy(ptr, data(), len);
             }
             _offset += len;
@@ -149,13 +150,13 @@ public:
         pop_front(NULL, len);
     }
 
-private:
+ private:
     size_t _offset;
     size_t _size;
 };
 
 class HRingBuf : public HBuf {
-public:
+ public:
     HRingBuf() : HBuf() {_head = _tail = _size = 0;}
     HRingBuf(size_t cap) : HBuf(cap) {_head = _tail = _size = 0;}
 
@@ -167,11 +168,11 @@ public:
                 ret = base + _tail;
                 _tail += len;
                 if (_tail == this->len) _tail = 0;
-            } else if(_head >= len) {
+            } else if (_head >= len) {
                 ret = base;
                 _tail = len;
             }
-        }else{
+        } else {
             // [_tail, _head)
             if (_head - _tail >= len) {
                 ret = base + _tail;
@@ -194,10 +195,10 @@ public:
 
     size_t size() {return _size;}
 
-private:
+ private:
     size_t _head;
     size_t _tail;
     size_t _size;
 };
 
-#endif // H_BUF_H
+#endif  // HW_BUF_H_

+ 8 - 8
hbytearray.h

@@ -1,26 +1,26 @@
-#ifndef H_BYTE_ARRAY_H
-#define H_BYTE_ARRAY_H
+#ifndef HW_BYTE_ARRAY_H_
+#define HW_BYTE_ARRAY_H_
 
 #include "hbuf.h"
 #include "base64.h"
 
-class HByteArray : public HVLBuf{
-public:
+class HByteArray : public HVLBuf {
+ public:
     HByteArray() : HVLBuf() {}
     HByteArray(int cap) : HVLBuf(cap) {}
     HByteArray(void* data, int len) : HVLBuf(data, len) {}
 
-    bool encodeBase64(void* ptr, int len){
-        int base64_len = BASE64_ENCODE_OUT_SIZE(len) + 1; // +1 for '\0'
+    bool encodeBase64(void* ptr, int len) {
+        int base64_len = BASE64_ENCODE_OUT_SIZE(len) + 1;   // +1 for '\0'
         init(base64_len);
         return base64_encode((unsigned char*)ptr, len, (char*)data()) == BASE64_OK;
     }
 
-    bool decodeBase64(const char* base64){
+    bool decodeBase64(const char* base64) {
         int out_len = BASE64_DECODE_OUT_SIZE(strlen(base64));
         init(out_len);
         return base64_decode(base64, strlen(base64), data()) == BASE64_OK;
     }
 };
 
-#endif // H_BYTE_ARRAY
+#endif  // HW_BYTE_ARRAY_H_

+ 15 - 15
hdef.h

@@ -1,5 +1,5 @@
-#ifndef H_DEF_H
-#define H_DEF_H
+#ifndef HW_DEF_H_
+#define HW_DEF_H_
 
 typedef unsigned char       uint8;
 typedef unsigned short      uint16;
@@ -62,27 +62,27 @@ typedef void (*procedure_t)(void* userdata);
 #endif
 
 #ifndef MAX
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
 #endif
 
 #ifndef MIN
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
 #endif
 
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
 
-#define SAFE_FREE(p)    do{if (p) {free(p); (p) = NULL;}}while(0)
-#define SAFE_DELETE(p)  do{if (p) {delete (p); (p) = NULL;}}while(0)
-#define SAFE_DELETE_ARRAY(p) do{if (p) {delete[] (p); (p) = NULL;}}while(0)
-#define SAFE_RELEASE(p) do{if (p) {(p)->release(); (p) = NULL;}}while(0)
+#define SAFE_FREE(p)    do {if (p) {free(p); (p) = NULL;}}while(0)
+#define SAFE_DELETE(p)  do {if (p) {delete (p); (p) = NULL;}}while(0)
+#define SAFE_DELETE_ARRAY(p) do {if (p) {delete[] (p); (p) = NULL;}}while(0)
+#define SAFE_RELEASE(p) do {if (p) {(p)->release(); (p) = NULL;}}while(0)
 
 #ifndef MAKE_FOURCC
-#define MAKE_FOURCC(a,b,c,d) \
+#define MAKE_FOURCC(a, b, c, d) \
 ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
 #endif
 
 #ifndef MAKE_WORD
-#define MAKE_WORD(h,l)  ( (((WORD)h) << 8) | (l & 0xff) )
+#define MAKE_WORD(h, l)  ( (((WORD)h) << 8) | (l & 0xff) )
 #endif
 
 #ifndef HIBYTE
@@ -93,11 +93,11 @@ typedef void (*procedure_t)(void* userdata);
 #define LOBYTE(w) ( (BYTE)(w & 0xff) )
 #endif
 
-#define MAKE_INT32(h,l)   ( ((int32)h) << 16 | (l & 0xffff) )
+#define MAKE_INT32(h, l)   ( ((int32)h) << 16 | (l & 0xffff) )
 #define HIINT16(n)        ( (int16)(((int32)n) >> 16) )
 #define LOINI16(n)        ( (int16)(n & 0xffff) )
 
-#define MAKE_INT64(h,l)   ( ((int64)h) << 32 | (l & 0xffffffff) )
+#define MAKE_INT64(h, l)   ( ((int64)h) << 32 | (l & 0xffffffff) )
 #define HIINT32(n)        ( (int32)(((int64)n) >> 32) )
 #define LOINI32(n)        ( (int32)(n & 0xffffffff) )
 
@@ -107,10 +107,10 @@ typedef void (*procedure_t)(void* userdata);
 #define STRINGIFY(x)    STRINGIFY_HELPER(x)
 #define STRINGIFY_HELPER(x)    #x
 
-#define STRINGCAT(x,y)  STRINGCAT_HELPER(x,y)
-#define STRINGCAT_HELPER(x,y)   x##y
+#define STRINGCAT(x, y)  STRINGCAT_HELPER(x, y)
+#define STRINGCAT_HELPER(x, y)   x##y
 
 #define container_of(ptr, type, member) \
   ((type *) ((char *) (ptr) - offsetof(type, member)))
 
-#endif // H_DEF_H
+#endif  // HW_DEF_H_

+ 15 - 15
hendian.h

@@ -1,5 +1,5 @@
-#ifndef H_ENDIAN_H
-#define H_ENDIAN_H
+#ifndef HW_ENDIAN_H_
+#define HW_ENDIAN_H_
 
 #include "hdef.h"
 
@@ -7,28 +7,28 @@
 #define BIG_ENDIAN      1
 #define NET_ENDIAN      BIG_ENDIAN
 
-int check_endian(){
-    union{
+int check_endian() {
+    union {
         char c;
         short s;
-    }u;
+    } u;
     u.s = 0x1122;
-    if (u.c == 0x11){
+    if (u.c == 0x11) {
         return BIG_ENDIAN;
     }
     return LITTLE_ENDIAN;
 }
 
 template <typename T>
-uint8* serialize(uint8* buf, T value, int host_endian = LITTLE_ENDIAN, int buf_endian = BIG_ENDIAN){
+uint8* serialize(uint8* buf, T value, int host_endian = LITTLE_ENDIAN, int buf_endian = BIG_ENDIAN) {
     size_t size = sizeof(T);
     uint8* pDst = buf;
     uint8* pSrc = (uint8*)&value;
 
-    if (host_endian == buf_endian){
+    if (host_endian == buf_endian) {
         memcpy(pDst, pSrc, size);
-    }else{
-        for (int i = 0; i < size; ++i){
+    } else {
+        for (int i = 0; i < size; ++i) {
             pDst[i] = pSrc[size-i-1];
         }
     }
@@ -37,15 +37,15 @@ uint8* serialize(uint8* buf, T value, int host_endian = LITTLE_ENDIAN, int buf_e
 }
 
 template <typename T>
-uint8* deserialize(uint8* buf, T* value, int host_endian = LITTLE_ENDIAN, int buf_endian = BIG_ENDIAN){
+uint8* deserialize(uint8* buf, T* value, int host_endian = LITTLE_ENDIAN, int buf_endian = BIG_ENDIAN) {
     size_t size = sizeof(T);
     uint8* pSrc = buf;
     uint8* pDst = (uint8*)value;
 
-    if (host_endian == buf_endian){
+    if (host_endian == buf_endian) {
         memcpy(pDst, pSrc, size);
-    }else{
-        for (int i = 0; i < size; ++i){
+    } else {
+        for (int i = 0; i < size; ++i) {
             pDst[i] = pSrc[size-i-1];
         }
     }
@@ -53,4 +53,4 @@ uint8* deserialize(uint8* buf, T* value, int host_endian = LITTLE_ENDIAN, int bu
     return buf+size;
 }
 
-#endif // H_ENDIAN_H
+#endif  // HW_ENDIAN_H_

+ 9 - 8
herr.cpp

@@ -1,35 +1,36 @@
 #include "herr.h"
-#include "hthread.h" // for gettid
 
 #include <map>
 
+#include "hthread.h"    // for gettid
+
 // id => errcode
 static std::map<int, int>   s_mapErr;
 
-void set_id_errcode(int id, int errcode){
+void set_id_errcode(int id, int errcode) {
     s_mapErr[id] = errcode;
 }
 
-int  get_id_errcode(int id){
+int  get_id_errcode(int id) {
     auto iter = s_mapErr.find(id);
     if (iter != s_mapErr.end()) {
         // note: erase after get
         s_mapErr.erase(iter);
         return iter->second;
     }
-    return ERR_OK;    
+    return ERR_OK;
 }
 
-void set_last_errcode(int errcode){
+void set_last_errcode(int errcode) {
     set_id_errcode(gettid(), errcode);
 }
 
-int  get_last_errcode(){
+int  get_last_errcode() {
     return get_id_errcode(gettid());
 }
 
-const char* get_errmsg(int err){
-    switch(err){
+const char* get_errmsg(int err) {
+    switch (err) {
 #define CASE_ERR(macro, errcode, errmsg) \
     case errcode: \
         return errmsg;

+ 4 - 4
herr.h

@@ -1,5 +1,5 @@
-#ifndef H_ERR_H
-#define H_ERR_H
+#ifndef HW_ERR_H_
+#define HW_ERR_H_
 
 // F(macro, errcode, errmsg)
 #define FOREACH_ERR_COMMON(F) \
@@ -45,7 +45,7 @@
     F(ERR_GROUP_NOT_FOUND,      3001, "group not found")    \
     F(ERR_PERSON_NOT_FOUND,     3002, "person not found")   \
     F(ERR_FACE_NOT_FOUND,       3003, "face not found")     \
-    F(ERR_DEVICE_NOT_FOUND,     3004, "device not found")   
+    F(ERR_DEVICE_NOT_FOUND,     3004, "device not found")
 
 #define FOREACH_ERR(F) \
     FOREACH_ERR_COMMON(F) \
@@ -70,4 +70,4 @@ int  get_last_errcode();
 // errcode => errmsg
 const char* get_errmsg(int errcode);
 
-#endif // H_ERR_H
+#endif  // HW_ERR_H_

+ 5 - 5
hfile.h

@@ -1,5 +1,5 @@
-#ifndef HW_FILE_H
-#define HW_FILE_H
+#ifndef HW_FILE_H_
+#define HW_FILE_H_
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -12,7 +12,7 @@
 #include "herr.h"
 
 class HFile {
-public:
+ public:
     HFile() {
         _fp = NULL;
     }
@@ -41,7 +41,7 @@ public:
     size_t size() {
         struct stat st;
         stat(_filepath, &st);
-        return st.st_size;     
+        return st.st_size;
     }
 
     size_t read(void* ptr, size_t len) {
@@ -74,4 +74,4 @@ public:
     FILE* _fp;
 };
 
-#endif // HW_FILE_H
+#endif  // HW_FILE_H_

+ 8 - 8
hframe.cpp

@@ -1,6 +1,6 @@
 #include "hframe.h"
 
-int HFrameBuf::push(HFrame* pFrame){
+int HFrameBuf::push(HFrame* pFrame) {
     if (pFrame->isNull())
         return -10;
 
@@ -8,9 +8,9 @@ int HFrameBuf::push(HFrame* pFrame){
 
     std::lock_guard<std::mutex> locker(mutex);
 
-    if (frames.size() >= cache_num){
-        if (policy == HFrameBuf::DISCARD){
-            return -20; // note: cache full, discard frame
+    if (frames.size() >= cache_num) {
+        if (policy == HFrameBuf::DISCARD) {
+            return -20;     // note: cache full, discard frame
         }
 
         // note: cache full, remove front, push newer frame
@@ -20,9 +20,9 @@ int HFrameBuf::push(HFrame* pFrame){
     }
 
     int ret = 0;
-    if (isNull()){
+    if (isNull()) {
         init(pFrame->buf.len * cache_num);
-        ret = 1; // note: first push
+        ret = 1;    // note: first push
 
         frame_info.w = pFrame->w;
         frame_info.h = pFrame->h;
@@ -40,7 +40,7 @@ int HFrameBuf::push(HFrame* pFrame){
     return ret;
 }
 
-int HFrameBuf::pop(HFrame* pFrame){
+int HFrameBuf::pop(HFrame* pFrame) {
     frame_stats.pop_cnt++;
 
     std::lock_guard<std::mutex> locker(mutex);
@@ -62,4 +62,4 @@ int HFrameBuf::pop(HFrame* pFrame){
     frame_stats.pop_ok_cnt++;
 
     return 0;
-}
+}

+ 18 - 18
hframe.h

@@ -1,12 +1,12 @@
-#ifndef H_FRAME_H
-#define H_FRAME_H
+#ifndef HW_FRAME_H_
+#define HW_FRAME_H_
 
 #include <deque>
 
 #include "hbuf.h"
 #include "hmutex.h"
 
-typedef struct hframe_s{
+typedef struct hframe_s {
     hbuf_t buf;
     int w;
     int h;
@@ -14,58 +14,58 @@ typedef struct hframe_s{
     int bpp;
     uint64 ts;
     void* userdata;
-    hframe_s(){
+    hframe_s() {
         w = h = type = bpp = ts = 0;
         userdata = NULL;
     }
 
-    bool isNull(){
+    bool isNull() {
         return w == 0 || h == 0 || buf.isNull();
     }
 
     // deep copy
-    void copy(const hframe_s& rhs){
+    void copy(const hframe_s& rhs) {
         this->w = rhs.w;
         this->h = rhs.h;
         this->type = rhs.type;
         this->bpp  = rhs.bpp;
         this->ts   = rhs.ts;
         this->userdata = rhs.userdata;
-        if (this->buf.isNull() || this->buf.len != rhs.buf.len){
+        if (this->buf.isNull() || this->buf.len != rhs.buf.len) {
             this->buf.init(rhs.buf.len);
         }
         memcpy(this->buf.base, rhs.buf.base, rhs.buf.len);
     }
 }HFrame;
 
-typedef struct frame_info_s{
+typedef struct frame_info_s {
     int w;
     int h;
     int type;
     int bpp;
-}FrameInfo;
+} FrameInfo;
 
-typedef struct frame_stats_s{
+typedef struct frame_stats_s {
     int push_cnt;
     int pop_cnt;
 
     int push_ok_cnt;
     int pop_ok_cnt;
 
-    frame_stats_s(){
+    frame_stats_s() {
         push_cnt = pop_cnt = push_ok_cnt = pop_ok_cnt = 0;
     }
-}FrameStats;
+} FrameStats;
 
 #define DEFAULT_FRAME_CACHENUM  10
 
 class HFrameBuf : public HRingBuf {
-public:
-    enum CacheFullPolicy{
+ public:
+    enum CacheFullPolicy {
         SQUEEZE,
-        DISCARD,       
-    }policy;
-    
+        DISCARD,
+    } policy;
+
     HFrameBuf() : HRingBuf() {
         cache_num = DEFAULT_FRAME_CACHENUM;
         policy = SQUEEZE;
@@ -84,4 +84,4 @@ public:
     std::mutex         mutex;
 };
 
-#endif // H_FRAME_H
+#endif  // HW_FRAME_H_

+ 14 - 11
hgl.h

@@ -1,24 +1,27 @@
-#ifndef HGL_H
-#define HGL_H
+#ifndef HW_GL_H_
+#define HW_GL_H_
 
 #include <GL/glew.h>
+
 #include "hframe.h"
 
 // GL PixelFormat extend
-#define GL_I420				0x1910  // YYYYYYYYUUVV
+#define GL_I420             0x1910  // YYYYYYYYUUVV
 #define GL_YV12             0x1911  // YYYYYYYYVVUU
 #define GL_NV12             0x1912  // YYYYYYYYUVUV
 #define GL_NV21             0x1913  // YYYYYYYYVUVU
 
-//#define GL_RGB  0x1907            // RGBRGB
-//#define GL_RGBA 0x1908            // RGBARGBA
+/*
+#define GL_RGB      0x1907      // RGBRGB
+#define GL_RGBA     0x1908      // RGBARGBA
 
-//#define GL_BGR 0x80E0             // BGRBGR       .bmp
-//#define GL_BGRA 0x80E1            // BGRABGRA
+#define GL_BGR      0x80E0      // BGRBGR       .bmp
+#define GL_BGRA     0x80E1      // BGRABGRA
+*/
 
-typedef struct GLTexture_s{
-    GLuint id; // for glGenTextures
+typedef struct GLTexture_s {
+    GLuint id;  // for glGenTextures
     HFrame frame;
-}GLTexture;
+} GLTexture;
 
-#endif // HGL_H
+#endif  // HW_GL_H_

+ 17 - 17
hgui.h

@@ -1,60 +1,60 @@
-#ifndef HGUI_H
-#define HGUI_H
+#ifndef HW_GUI_H_
+#define HW_GUI_H_
 
 #include "hdef.h"
 
-typedef uint32 HColor; // 0xAARRGGBB
+typedef uint32 HColor;  // 0xAARRGGBB
 
-#define CLR_B(c)    ( c        & 0xff)
+#define CLR_B(c)    (c         & 0xff)
 #define CLR_G(c)    ((c >> 8)  & 0xff)
 #define CLR_R(c)    ((c >> 16) & 0xff)
 #define CLR_A(c)    ((c >> 24) & 0xff)
-#define ARGB(a, r, g, b) MAKE_FOURCC(a,r,g,b)
+#define ARGB(a, r, g, b) MAKE_FOURCC(a, r, g, b)
 
-typedef struct hpoint_s{
+typedef struct hpoint_s {
     int x;
     int y;
 
 #ifdef __cplusplus
-    hpoint_s(){
+    hpoint_s() {
         x = y = 0;
     }
 
-    hpoint_s(int x, int y){
+    hpoint_s(int x, int y) {
         this->x = x;
         this->y = y;
     }
 #endif
-}HPoint;
+} HPoint;
 
-typedef struct hsize_s{
+typedef struct hsize_s {
     int w;
     int h;
 
 #ifdef __cplusplus
-    hsize_s(){
+    hsize_s() {
         w = h = 0;
     }
 
-    hsize_s(int w, int h){
+    hsize_s(int w, int h) {
         this->w = w;
         this->h = h;
     }
 #endif
 }HSize;
 
-typedef struct hrect_s{
+typedef struct hrect_s {
     int x;
     int y;
     int w;
     int h;
 
 #ifdef __cplusplus
-    hrect_s(){
+    hrect_s() {
         x = y = w = h = 0;
     }
 
-    hrect_s(int x, int y, int w, int h){
+    hrect_s(int x, int y, int w, int h) {
         this->x = x;
         this->y = y;
         this->w = w;
@@ -66,6 +66,6 @@ typedef struct hrect_s{
     int top()      {return y;}
     int bottom()   {return y+h;}
 #endif
-}HRect;
+} HRect;
 
-#endif // HGUI_H
+#endif  // HW_GUI_H_

+ 9 - 6
hlog.cpp

@@ -1,9 +1,11 @@
 #include "hlog.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
 #include <mutex>
-#include "htime.h" // for get_datetime
+
+#include "htime.h"  // for get_datetime
 
 #define LOGBUF_SIZE         (1<<13)  // 8k
 #define LOGFILE_MAXSIZE     (1<<23)  // 8M
@@ -19,7 +21,7 @@ int hlog_set_file(const char* logfile) {
     if (logfile && strlen(logfile) > 0) {
         strncpy(s_logfile, logfile, 256);
     }
-    
+
     if (s_logfp) {
         fclose(s_logfp);
         s_logfp = NULL;
@@ -30,11 +32,11 @@ int hlog_set_file(const char* logfile) {
     return s_logfp ? 0 : -1;
 }
 
-void hlog_set_level(int level){
+void hlog_set_level(int level) {
     s_loglevel = level;
 }
 
-void hlog_enable_color(bool enable){
+void hlog_enable_color(bool enable) {
     s_logcolor = enable;
 }
 
@@ -57,12 +59,12 @@ int hlog_printf(int level, const char* fmt, ...) {
 
     std::lock_guard<std::mutex> locker(s_mutex);
 
-    if (!s_logfp){
+    if (!s_logfp) {
         if (hlog_set_file(s_logfile) != 0)
             return -20;
     }
 
-    if (ftell(s_logfp) > LOGFILE_MAXSIZE){
+    if (ftell(s_logfp) > LOGFILE_MAXSIZE) {
         fclose(s_logfp);
         s_logfp = fopen(s_logfile, "w");
         if (!s_logfp)
@@ -73,6 +75,7 @@ int hlog_printf(int level, const char* fmt, ...) {
 
     int len = snprintf(s_logbuf, LOGBUF_SIZE, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s]: ",
         pcolor, now.year, now.month, now.day, now.hour, now.min, now.sec, now.ms, plevel);
+
     va_list ap;
     va_start(ap, fmt);
     len += vsnprintf(s_logbuf + len, LOGBUF_SIZE-len, fmt, ap);

+ 3 - 3
hlog.h

@@ -1,5 +1,5 @@
-#ifndef H_LOG_H
-#define H_LOG_H
+#ifndef HW_LOG_H_
+#define HW_LOG_H_
 
 #define CL_CLR      "\033[0m"       /* 恢复颜色 */
 #define CL_BLACK    "\033[30m"      /* 黑色字 */
@@ -49,4 +49,4 @@ int     hlog_printf(int level, const char* fmt, ...);
 #define hloge(fmt, ...) hlog_printf(LOG_LEVEL_ERROR, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
 #define hlogf(fmt, ...) hlog_printf(LOG_LEVEL_FATAL, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
 
-#endif // H_LOG_H
+#endif  // HW_LOG_H_

+ 8 - 13
hmutex.h

@@ -1,18 +1,13 @@
-#ifndef H_MUTEX_H
-#define H_MUTEX_H
+#ifndef HW_MUTEX_H_
+#define HW_MUTEX_H_
 
 #include <mutex>
 
 #include "hplatform.h"
 
-class HMutex {
-protected:
-    std::mutex  _mutex;
-};
-
 #ifdef _MSC_VER
-class RWLock{
-public:
+class RWLock {
+ public:
     RWLock() { InitializeSRWLock(&_rwlock); }
     ~RWLock() { }
 
@@ -21,12 +16,12 @@ public:
 
     void wrlock()   { AcquireSRWLockExclusive(&_rwlock); }
     void wrunlock() { ReleaseSRWLockExclusive(&_rwlock); }
-private:
+ private:
     SRWLOCK _rwlock;
 };
 #else
 class RWLock{
-public:
+ public:
     RWLock() { pthread_rwlock_init(&_rwlock, NULL); }
     ~RWLock() { pthread_rwlock_destroy(&_rwlock); }
 
@@ -35,9 +30,9 @@ public:
 
     void wrlock()   { pthread_rwlock_wrlock(&_rwlock); }
     void wrunlock() { pthread_rwlock_unlock(&_rwlock); }
-private:
+ private:
     pthread_rwlock_t _rwlock;
 };
 #endif
 
-#endif // H_MUTEX_H
+#endif  // HW_MUTEX_H_

+ 44 - 26
hobj.h

@@ -1,24 +1,21 @@
-#ifndef H_OBJ_H
-#define H_OBJ_H
+#ifndef HW_OBJ_H_
+#define HW_OBJ_H_
 
-#include "hdef.h"
-#include "hvar.h"
 #include <string>
 #include <map>
 #include <list>
 
-class HObj{
-public:
+#include "hdef.h"
+#include "hvar.h"
+
+class HObj {
+ public:
     HObj(HObj* parent = NULL) {
         _parent = parent;
     }
 
     virtual ~HObj() {
-        auto iter = children.begin();
-        while (iter != children.end()) {
-            SAFE_DELETE(*iter);
-            iter++;
-        }
+        deleteAllChild();
     }
 
     std::string name() {
@@ -37,13 +34,34 @@ public:
         _parent = ptr;
     }
 
-    void  setChild(HObj* ptr) {
-        children.push_back(ptr);
+    void  addChild(HObj* ptr) {
+        _children.push_back(ptr);
+    }
+
+    void removeChild(HObj* ptr) {
+        auto iter = _children.begin();
+        while (iter != _children.end()) {
+            if ((*iter) == ptr) {
+                iter = _children.erase(iter);
+            } else {
+                ++iter;
+            }
+            ++iter;
+        }
+    }
+
+    void deleteAllChild() {
+        auto iter = _children.begin();
+        while (iter != _children.end()) {
+            SAFE_DELETE(*iter);
+            ++iter;
+        }
+        _children.clear();
     }
 
     HObj* findChild(std::string objName) {
-        auto iter = children.begin();
-        while (iter != children.end()) {
+        auto iter = _children.begin();
+        while (iter != _children.end()) {
             if ((*iter)->name() == objName)
                 return *iter;
             iter++;
@@ -51,34 +69,34 @@ public:
     }
 
     HVar  property(std::string key) {
-        auto iter = properties.find(key);
-        if (iter != properties.end())
+        auto iter = _properties.find(key);
+        if (iter != _properties.end())
             return iter->second;
         return HVar();
     }
 
     void  setProperty(std::string key, HVar value) {
-        properties[key] = value;
+        _properties[key] = value;
     }
 
     method_t method(std::string key) {
-        auto iter = methods.find(key);
-        if (iter != methods.end())
+        auto iter = _methods.find(key);
+        if (iter != _methods.end())
             return iter->second;
         return NULL;
     }
 
     void  setMethod(std::string key, method_t method) {
-        methods[key] = method;
+        _methods[key] = method;
     }
 
-public:
+ public:
     std::string _objName;
-    std::map<std::string, HVar> properties;
-    std::map<std::string, method_t> methods;
+    std::map<std::string, HVar> _properties;
+    std::map<std::string, method_t> _methods;
 
     HObj* _parent;
-    std::list<HObj*> children;
+    std::list<HObj*> _children;
 };
 
-#endif
+#endif  // HW_OBJ_H_

+ 5 - 5
hplatform.h

@@ -1,5 +1,5 @@
-#ifndef H_PLATFORM_H
-#define H_PLATFORM_H
+#ifndef HW_PLATFORM_H_
+#define HW_PLATFORM_H_
 
 #ifdef _MSC_VER
     #ifndef WIN32_LEAN_AND_MEAN
@@ -8,7 +8,7 @@
     #include <winsock2.h>
     #include <windows.h>
     #undef  WIN32_LEAN_AND_MEAN
-#else    
+#else
     #include <unistd.h>
     #include <pthread.h>
     #include <sys/time.h>
@@ -19,9 +19,9 @@
 #endif
 
 #ifdef __GNUC__
-    #define GNUC_ALIGN(n)   __attribute__((aligned(n))) 
+    #define GNUC_ALIGN(n)   __attribute__((aligned(n)))
 #else
     #define GNUC_ALIGN(n)
 #endif
 
-#endif // H_PLATFORM_H
+#endif  // HW_PLATFORM_H_

+ 23 - 22
hscope.h

@@ -1,11 +1,12 @@
-#ifndef H_SCOPE_H
-#define H_SCOPE_H
+#ifndef HW_SCOPE_H_
+#define HW_SCOPE_H_
 
-#include "hdef.h"
 #include <functional>
 
-class ScopeCleanup{
-public:
+#include "hdef.h"
+
+class ScopeCleanup {
+ public:
     typedef std::function<void()> FT;
 
     template<typename Fn, typename... Args>
@@ -17,53 +18,53 @@ public:
         cleanup_();
     }
 
-private:
+ private:
     FT cleanup_;
 };
 
 template<typename T>
-class ScopeFree{
-public:
+class ScopeFree {
+ public:
     ScopeFree(T* p) : _p(p) {} 
     ~ScopeFree()    {SAFE_FREE(_p);}
-private:
+ private:
     T*  _p;
 };
 
 template<typename T>
-class ScopeDelete{
-public:
+class ScopeDelete {
+ public:
     ScopeDelete(T* p) : _p(p) {} 
     ~ScopeDelete()    {SAFE_DELETE(_p);}
-private:
+ private:
     T*  _p;
 };
 
 template<typename T>
-class ScopeDeleteArray{
-public:
+class ScopeDeleteArray {
+ public:
     ScopeDeleteArray(T* p) : _p(p) {} 
     ~ScopeDeleteArray()    {SAFE_DELETE_ARRAY(_p);}
-private:
+ private:
     T*  _p;
 };
 
 template<typename T>
-class ScopeRelease{
-public:
+class ScopeRelease {
+ public:
     ScopeRelease(T* p) : _p(p) {} 
     ~ScopeRelease()    {SAFE_RELEASE(_p);}
-private:
+ private:
     T*  _p;
 };
 
 template<typename T>
-class ScopeLock{
-public:
+class ScopeLock {
+ public:
     ScopeLock(T& mutex) : _mutex(mutex) {_mutex.lock();} 
     ~ScopeLock()    {_mutex.unlock();}
-private:
+ private:
     T& _mutex;
 };
 
-#endif // H_SCOPE_H
+#endif  // HW_SCOPE_H_

+ 33 - 33
hstring.cpp

@@ -11,43 +11,43 @@
 #define SPACE_CHARS     " \t\r\n"
 
 int vscprintf(const char* fmt, va_list ap) {
-	return vsnprintf(NULL, 0, fmt, ap);
+    return vsnprintf(NULL, 0, fmt, ap);
 }
 
 string asprintf(const char* fmt, ...) {
-	va_list ap;
-	va_start(ap, fmt);
-	int len = vscprintf(fmt, ap) + 1;
-	va_end(ap);
-	// must recall va_start in linux
-	va_start(ap, fmt);
-	char* buf = (char*)malloc(len);
+    va_list ap;
+    va_start(ap, fmt);
+    int len = vscprintf(fmt, ap) + 1;
+    va_end(ap);
+    // must recall va_start in linux
+    va_start(ap, fmt);
+    char* buf = (char*)malloc(len);
     memset(buf, 0, len);
-	vsnprintf(buf, len, fmt, ap);
-	va_end(ap);
+    vsnprintf(buf, len, fmt, ap);
+    va_end(ap);
 
-	string res(buf);
-	free(buf);
-	return res;
+    string res(buf);
+    free(buf);
+    return res;
 }
 
 StringList split(const string& str, char delim) {
-	std::stringstream ss;
-	ss << str;
-	string item;
-	StringList res;
-	while (std::getline(ss, item, delim)) {
-		res.push_back(item);
-	}
-	return res;
+    std::stringstream ss;
+    ss << str;
+    string item;
+    StringList res;
+    while (std::getline(ss, item, delim)) {
+        res.push_back(item);
+    }
+    return res;
 }
 
-string trim(const string& str){
-	string::size_type pos1 = str.find_first_not_of(SPACE_CHARS);
-	if (pos1 == string::npos)	return "";
+string trim(const string& str) {
+    string::size_type pos1 = str.find_first_not_of(SPACE_CHARS);
+    if (pos1 == string::npos)   return "";
 
-	string::size_type pos2 = str.find_last_not_of(SPACE_CHARS);
-	return str.substr(pos1, pos2-pos1+1);
+    string::size_type pos2 = str.find_last_not_of(SPACE_CHARS);
+    return str.substr(pos1, pos2-pos1+1);
 }
 
 string trimL(const string& str) {
@@ -62,15 +62,15 @@ string trimR(const string& str) {
 }
 
 string replace(const string& str, const string& find, const string& rep) {
-	string::size_type pos = 0;
-	string::size_type a = find.size();
-	string::size_type b = rep.size();
+    string::size_type pos = 0;
+    string::size_type a = find.size();
+    string::size_type b = rep.size();
 
     string res(str);
-	while ((pos = res.find(find,pos)) != string::npos){
-	    res.replace(pos, a, rep);
-		pos += b;
-	}
+    while ((pos = res.find(find, pos)) != string::npos) {
+        res.replace(pos, a, rep);
+        pos += b;
+    }
     return res;
 }
 

+ 3 - 3
hstring.h

@@ -1,5 +1,5 @@
-#ifndef H_STRING_H
-#define H_STRING_H
+#ifndef HW_STRING_H_
+#define HW_STRING_H_
 
 #include <string>
 #include <vector>
@@ -19,4 +19,4 @@ string dirname(const string& str);
 string filename(const string& str);
 string suffixname(const string& str);
 
-#endif // H_STRING_H
+#endif  // HW_STRING_H_

+ 16 - 14
htable.cpp

@@ -1,33 +1,34 @@
 #include "htable.h"
+
 #include "hdef.h"
 
-HTable::HTable(){
+HTable::HTable() {
     row = col = 0;
 }
 
-void HTable::init(int row, int col){
+void HTable::init(int row, int col) {
     this->row = row;
     this->col = col;
     m_mapCells.clear();
-    for (int r = 1; r <= row; ++r){
-        for (int c = 1; c <= col; ++c){
+    for (int r = 1; r <= row; ++r) {
+        for (int c = 1; c <= col; ++c) {
             int id = (r-1) * col + c;
-            m_mapCells[id] = HTableCell(r,r,c,c);
+            m_mapCells[id] = HTableCell(r, r, c, c);
         }
     }
 }
 
-bool HTable::getTableCell(int id, HTableCell& rst){
-    if (m_mapCells.find(id) != m_mapCells.end()){
+bool HTable::getTableCell(int id, HTableCell& rst) {
+    if (m_mapCells.find(id) != m_mapCells.end()) {
         rst = m_mapCells[id];
         return true;
     }
     return false;
 }
 
-HTableCell HTable::merge(int lt, int rb){
-    HTableCell cell_lt,cell_rb;
-    if (getTableCell(lt, cell_lt) && getTableCell(rb, cell_rb)){
+HTableCell HTable::merge(int lt, int rb) {
+    HTableCell cell_lt, cell_rb;
+    if (getTableCell(lt, cell_lt) && getTableCell(rb, cell_rb)) {
         int r1 = MIN(cell_lt.r1, cell_rb.r1);
         int r2 = MAX(cell_lt.r2, cell_rb.r2);
         int c1 = MIN(cell_lt.c1, cell_rb.c1);
@@ -35,16 +36,17 @@ HTableCell HTable::merge(int lt, int rb){
 
         HTableCell cell(r1, r2, c1, c2);
         std::map<int, HTableCell>::iterator iter = m_mapCells.begin();
-        while (iter != m_mapCells.end()){
-            if (cell.contain(iter->second)){
+        while (iter != m_mapCells.end()) {
+            if (cell.contain(iter->second)) {
                 iter = m_mapCells.erase(iter);
-            }else
+            } else {
                 ++iter;
+            }
         }
         m_mapCells[lt] = cell;
 
         return cell;
     }
 
-    return HTableCell(0,0,0,0);
+    return HTableCell();
 }

+ 14 - 15
htable.h

@@ -1,12 +1,12 @@
-#ifndef H_TABLE_H
-#define H_TABLE_H
+#ifndef HW_TABLE_H_
+#define HW_TABLE_H_
 
 #include <map>
 
-class HTableCell{
-public:
-    HTableCell(){r1=r2=c1=c2=0;}
-    HTableCell(int r1, int r2, int c1, int c2){
+class HTableCell {
+ public:
+    HTableCell() {r1 = r2 = c1 = c2 = 0;}
+    HTableCell(int r1, int r2, int c1, int c2) {
         this->r1 = r1;
         this->r2 = r2;
         this->c1 = c1;
@@ -17,29 +17,28 @@ public:
     int colspan() {return c2 - c1 + 1;}
     int span() {return rowspan() * colspan();}
 
-    bool contain(HTableCell cell){
+    bool contain(HTableCell cell) {
         if (cell.r1 >= r1 && cell.r2 <= r2 &&
-                cell.c1 >= c1 && cell.c2 <= c2)
+            cell.c1 >= c1 && cell.c2 <= c2)
             return true;
         return false;
     }
 
-    int r1,r2,c1,c2;
+    int r1, r2, c1, c2;
 };
 
-class HTable
-{
-public:
+class HTable {
+ public:
     HTable();
 
     void init(int row, int col);
     bool getTableCell(int id, HTableCell& rst);
     HTableCell merge(int lt, int rb);
 
-public:
+ public:
     int row;
     int col;
-    std::map<int, HTableCell> m_mapCells; // id => HTabelCell
+    std::map<int, HTableCell> m_mapCells;   // id => HTabelCell
 };
 
-#endif // H_TABLE_H
+#endif  // HW_TABLE_H_

+ 20 - 21
hthread.h

@@ -1,19 +1,20 @@
-#ifndef H_THREAD_H
-#define H_THREAD_H
+#ifndef HW_THREAD_H_
+#define HW_THREAD_H_
 
-#include "hdef.h"
-#include "hplatform.h"
 #include <thread>
 #include <atomic>
 #include <chrono>
 
+#include "hdef.h"
+#include "hplatform.h"
+
 #ifdef _MSC_VER
-inline uint32 getpid(){
+inline uint32 getpid() {
     return GetCurrentProcessId();
 }
 #endif
 
-inline uint32 gettid(){
+inline uint32 gettid() {
 #ifdef _MSC_VER
     return GetCurrentThreadId();
 #else
@@ -28,8 +29,8 @@ inline uint32 gettid(){
  * first-level virtual: doTask
  * second-level virtual: run
 ************************************************/
-class HThread{
-public:
+class HThread {
+ public:
     HThread() {
         status = STOP;
         status_switch = false;
@@ -37,9 +38,7 @@ public:
         sleep_ms = 0;
     }
 
-    virtual ~HThread() {
-
-    }
+    virtual ~HThread() {}
 
     virtual int start() {
         if (status == STOP) {
@@ -58,7 +57,7 @@ public:
     virtual int stop() {
         if (status != STOP) {
             switchStatus(STOP);
-            thread.join(); // wait thread exit
+            thread.join();  // wait thread exit
         }
         return 0;
     }
@@ -110,13 +109,13 @@ public:
         NO_SLEEP,
     };
 
-    void setSleepPolicy(SleepPolicy policy, long long ms = 0) {
+    void setSleepPolicy(SleepPolicy policy, int64 ms = 0) {
         status_switch = true;
         sleep_policy = policy;
         sleep_ms = ms;
     }
 
-protected:
+ protected:
     void switchStatus(Status stat) {
         status_switch = true;
         status = stat;
@@ -124,11 +123,11 @@ protected:
 
     void sleep() {
         switch (sleep_policy) {
-        case YIELD: 
-            std::this_thread::yield();  
+        case YIELD:
+            std::this_thread::yield();
             break;
-        case SLEEP_FOR: 
-            std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));    
+        case SLEEP_FOR:
+            std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
             break;
         case SLEEP_UNTIL: {
             if (status_switch) {
@@ -136,7 +135,7 @@ protected:
                 base_tp = std::chrono::system_clock::now();
             }
             base_tp += std::chrono::milliseconds(sleep_ms);
-            std::this_thread::sleep_until(base_tp); 
+            std::this_thread::sleep_until(base_tp);
         }
             break;
         default:    // donothing, go all out.
@@ -147,7 +146,7 @@ protected:
     std::atomic<bool> status_switch;
     SleepPolicy sleep_policy;
     std::chrono::system_clock::time_point base_tp;   // for SLEEP_UNTIL
-    long long sleep_ms;
+    int64 sleep_ms;
 };
 
-#endif // H_THREAD_H
+#endif  // HW_THREAD_H_

+ 21 - 20
hthreadpool.h

@@ -1,5 +1,5 @@
-#ifndef H_THREAD_POOL_H
-#define H_THREAD_POOL_H
+#ifndef HW_THREAD_POOL_H_
+#define HW_THREAD_POOL_H_
 
 #include <vector>
 #include <thread>
@@ -9,30 +9,32 @@
 #include <mutex>
 #include <condition_variable>
 #include <future>
+#include <memory>
+#include <utility>
 
 #include "hlog.h"
 #include "hthread.h"
 
-class HThreadPool{
-public:
+class HThreadPool {
+ public:
     using Task = std::function<void()>;
 
-    HThreadPool(int size = std::thread::hardware_concurrency()) : pool_size(size), idle_num(size), status(STOP){
-
+    HThreadPool(int size = std::thread::hardware_concurrency())
+        : pool_size(size), idle_num(size), status(STOP) {
     }
 
-    ~HThreadPool(){
+    ~HThreadPool() {
         stop();
     }
 
     int start() {
         if (status == STOP) {
             status = RUNNING;
-            for (int i = 0; i < pool_size; ++i){
+            for (int i = 0; i < pool_size; ++i) {
                 workers.emplace_back(std::thread([this]{
                     hlogd("work thread[%X] running...", gettid());
-                    while (status != STOP){
-                        while (status == PAUSE){
+                    while (status != STOP) {
+                        while (status == PAUSE) {
                             std::this_thread::yield();
                         }
 
@@ -44,8 +46,8 @@ public:
                             });
 
                             if (status == STOP) return;
-                            
-                            if (!tasks.empty()){
+
+                            if (!tasks.empty()) {
                                 task = std::move(tasks.front());
                                 tasks.pop();
                             }
@@ -54,7 +56,7 @@ public:
                         --idle_num;
                         task();
                         ++idle_num;
-                    }    
+                    }
                 }));
             }
         }
@@ -65,7 +67,7 @@ public:
         if (status != STOP) {
             status = STOP;
             cond.notify_all();
-            for (auto& thread: workers){
+            for (auto& thread : workers) {
                 thread.join();
             }
         }
@@ -91,11 +93,10 @@ public:
     // commit(std::bind(&Class::mem_fn, &obj))
     // commit(std::mem_fn(&Class::mem_fn, &obj))
     template<class Fn, class... Args>
-    auto commit(Fn&& fn, Args&&... args) -> std::future<decltype(fn(args...))>{
+    auto commit(Fn&& fn, Args&&... args) -> std::future<decltype(fn(args...))> {
         using RetType = decltype(fn(args...));
         auto task = std::make_shared<std::packaged_task<RetType()> >(
-            std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...)
-        );
+            std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...));
         std::future<RetType> future = task->get_future();
         {
             std::lock_guard<std::mutex> locker(mutex);
@@ -103,12 +104,12 @@ public:
                 (*task)();
             });
         }
-        
+
         cond.notify_one();
         return future;
     }
 
-public:
+ public:
     int pool_size;
     std::atomic<int> idle_num;
 
@@ -125,4 +126,4 @@ public:
     std::condition_variable cond;
 };
 
-#endif // H_THREAD_POOL_H
+#endif  // HW_THREAD_POOL_H_

+ 18 - 17
htime.cpp

@@ -1,8 +1,9 @@
 #include "htime.h"
+
 #include <stdio.h>
 #include <string.h>
 
-void msleep(unsigned long ms){
+void msleep(unsigned long ms) {
 #ifdef _MSC_VER
     Sleep(ms);
 #else
@@ -10,7 +11,7 @@ void msleep(unsigned long ms){
 #endif
 }
 
-uint64 gettick(){
+uint64 gettick() {
 #ifdef _MSC_VER
     return GetTickCount();
 #else
@@ -20,18 +21,18 @@ uint64 gettick(){
 #endif
 }
 
-datetime_t get_datetime(){
+datetime_t get_datetime() {
     datetime_t  dt;
 #ifdef _MSC_VER
     SYSTEMTIME tm;
-	GetLocalTime(&tm);
-	dt.year     = tm.wYear;
-	dt.month    = tm.wMonth;
-	dt.day      = tm.wDay;
-	dt.hour     = tm.wHour;
-	dt.min      = tm.wMinute;
-	dt.sec      = tm.wSecond;
-	dt.ms       = tm.wMilliseconds;
+    GetLocalTime(&tm);
+    dt.year     = tm.wYear;
+    dt.month    = tm.wMonth;
+    dt.day      = tm.wDay;
+    dt.hour     = tm.wHour;
+    dt.min      = tm.wMinute;
+    dt.sec      = tm.wSecond;
+    dt.ms       = tm.wMilliseconds;
 #else
     struct timeval tv;
     struct tm* tm = NULL;
@@ -46,29 +47,29 @@ datetime_t get_datetime(){
     dt.min      = tm->tm_min;
     dt.sec      = tm->tm_sec;
     dt.ms       = tv.tv_usec/1000;
-#endif    
+#endif
     return dt;
 }
 
 static const char* s_month[] = {"January", "February", "March", "April", "May", "June",
     "July", "August", "September", "October", "November", "December"};
 
-int month_atoi(const char* month){
-    for (int i = 0; i < ARRAY_SIZE(s_month); ++i){
+int month_atoi(const char* month) {
+    for (int i = 0; i < ARRAY_SIZE(s_month); ++i) {
         if (strnicmp(month, s_month[i], strlen(month)) == 0)
             return i+1;
     }
     return 0;
 }
 
-const char* month_itoa(int month){
-    if (month < 1 || month > 12){
+const char* month_itoa(int month) {
+    if (month < 1 || month > 12) {
         return NULL;
     }
     return s_month[month-1];
 }
 
-datetime_t get_compile_datetime(){
+datetime_t get_compile_datetime() {
     static datetime_t dt;
     char month[32];
     sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);

+ 8 - 7
htime.h

@@ -1,11 +1,12 @@
-#ifndef H_TIME_H
-#define H_TIME_H
+#ifndef HW_TIME_H_
+#define HW_TIME_H_
+
+#include <time.h>
 
 #include "hdef.h"
 #include "hplatform.h"
-#include <time.h>
 
-typedef struct datetime_s{
+typedef struct datetime_s {
     int year;
     int month;
     int day;
@@ -13,12 +14,12 @@ typedef struct datetime_s{
     int min;
     int sec;
     int ms;
-}datetime_t;
+} datetime_t;
 
 void msleep(unsigned long ms);
 
 #ifdef _MSC_VER
-inline void sleep(unsigned int s){
+inline void sleep(unsigned int s) {
     Sleep(s*1000);
 }
 #endif
@@ -31,4 +32,4 @@ const char* month_itoa(int month);
 datetime_t get_datetime();
 datetime_t get_compile_datetime();
 
-#endif // H_TIME_H
+#endif  // HW_TIME_H_

+ 12 - 11
hvar.h

@@ -1,13 +1,14 @@
-#ifndef H_VAR_H
-#define H_VAR_H
+#ifndef HW_VAR_H_
+#define HW_VAR_H_
 
-#include "hdef.h"
 #include <stdlib.h>
 #include <string.h>
 
-class HVar{
-public:
-    enum TYPE{
+#include "hdef.h"
+
+class HVar {
+ public:
+    enum TYPE {
         UNKNOWN,
         BOOLEAN,
         INTEGER,
@@ -16,7 +17,7 @@ public:
         POINTER
     } type;
 
-    union DATA{
+    union DATA {
         bool b;
         int64 i;
         float64 f;
@@ -29,8 +30,8 @@ public:
     HVar(int64 i)   {data.i = i; type = INTEGER;}
     HVar(float64 f) {data.f = f; type = FLOAT;}
     HVar(char* str) {
-        data.str = (char*)malloc(strlen(str)+1); 
-        strcpy(data.str, str); 
+        data.str = (char*)malloc(strlen(str)+1);
+        strcpy(data.str, str);
         type = STRING;
     }
     HVar(void* ptr) {data.ptr = ptr; type = POINTER;}
@@ -45,10 +46,10 @@ public:
     bool    isValid()   {return type != UNKNOWN;}
 
     bool    toBool()    {return data.b;}
-    int64   toInt()     {return data.i;}     
+    int64   toInt()     {return data.i;}
     float64 toFloat()   {return data.f;}
     char*   toString()  {return data.str;}
     void*   toPointer() {return data.ptr;}
 };
 
-#endif // H_VAR_H
+#endif  // HW_VAR_H_

+ 10 - 8
hversion.h

@@ -1,9 +1,10 @@
-#ifndef H_VERSION_H
-#define H_VERSION_H
+#ifndef HW_VERSION_H_
+#define HW_VERSION_H_
+
+#include <stdio.h>
 
 #include "hdef.h"
 #include "htime.h"
-#include <stdio.h>
 
 #define VERSION_MAJOR   1
 #define VERSION_MINOR   18
@@ -17,15 +18,16 @@
 
 #define VERSION_HEX     (VERSION_MAJOR << 24) | (VERSION_MINOR << 16) | (VERSION_MICRO << 8) | VERSION_PATCH
 
-inline const char* get_static_version(){
+inline const char* get_static_version() {
     return VERSION_STRING;
 }
 
-inline const char* get_compile_version(){
-    static char version[16];
+inline const char* get_compile_version() {
+    static char version[64];
     static datetime_t dt = get_compile_datetime();
-    sprintf(version, "%d.%d.%d.%d", VERSION_MAJOR, dt.year%100, dt.month, dt.day);
+    snprintf(version, sizeof(version), "%d.%02d.%02d.%02d",
+        VERSION_MAJOR, dt.year%100, dt.month, dt.day);
     return version;
 }
 
-#endif // H_VERSION_H
+#endif  // HW_VERSION_H_

+ 6 - 6
iniparser.cpp

@@ -89,7 +89,7 @@ int IniParser::LoadFromMem(const char* data) {
         if (content[0] == '[') {
             if (content[content.length()-1] == ']') {
                 // section
-                content = trim(content.substr(1,content.length()-2));
+                content = trim(content.substr(1, content.length()-2));
                 pNewNode = new IniNode;
                 pNewNode->type = IniNode::INI_NODE_TYPE_SECTION;
                 pNewNode->label = content;
@@ -97,7 +97,7 @@ int IniParser::LoadFromMem(const char* data) {
                 pScopeNode = pNewNode;
             } else {
                 // hlogw("format error, line:%d", line);
-                continue;   // ignore    
+                continue;   // ignore
             }
         } else {
             pos = content.find_first_of(_delim);
@@ -105,7 +105,7 @@ int IniParser::LoadFromMem(const char* data) {
                 // key-value
                 pNewNode = new IniNode;
                 pNewNode->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
-                pNewNode->label = trim(content.substr(0,pos));
+                pNewNode->label = trim(content.substr(0, pos));
                 pNewNode->value = trim(content.substr(pos+_delim.length()));
                 pScopeNode->Add(pNewNode);
             } else {
@@ -207,11 +207,11 @@ string IniParser::GetValue(const string& key, const string& section) {
 
     IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
     if (pKV == NULL)    return "";
-    
+
     return pKV->value;
 }
 
-void   IniParser::SetValue(const string& key, const string& value, const string& section) {
+void IniParser::SetValue(const string& key, const string& value, const string& section) {
     IniNode* pSection = root_;
     if (section.length() != 0) {
         pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
@@ -231,4 +231,4 @@ void   IniParser::SetValue(const string& key, const string& value, const string&
         pSection->Add(pKV);
     }
     pKV->value = value;
-}
+}

+ 9 - 8
iniparser.h

@@ -1,7 +1,8 @@
-#ifndef HW_INI_PARSER_H
-#define HW_INI_PARSER_H
+#ifndef HW_INI_PARSER_H_
+#define HW_INI_PARSER_H_
 
 #include <list>
+#include <string>
 
 #include "hdef.h"
 #include "hstring.h"
@@ -24,7 +25,7 @@ key = value # span
 // class IniKeyValue;
 // for simplicity, we add a member value.
 class IniNode {
-public:
+ public:
     enum Type {
         INI_NODE_TYPE_UNKNOWN,
         INI_NODE_TYPE_ROOT,
@@ -33,8 +34,8 @@ public:
         INI_NODE_TYPE_DIV,
         INI_NODE_TYPE_SPAN,
     } type;
-    string  label;  
-    string  value;  
+    string  label;
+    string  value;
     std::list<IniNode*>    children;
 
     virtual ~IniNode() {
@@ -85,7 +86,7 @@ public:
 // };
 
 class IniParser {
-public:
+ public:
     IniParser();
     ~IniParser();
 
@@ -104,7 +105,7 @@ public:
     string GetValue(const string& key, const string& section = "");
     void   SetValue(const string& key, const string& value, const string& section = "");
 
-private:
+ private:
     string  _comment;
     string  _delim;
     string  _filepath;
@@ -112,4 +113,4 @@ private:
     IniNode* root_;
 };
 
-#endif // HW_INI_PARSER_H
+#endif  // HW_INI_PARSER_H_

+ 7 - 7
singleton.h

@@ -1,5 +1,5 @@
-#ifndef SINGLETON_H
-#define SINGLETON_H
+#ifndef SINGLETON_H_
+#define SINGLETON_H_
 
 #define DISABLE_COPY(Class) \
     Class(const Class &) = delete; \
@@ -15,17 +15,17 @@
 
 #define IMPL_SINGLETON(Class) \
     Class* Class::s_pInstance = NULL; \
-    Class* Class::instance(){ \
-        if (s_pInstance == NULL){ \
+    Class* Class::instance() { \
+        if (s_pInstance == NULL) { \
             s_pInstance = new Class; \
         } \
         return s_pInstance; \
     } \
-    void Class::exitInstance(){ \
-        if (s_pInstance){  \
+    void Class::exitInstance() { \
+        if (s_pInstance) {  \
             delete s_pInstance; \
             s_pInstance = NULL; \
         }   \
     }
 
-#endif // SINGLETON_H
+#endif  // SINGLETON_H_