ithewei před 6 roky
rodič
revize
b6d76333c6
13 změnil soubory, kde provedl 146 přidání a 65 odebrání
  1. 4 4
      h.h
  2. 7 6
      hdef.h
  3. 2 5
      hendian.h
  4. 1 1
      hframe.cpp
  5. 5 2
      hifconf.cpp
  6. 3 5
      hmain.cpp
  7. 1 1
      hmain.h
  8. 93 11
      hplatform.h
  9. 5 5
      hsysinfo.h
  10. 4 6
      hthread.h
  11. 6 4
      htime.cpp
  12. 2 2
      htime.h
  13. 13 13
      main.cpp.tmpl

+ 4 - 4
h.h

@@ -7,16 +7,16 @@
 
 // platform
 #include "hplatform.h"
-
-#include "hversion.h"
 #include "hdef.h"
+#include "hversion.h"
+
 #include "herr.h"
 #include "hlog.h"
-#include "htime.h"
 
+#include "htime.h"
 #include "hstring.h"
-#include "hbuf.h"
 #include "hfile.h"
+#include "hbuf.h"
 #include "hbytearray.h"
 
 #include "hscope.h"

+ 7 - 6
hdef.h

@@ -1,12 +1,7 @@
 #ifndef HW_DEF_H_
 #define HW_DEF_H_
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <string>
-#include <map>
+#include "hplatform.h"
 
 typedef unsigned char       uint8;
 typedef unsigned short      uint16;
@@ -30,7 +25,11 @@ typedef void*               handle;
 typedef int (*method_t)(void* userdata);
 typedef void (*procedure_t)(void* userdata);
 
+#ifdef __cplusplus
+#include <string>
+#include <map>
 typedef std::map<std::string, std::string> keyval_t;
+#endif
 
 #ifndef MAX_PATH
 #define MAX_PATH          260
@@ -109,6 +108,7 @@ typedef std::map<std::string, std::string> keyval_t;
 ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
 #endif
 
+#ifndef OS_WIN
 #ifndef MAKE_WORD
 #define MAKE_WORD(h, l)  ( (((WORD)h) << 8) | (l & 0xff) )
 #endif
@@ -120,6 +120,7 @@ typedef std::map<std::string, std::string> keyval_t;
 #ifndef LOBYTE
 #define LOBYTE(w) ( (BYTE)(w & 0xff) )
 #endif
+#endif
 
 #define MAKE_INT32(h, l)   ( ((int32)h) << 16 | (l & 0xffff) )
 #define HIINT16(n)        ( (int16)(((int32)n) >> 16) )

+ 2 - 5
hendian.h

@@ -4,13 +4,10 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "hplatform.h"
 #include "hdef.h"
 
-#define LITTLE_ENDIAN   0
-#define BIG_ENDIAN      1
-#define NET_ENDIAN      BIG_ENDIAN
-
-int check_endian() {
+int detect_endian() {
     union {
         char c;
         short s;

+ 1 - 1
hframe.cpp

@@ -10,7 +10,7 @@ int HFrameBuf::push(HFrame* pFrame) {
 
     std::lock_guard<std::mutex> locker(mutex);
 
-    if (frames.size() >= cache_num) {
+    if (frames.size() >= (size_t)cache_num) {
         hlogd("frame cache full!");
         if (policy == HFrameBuf::DISCARD) {
             return -20;     // note: cache full, discard frame

+ 5 - 2
hifconf.cpp

@@ -4,7 +4,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef __unix__
+#include "hplatform.h"
+
+#ifdef OS_UNIX
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -91,7 +93,7 @@ int ifconfig(std::vector<ifconfig_t>& ifcs) {
     close(sock);
     return 0;
 }
-#elif defined(_WIN32)
+#elif defined(OS_WIN)
 #include <winsock2.h>
 #include <windows.h>
 #include <IphlpApi.h>
@@ -170,3 +172,4 @@ int ifconfig(std::vector<ifconfig_t>& ifcs) {
     return -10; // unimplemented
 }
 #endif
+

+ 3 - 5
hmain.cpp

@@ -26,7 +26,7 @@ int main_ctx_init(int argc, char** argv) {
         --e;
     }
     strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
-#ifdef _WIN32
+#ifdef OS_WIN
     if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
         *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
     }
@@ -108,12 +108,10 @@ int main_ctx_init(int argc, char** argv) {
     snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
 
     g_main_ctx.oldpid = getpid_from_pidfile();
-#ifdef __unix__
+#ifdef OS_UNIX
     if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
         g_main_ctx.oldpid = -1;
     }
-#else
-
 #endif
 
     return 0;
@@ -135,7 +133,7 @@ const char* get_env(const char* key) {
     return iter->second.c_str();
 }
 
-#ifdef __unix__
+#ifdef OS_UNIX
 /*
  * memory layout
  * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0

+ 1 - 1
hmain.h

@@ -37,7 +37,7 @@ int main_ctx_init(int argc, char** argv);
 const char* get_arg(const char* key);
 const char* get_env(const char* key);
 
-#ifdef __unix__
+#ifdef OS_UNIX
 void setproctitle(const char* title);
 #endif
 

+ 93 - 11
hplatform.h

@@ -1,7 +1,87 @@
 #ifndef HW_PLATFORM_H_
 #define HW_PLATFORM_H_
 
-#ifdef _WIN32
+// OS
+#if defined(WIN32)|| defined(_WIN32)
+    #define OS_WIN32
+#elif defined(WIN64) || defined(_WIN64)
+    #define OS_WIN64
+    #define OS_WIN32
+#elif defined(linux) || defined(__linux) || defined(__linux__)
+    #define OS_LINUX
+#elif defined(ANDROID) || defined(__ANDROID__)
+    #define OS_ANDROID
+    #define OS_LINUX
+#elif defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
+    #include <TargetConditionals.h>
+    #if defined(TARGET_OS_MAC) && TARGET_OS_MAC
+        #define OS_MAC
+        #define OS_DARWIN
+        #ifdef __LP64__
+            #define OS_DARWIN64
+        #else
+            #define OS_DARWIN32
+        #endif
+    #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+        #define OS_IOS
+    #else
+        #define OS_MACOS
+    #endif
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+    #define OS_FREEBSD
+#elif defined(__NetBSD__)
+    #define OS_NETBSD
+#elif defined(__OpenBSD__)
+    #define OS_OPENBSD
+#else
+    #error "Unsupported operating system platform!"
+#endif
+
+#if defined(OS_WIN32) || defined(OS_WIN64)
+    #undef  OS_UNIX
+    #define OS_WIN
+#else
+    #define OS_UNIX
+#endif
+
+// CC
+// _MSC_VER
+// __MINGW32__
+// __GNUC__
+// __clang__
+
+// ARCH
+#if defined(__i386) || defined(__i386__) || defined(_M_IX86)
+    #define ARCH_X86
+    #define ARCH_X86_32
+#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
+    #define ARCH_X64
+    #define ARCH_X86_64
+#elif defined(__arm__)
+    #define ARCH_ARM
+#elif defined(__aarch64__) || defined(__ARM64__)
+    #define ARCH_ARM64
+#endif
+
+// ENDIAN
+#ifndef BIG_ENDIAN
+    #define BIG_ENDIAN      4321
+#endif
+#ifndef LITTLE_ENDIAN
+    #define LITTLE_ENDIAN   1234
+#endif
+#define NET_ENDIAN     BIG_ENDIAN
+
+#ifndef BYTE_ORDER
+#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
+    #define BYTE_ORDER LITTLE_ENDIAN
+#elif defined(__ARMEB__)
+    #define BYTE_ORDER BIG_ENDIAN
+#endif
+#endif
+
+// header files
+#ifdef OS_WIN
     #define WIN32_LEAN_AND_MEAN
     #define _CRT_SECURE_NO_WARNINGS
     #define _CRT_NONSTDC_NO_DEPRECATE
@@ -12,6 +92,7 @@
 
     #define strcasecmp stricmp
     #define strncasecmp strnicmp
+    #define MKDIR(dir) mkdir(dir)
 #else
     #include <unistd.h> // for daemon
     #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
@@ -23,8 +104,19 @@
 
     #define stricmp     strcasecmp
     #define strnicmp    strncasecmp
+    #define MKDIR(dir) mkdir(dir, 0777)
 #endif
 
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include <error.h>
+
 #ifdef _MSC_VER
     typedef int pid_t;
     typedef int gid_t;
@@ -37,14 +129,4 @@
     #define GNUC_ALIGN(n)
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <signal.h>
-
-#ifdef __unix__
-#define MKDIR(dir) mkdir(dir, 0777)
-#else
-#define MKDIR(dir) mkdir(dir)
-#endif
-
 #endif  // HW_PLATFORM_H_

+ 5 - 5
hsysinfo.h

@@ -6,12 +6,12 @@
 
 // logic processors number
 inline int get_ncpu() {
-#ifdef __unix__
-    return sysconf(_SC_NPROCESSORS_CONF);
-#elif defined(_WIN32)
+#ifdef OS_WIN
     SYSTEM_INFO si;
     GetSystemInfo(&si);
     return si.dwNumberOfProcessors;
+#else
+    return sysconf(_SC_NPROCESSORS_CONF);
 #endif
     return 1;
 }
@@ -22,7 +22,7 @@ typedef struct meminfo_s {
 } meminfo_t;
 
 inline int get_meminfo(meminfo_t* mem) {
-#ifdef __linux__
+#ifdef OS_LINUX
     // cat /proc/meminfo
     // MemTotal:       16432348 kB
     // MemFree:          180704 kB
@@ -47,7 +47,7 @@ inline int get_meminfo(meminfo_t* mem) {
     //printf("%s %u %s\n", name, num, unit);
     mem->avail = num >> 10;
     return 0;
-#elif defined(_WIN32)
+#elif defined(OS_WIN)
     MEMORYSTATUSEX memstat;
     memset(&memstat, 0, sizeof(memstat));
     memstat.dwLength = sizeof(memstat);

+ 4 - 6
hthread.h

@@ -5,14 +5,12 @@
 #include <atomic>
 #include <chrono>
 
-#include "hdef.h"
 #include "hplatform.h"
+#include "hdef.h"
 
-#ifdef _WIN32
-#define gettid GetCurrentThreadId
-#endif
-
-#if defined(__unix__) && !defined(__ANDROID__)
+#ifdef OS_WIN
+#define gettid  GetCurrentThreadId
+#elif !defined(OS_ANDROID)
 #define gettid  pthread_self
 #endif
 

+ 6 - 4
htime.cpp

@@ -3,8 +3,10 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "hplatform.h"
+
 void msleep(unsigned long ms) {
-#ifdef _WIN32
+#ifdef OS_WIN
     Sleep(ms);
 #else
     usleep(ms*1000);
@@ -12,7 +14,7 @@ void msleep(unsigned long ms) {
 }
 
 uint64 gettick() {
-#ifdef _WIN32
+#ifdef OS_WIN
     return GetTickCount();
 #else
     struct timeval tv;
@@ -23,7 +25,7 @@ uint64 gettick() {
 
 datetime_t get_datetime() {
     datetime_t  dt;
-#ifdef _WIN32
+#ifdef OS_WIN
     SYSTEMTIME tm;
     GetLocalTime(&tm);
     dt.year     = tm.wYear;
@@ -55,7 +57,7 @@ static const char* s_month[] = {"January", "February", "March", "April", "May",
     "July", "August", "September", "October", "November", "December"};
 
 int month_atoi(const char* month) {
-    for (int i = 0; i < ARRAY_SIZE(s_month); ++i) {
+    for (size_t i = 0; i < ARRAY_SIZE(s_month); ++i) {
         if (strnicmp(month, s_month[i], strlen(month)) == 0)
             return i+1;
     }

+ 2 - 2
htime.h

@@ -3,8 +3,8 @@
 
 #include <time.h>
 
-#include "hdef.h"
 #include "hplatform.h"
+#include "hdef.h"
 
 typedef struct datetime_s {
     int year;
@@ -18,7 +18,7 @@ typedef struct datetime_s {
 
 void msleep(unsigned long ms);
 
-#ifdef _WIN32
+#ifdef OS_WIN
 inline void sleep(unsigned int s) {
     Sleep(s*1000);
 }

+ 13 - 13
main.cpp.tmpl

@@ -54,15 +54,16 @@ void print_help() {
     printf("Options:\n%s\n", detail_options);
 }
 
-#define INVALID_OPTION  -1
-#define FLAG_OPTION     1
-#define PARMA_OPTION    2
+#define INVALID_OPTION      -1
+#define NO_ARGUMENT         0
+#define REQUIRED_ARGUMENT   1
+#define OPTIONAL_ARGUMENT   2
 int get_option(char opt) {
     char* p = options;
     while (*p && *p != opt) ++p;
     if (*p == '\0')     return INVALID_OPTION;
-    if (*(p+1) == ':')  return PARMA_OPTION;
-    return FLAG_OPTION;
+    if (*(p+1) == ':')  return REQUIRED_ARGUMENT;
+    return NO_ARGUMENT;
 }
 
 int parse_cmdline(int argc, char** argv) {
@@ -78,10 +79,10 @@ int parse_cmdline(int argc, char** argv) {
             case INVALID_OPTION:
                 printf("Invalid option: '%c'\n", *p);
                 exit(-20);
-            case FLAG_OPTION:
+            case NO_ARGUMENT:
                 g_main_ctx.arg_kv[std::string(p, 1)] = "true";
                 break;
-            case PARMA_OPTION:
+            case REQUIRED_ARGUMENT:
                 if (*(p+1) != '\0') {
                     g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
                     ++i;
@@ -156,7 +157,7 @@ int parse_confile(const char* confile) {
     return 0;
 }
 
-#ifdef __unix__
+#ifdef OS_UNIX
 // unix use signal
 // we use SIGTERM to quit process
 #define SIGNAL_TERMINATE    SIGTERM
@@ -208,7 +209,7 @@ int signal_init() {
 
 void signal_cleanup() {
 }
-#elif defined(_WIN32)
+#elif defined(OS_WIN)
 // win32 use Event
 static HANDLE s_hEventTerm = NULL;
 
@@ -253,7 +254,7 @@ void handle_signal() {
             }
         } else if (strcmp(signal, "stop") == 0) {
             if (g_main_ctx.oldpid > 0) {
-#ifdef __unix__
+#ifdef OS_UNIX
                 kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
 #else
                 SetEvent(s_hEventTerm);
@@ -265,7 +266,7 @@ void handle_signal() {
             exit(0);
         } else if (strcmp(signal, "restart") == 0) {
             if (g_main_ctx.oldpid > 0) {
-#ifdef __unix__
+#ifdef OS_UNIX
                 kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
 #else
                 SetEvent(s_hEventTerm);
@@ -328,7 +329,7 @@ int main(int argc, char** argv) {
     signal_init();
     handle_signal();
 
-#ifdef __unix__
+#ifdef OS_UNIX
     // daemon
     if (get_arg("d")) {
         // nochdir, noclose
@@ -371,4 +372,3 @@ void master_proc(void* userdata) {
 void worker_proc(void* userdata) {
     while(1) msleep(1000);
 }
-