From c02f9b6b0de3655f855ddb648c8697b52c05e6b6 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 12 Sep 2022 10:46:05 +0800 Subject: [PATCH] feat: add int list structure --- include/utils/{str.h => structure.h} | 5 +++ src/cleardns.c | 21 ++++++++-- src/common.c | 2 +- src/dnsproxy.c | 2 +- src/overture.c | 2 +- src/utils/CMakeLists.txt | 2 +- src/utils/{str.c => structure.c} | 58 ++++++++++++++++++++++++---- 7 files changed, 78 insertions(+), 14 deletions(-) rename include/utils/{str.h => structure.h} (68%) rename src/utils/{str.c => structure.c} (54%) diff --git a/include/utils/str.h b/include/utils/structure.h similarity index 68% rename from include/utils/str.h rename to include/utils/structure.h index 81f8384..bc79058 100644 --- a/include/utils/str.h +++ b/include/utils/structure.h @@ -4,6 +4,11 @@ char* string_init(const char *str); char* string_join(const char *base, const char *add); +int** int_list_init(); +int int_list_len(int **int_list); +char* int_list_dump(int **int_list); +int** int_list_append(int **int_list, int number); + char** string_list_init(); int string_list_len(char **string_list); char* string_list_dump(char **string_list); diff --git a/src/cleardns.c b/src/cleardns.c index 42231de..7929a3c 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -3,7 +3,7 @@ #include "common.h" #include "dnsproxy.h" #include "overture.h" -#include "str.h" +#include "structure.h" //#include //#include @@ -35,8 +35,23 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); - FILE *fp = fopen("/dev/null/123", "r"); - log_perror("Test prefix"); + int **temp = int_list_init(); + temp = int_list_append(temp, 1232); + temp = int_list_append(temp, 2342); + temp = int_list_append(temp, 3435); + temp = int_list_append(temp, 456); + temp = int_list_append(temp, 567); + + char *str = int_list_dump(temp); + log_info("`%s`", str); + +// char **temp = string_list_init(); +// temp = string_list_append(temp, "1"); +// temp = string_list_append(temp, ""); +// temp = string_list_append(temp, "3"); + +// char *str = string_list_dump(temp); +// log_info("`%s`", str); return 0; diff --git a/src/common.c b/src/common.c index e3f3a14..9054d06 100644 --- a/src/common.c +++ b/src/common.c @@ -1,7 +1,7 @@ #include #include "common.h" #include "logger.h" -#include "str.h" +#include "structure.h" char* show_bool(int value) { if (value) { diff --git a/src/dnsproxy.c b/src/dnsproxy.c index 05cded3..b7110f1 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -1,6 +1,6 @@ #include #include "dnsproxy.h" -#include "str.h" +#include "structure.h" #include "process.h" #include "logger.h" #include "common.h" diff --git a/src/overture.c b/src/overture.c index 2614a11..f98f5f5 100644 --- a/src/overture.c +++ b/src/overture.c @@ -5,7 +5,7 @@ #include "common.h" #include "logger.h" #include "cJSON.h" -#include "str.h" +#include "structure.h" void overture_dump(overture *info); char* overture_gen_config(overture *info); diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 1c11971..bfad49b 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils logger.c str.c cJSON.c) +add_library(utils logger.c structure.c cJSON.c) diff --git a/src/utils/str.c b/src/utils/structure.c similarity index 54% rename from src/utils/str.c rename to src/utils/structure.c index 6a8c30e..1a4274e 100644 --- a/src/utils/str.c +++ b/src/utils/structure.c @@ -1,6 +1,13 @@ +#include #include #include -#include "str.h" +#include "structure.h" + +int* int_init(int number) { + int *data = (int*)malloc(sizeof(int)); + *data = number; + return data; +} char* string_init(const char *str) { return strcpy((char*)malloc(strlen(str) + 1), str); @@ -23,6 +30,14 @@ int string_list_len(char **string_list) { return num - 1; } +char** string_list_append(char **string_list, const char *string) { + int len = string_list_len(string_list); + string_list = (char**)realloc(string_list, sizeof(char*) * (len + 2)); + string_list[len] = string_init(string); + string_list[len + 1] = NULL; // list end sign + return string_list; +} + char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] unsigned long string_len = 0; for (char **string = string_list; *string != NULL; ++string) { @@ -43,10 +58,39 @@ char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] return string_ret; } -char** string_list_append(char **string_list, const char *string) { - int len = string_list_len(string_list); - string_list = (char**)realloc(string_list, sizeof(char*) * (len + 2)); - string_list[len] = string_init(string); - string_list[len + 1] = NULL; // list end sign - return string_list; +int** int_list_init() { + int **int_list = (int**)malloc(sizeof(int*)); + *int_list = NULL; + return int_list; +} + +int int_list_len(int **int_list) { + int num = 0; + while(int_list[num++] != NULL); // get int list size + return num - 1; +} + +int** int_list_append(int **int_list, int number) { + int len = int_list_len(int_list); + int_list = (int**)realloc(int_list, sizeof(int*) * (len + 2)); + int_list[len] = int_init(number); + int_list[len + 1] = NULL; // list end sign + return int_list; +} + +char* int_list_dump(int **int_list) { // [1, 2, 3, ...] + if (int_list_len(int_list) == 0) { + return string_init("[]"); // empty int list + } + char int_str[12]; + char *string_ret = (char*)malloc(sizeof(char) * 2); + string_ret[0] = '['; string_ret[1] = 0x00; + for (int **number = int_list; *number != NULL; ++number) { + sprintf(int_str, "%d", **number); + string_ret = (char*)realloc(string_ret, sizeof(char) * (strlen(string_ret) + 15)); + string_ret = strcat(strcat(string_ret, int_str), ", "); + } + string_ret[strlen(string_ret) - 2] = ']'; + string_ret[strlen(string_ret) - 1] = 0x00; + return string_ret; }