Forráskód Böngészése

Add hv_parse_size, hv_parse_time

ithewei 3 éve
szülő
commit
19267353a7
7 módosított fájl, 93 hozzáadás és 7 törlés
  1. 1 0
      Makefile
  2. 49 1
      base/hbase.c
  3. 9 5
      base/hbase.h
  4. 2 0
      docs/API.md
  5. 1 1
      scripts/unittest.sh
  6. 3 0
      unittest/CMakeLists.txt
  7. 28 0
      unittest/hbase_test.c

+ 1 - 0
Makefile

@@ -204,6 +204,7 @@ protorpc_server: prepare protorpc_protoc
 
 unittest: prepare
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/rbtree_test       unittest/rbtree_test.c        base/rbtree.c
+	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/hbase_test        unittest/hbase_test.c         base/hbase.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/mkdir_p           unittest/mkdir_test.c         base/hbase.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/rmdir_p           unittest/rmdir_test.c         base/hbase.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/date              unittest/date_test.c          base/htime.c

+ 49 - 1
base/hbase.c

@@ -335,7 +335,7 @@ int hv_rand(int min, int max) {
     return _rand;
 }
 
-void hv_random_string(char *buf, int len) {
+char* hv_random_string(char *buf, int len) {
     static char s_characters[] = {
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
         'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
@@ -346,6 +346,7 @@ void hv_random_string(char *buf, int len) {
         buf[i] = s_characters[hv_rand(0, sizeof(s_characters) - 1)];
     }
     buf[i] = '\0';
+    return buf;
 }
 
 bool hv_getboolean(const char* str) {
@@ -361,3 +362,50 @@ bool hv_getboolean(const char* str) {
     default: return false;
     }
 }
+
+size_t hv_parse_size(const char* str) {
+    size_t size = 0, n = 0;
+    const char* p = str;
+    char c;
+    while ((c = *p) != '\0') {
+        if (c >= '0' && c <= '9') {
+            n = n * 10 + c - '0';
+        } else {
+            switch (c) {
+            case 'K': case 'k': n <<= 10; break;
+            case 'M': case 'm': n <<= 20; break;
+            case 'G': case 'g': n <<= 30; break;
+            case 'T': case 't': n <<= 40; break;
+            default:                      break;
+            }
+            size += n;
+            n = 0;
+        }
+        ++p;
+    }
+    return size + n;
+}
+
+time_t hv_parse_time(const char* str) {
+    time_t time = 0, n = 0;
+    const char* p = str;
+    char c;
+    while ((c = *p) != '\0') {
+        if (c >= '0' && c <= '9') {
+            n = n * 10 + c - '0';
+        } else {
+            switch (c) {
+            case 's':                           break;
+            case 'm': n *= 60;                  break;
+            case 'h': n *= 60 * 60;             break;
+            case 'd': n *= 24 * 60 * 60;        break;
+            case 'w': n *= 7 * 24 * 60 * 60;    break;
+            default:                            break;
+            }
+            time += n;
+            n = 0;
+        }
+        ++p;
+    }
+    return time + n;
+}

+ 9 - 5
base/hbase.h

@@ -103,11 +103,15 @@ HV_EXPORT char* get_executable_file(char* buf, int size);
 HV_EXPORT char* get_run_dir(char* buf, int size);
 
 // random
-HV_EXPORT int  hv_rand(int min, int max);
-HV_EXPORT void hv_random_string(char *buf, int len);
-
-// 1 y on yes true enable
-HV_EXPORT bool hv_getboolean(const char* str);
+HV_EXPORT int   hv_rand(int min, int max);
+HV_EXPORT char* hv_random_string(char *buf, int len);
+
+// 1 y on yes true enable => true
+HV_EXPORT bool   hv_getboolean(const char* str);
+// 1T2G3M4K5B => ?B
+HV_EXPORT size_t hv_parse_size(const char* str);
+// 1w2d3h4m5s => ?s
+HV_EXPORT time_t hv_parse_time(const char* str);
 
 END_EXTERN_C
 

+ 2 - 0
docs/API.md

@@ -124,6 +124,8 @@
 - hv_rand
 - hv_random_string
 - hv_getboolean
+- hv_parse_size
+- hv_parse_time
 
 ### hversion.h
 - hv_version

+ 1 - 1
scripts/unittest.sh

@@ -5,7 +5,7 @@ ROOT_DIR=${SCRIPT_DIR}/..
 cd ${ROOT_DIR}
 
 bin/rbtree_test
-
+bin/hbase_test
 bin/date
 bin/ifconfig
 bin/mkdir_p 123/456

+ 3 - 0
unittest/CMakeLists.txt

@@ -1,6 +1,9 @@
 add_definitions(-DHV_SOURCE=1)
 
 # ------base------
+add_executable(hbase_test hbase_test.c ../base/hbase.c)
+target_include_directories(hbase_test PRIVATE .. ../base)
+
 add_executable(mkdir_p mkdir_test.c ../base/hbase.c)
 target_include_directories(mkdir_p PRIVATE .. ../base)
 

+ 28 - 0
unittest/hbase_test.c

@@ -0,0 +1,28 @@
+#include "hbase.h"
+
+int main(int argc, char* argv[]) {
+    assert(hv_getboolean("1"));
+    assert(hv_getboolean("yes"));
+
+    assert(hv_parse_size("256") == 256);
+    assert(hv_parse_size("1K") == 1024);
+    assert(hv_parse_size("1G2M3K4B") ==
+            1 * 1024 * 1024 * 1024 +
+            2 * 1024 * 1024 +
+            3 * 1024 +
+            4);
+
+    assert(hv_parse_time("30") == 30);
+    assert(hv_parse_time("1m") == 60);
+    assert(hv_parse_time("1d2h3m4s") ==
+            1 * 24 * 60 * 60 +
+            2 * 60 * 60 +
+            3 * 60 +
+            4);
+
+    char buf[16] = {0};
+    printf("%d\n", hv_rand(10, 99));
+    printf("%s\n", hv_random_string(buf, 10));
+
+    return 0;
+}