Browse Source

update: free of string and uint32 list

dev
dnomd343 2 years ago
parent
commit
3eea855370
  1. 6
      include/utils/structure.h
  2. 20
      src/cleardns.c
  3. 54
      src/utils/structure.c

6
include/utils/structure.h

@ -7,13 +7,15 @@ char* string_init(const char *str);
char* string_join(const char *base, const char *add);
uint32_t** uint32_list_init();
uint32_t uint32_list_len(uint32_t **int_list);
char* uint32_list_dump(uint32_t **int_list);
void uint32_list_free(uint32_t **uint32_list);
uint32_t uint32_list_len(uint32_t **int_list);
uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number);
char** string_list_init();
uint32_t string_list_len(char **string_list);
void string_list_free(char **string_list);
char* string_list_dump(char **string_list);
uint32_t string_list_len(char **string_list);
char** string_list_append(char **string_list, const char *string);
#endif

20
src/cleardns.c

@ -36,15 +36,25 @@ int main(int argc, char *argv[]) { // ClearDNS server
log_info("ClearDNS server start (%s)", VERSION);
char **temp = string_list_init();
// char **temp = string_list_init();
// temp = string_list_append(temp, "123");
// temp = string_list_append(temp, "abc");
// temp = string_list_append(temp, "ok");
char *str = string_list_dump(temp);
log_info("`%s`", str);
//
// char *str = string_list_dump(temp);
// log_info("`%s`", str);
// free(str);
// string_list_free(temp);
uint32_t **temp = uint32_list_init();
temp = uint32_list_append(temp, 123);
temp = uint32_list_append(temp, 456);
temp = uint32_list_append(temp, 789);
char *str = uint32_list_dump(temp);
log_info("`%s`\n", str);
free(str);
uint32_list_free(temp);
// dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT);
//

54
src/utils/structure.c

@ -8,10 +8,16 @@ char* string_init(const char *str) { // new string
}
char* string_join(const char *base, const char *add) { // combine string
char *ret = (char*)malloc(sizeof(char) * (strlen(base) + strlen(add) + 1));
char *ret = (char *)malloc(strlen(base) + strlen(add) + 1);
return strcat(strcpy(ret, base), add);
}
uint32_t* uint32_init(uint32_t number) { // new uint32 (by malloc)
uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));
*data = number;
return data;
}
char** string_list_init() { // init string list
char **string_list = (char **)malloc(sizeof(char *));
*string_list = NULL; // list end sign
@ -20,7 +26,7 @@ char** string_list_init() { // init string list
uint32_t string_list_len(char **string_list) { // get len of string list
uint32_t num = 0;
while(string_list[num++] != NULL); // get string list size
while(string_list[num++] != NULL); // get list size
return num - 1;
}
@ -32,10 +38,11 @@ char** string_list_append(char **string_list, const char *string) { // add new s
return string_list;
}
// TODO: string list free
void string_list_free(char **string_list) { // free string list
// free every string
// free list itself
for (char **string = string_list; *string != NULL; ++string) {
free(*string);
}
free(string_list);
}
char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
@ -52,25 +59,19 @@ char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
return strcat(string_ret, "]");
}
uint32_t** uint32_list_init() {
uint32_t **int_list = (uint32_t **)malloc(sizeof(uint32_t *));
*int_list = NULL;
return int_list;
}
uint32_t* uint32_init(uint32_t number) {
uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));
*data = number;
return data;
uint32_t** uint32_list_init() { // init uint32 list
uint32_t **uint32_list = (uint32_t **)malloc(sizeof(uint32_t *));
*uint32_list = NULL;
return uint32_list;
}
uint32_t uint32_list_len(uint32_t **int_list) {
uint32_t uint32_list_len(uint32_t **uint32_list) { // get len of uint32 list
uint32_t num = 0;
while(int_list[num++] != NULL); // get int list size
while(uint32_list[num++] != NULL); // get list size
return num - 1;
}
uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) {
uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) { // add new uint32 at the end of list
uint32_t len = uint32_list_len(uint32_list);
uint32_list = (uint32_t **)realloc(uint32_list, sizeof(uint32_t *) * (len + 2));
uint32_list[len] = uint32_init(number);
@ -78,22 +79,25 @@ uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) {
return uint32_list;
}
void uint32_list_free(uint32_t **uint32_list) { // free uint32 list
for (uint32_t **number = uint32_list; *number != NULL; ++number) {
free(*number);
}
free(uint32_list);
}
char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...]
if (uint32_list_len(uint32_list) == 0) {
return string_init("[]"); // empty int list
}
char uint32_str[12];
char *string_ret = (char *)malloc(2);
string_ret[0] = '[';
string_ret[1] = '\0';
strcpy(string_ret, "[");
for (uint32_t **number = uint32_list; *number != NULL; ++number) {
sprintf(uint32_str, "%d", **number);
string_ret = (char*)realloc(string_ret, strlen(string_ret) + 15);
string_ret = strcat(strcat(string_ret, uint32_str), ", ");
}
string_ret[strlen(string_ret) - 2] = ']';
string_ret[strlen(string_ret) - 1] = '\0';
return string_ret;
string_ret[strlen(string_ret) - 2] = '\0';
return strcat(string_ret, "]");
}
// TODO: uint32 list free

Loading…
Cancel
Save