]> git.seodisparate.com - UDPConnection/commitdiff
Add UDPC_strtoa
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 06:05:21 +0000 (15:05 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 06:05:21 +0000 (15:05 +0900)
src/UDPConnection.c
src/UDPConnection.h
src/test/UDPC_UnitTest.c

index 3fdc887215406a89d2da298f0c186badc968a28f..66a24894f4bc9e441bc77d452baa97a20fecc6ee 100644 (file)
@@ -1269,3 +1269,38 @@ void UDPC_INTERNAL_check_ids(void *userData, uint32_t addr, char *data)
         *((uint32_t*)userData) = 0x10000000;
     }
 }
+
+uint32_t UDPC_strtoa(const char *addrStr)
+{
+    uint32_t addr = 0;
+    uint32_t temp = 0;
+    uint32_t index = 0;
+    while(*addrStr != 0)
+    {
+        if(*addrStr >= '0' && *addrStr <= '9')
+        {
+            temp *= 10;
+            temp += *addrStr - '0';
+        }
+        else if(*addrStr == '.' && temp <= 0xFF && index < 3)
+        {
+            addr |= (temp << (24 - 8 * index++));
+            temp = 0;
+        }
+        else
+        {
+            return 0;
+        }
+        ++addrStr;
+    }
+
+    if(index == 3 && temp <= 0xFF)
+    {
+        addr |= temp;
+        return addr;
+    }
+    else
+    {
+        return 0;
+    }
+}
index c4728ba042ba3d9a0202e1df6c6f85e55d9b6ea0..91af8903b23c1004d1d888c3e76ff4f9d019f1b7 100644 (file)
@@ -240,4 +240,6 @@ uint32_t UDPC_INTERNAL_generate_id(UDPC_Context *ctx);
 
 void UDPC_INTERNAL_check_ids(void *userData, uint32_t addr, char *data);
 
+uint32_t UDPC_strtoa(const char *addrStr);
+
 #endif
index 32ac41478adaf3a94fa8685618c61e9ae811ec9e..2229ec8786786211fcc1e03fa1b2bb8783aa76b1 100644 (file)
@@ -244,12 +244,11 @@ void TEST_DEQUE()
 
 void TEST_ATOSTR()
 {
-    UDPC_Context *ctx = malloc(sizeof(UDPC_Context));
+    UDPC_Context ctx;
     ASSERT_EQ_MEM(
-        UDPC_INTERNAL_atostr(ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
+        UDPC_INTERNAL_atostr(&ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
         "172.30.1.255",
         13);
-    free(ctx);
     UNITTEST_REPORT(ATOSTR);
 }
 
@@ -386,10 +385,35 @@ void TEST_HASHMAP()
     UNITTEST_REPORT(HASHMAP);
 }
 
+void TEST_STRTOA()
+{
+    ASSERT_EQ(0x01020304, UDPC_strtoa("1.2.3.4"));
+    ASSERT_EQ(0x7F000001, UDPC_strtoa("127.0.0.1"));
+    ASSERT_EQ(0xC0A801FF, UDPC_strtoa("192.168.1.255"));
+    ASSERT_EQ(0, UDPC_strtoa("1.2.3.4.5"));
+    ASSERT_EQ(0, UDPC_strtoa("100.20.30"));
+    ASSERT_EQ(0, UDPC_strtoa("200.400.30.50"));
+    UNITTEST_REPORT(STRTOA);
+}
+
+void TEST_ATOSTRTOA()
+{
+    UDPC_Context ctx;
+
+    ASSERT_EQ(0x01020304, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0x01020304)));
+    ASSERT_EQ(0x7F000001, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0x7F000001)));
+    ASSERT_EQ(0xC0A801FF, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0xC0A801FF)));
+    ASSERT_EQ(0xFFFEFDFC, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0xFFFEFDFC)));
+
+    UNITTEST_REPORT(ATOSTRTOA);
+}
+
 int main()
 {
     TEST_DEQUE();
     TEST_ATOSTR();
+    TEST_STRTOA();
+    TEST_ATOSTRTOA();
     TEST_HASHMAP();
     return 0;
 }