Add UDPC_strtoa

This commit is contained in:
Stephen Seo 2019-03-04 15:05:21 +09:00
parent 4776a2f202
commit 89afc58bc7
3 changed files with 64 additions and 3 deletions

View 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;
}
}

View 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

View 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;
}