Browse Source

feat: add int list structure

dev
dnomd343 2 years ago
parent
commit
c02f9b6b0d
  1. 5
      include/utils/structure.h
  2. 21
      src/cleardns.c
  3. 2
      src/common.c
  4. 2
      src/dnsproxy.c
  5. 2
      src/overture.c
  6. 2
      src/utils/CMakeLists.txt
  7. 58
      src/utils/structure.c

5
include/utils/str.h → 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);

21
src/cleardns.c

@ -3,7 +3,7 @@
#include "common.h"
#include "dnsproxy.h"
#include "overture.h"
#include "str.h"
#include "structure.h"
//#include <stdio.h>
//#include <string.h>
@ -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;

2
src/common.c

@ -1,7 +1,7 @@
#include <stdio.h>
#include "common.h"
#include "logger.h"
#include "str.h"
#include "structure.h"
char* show_bool(int value) {
if (value) {

2
src/dnsproxy.c

@ -1,6 +1,6 @@
#include <stdlib.h>
#include "dnsproxy.h"
#include "str.h"
#include "structure.h"
#include "process.h"
#include "logger.h"
#include "common.h"

2
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);

2
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)

58
src/utils/str.c → src/utils/structure.c

@ -1,6 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
Loading…
Cancel
Save