From 36142ef5c543cd8a033bca3cea51db0d6fbdabe5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 10:04:21 +0800 Subject: [PATCH] feat: string load demo --- include/common/sundry.h | 1 + src/cleardns.c | 10 +++++++--- src/common/sundry.c | 11 ++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/common/sundry.h b/include/common/sundry.h index b841a91..f12a2a5 100644 --- a/include/common/sundry.h +++ b/include/common/sundry.h @@ -4,6 +4,7 @@ #include char* show_bool(uint8_t value); +char* string_load(const char *fmt, ...); char* uint32_to_string(uint32_t number); char* string_join(const char *base, const char *add); void string_list_debug(char *describe, char **string_list); diff --git a/src/cleardns.c b/src/cleardns.c index 3ad366a..e1f4f0e 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -115,8 +115,12 @@ void cleardns() { // cleardns service } int main(int argc, char *argv[]) { - init(argc, argv); - log_info("ClearDNS server start (%s)", VERSION); - cleardns(); +// init(argc, argv); +// log_info("ClearDNS server start (%s)", VERSION); +// cleardns(); + + char *test = string_load("`%s` -> %d", "test", 2333); + printf("%s\n", test); + return 0; } diff --git a/src/common/sundry.c b/src/common/sundry.c index bb6ff2c..ab5e9de 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -1,8 +1,9 @@ #include +#include #include #include -#include #include +#include #include "logger.h" #include "constant.h" #include "structure.h" @@ -19,6 +20,14 @@ char* string_join(const char *base, const char *add) { // combine string return strcat(strcpy(ret, base), add); } +char* string_load(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + char *buf = NULL; + vasprintf(&buf, fmt, ap); + return buf; +} + char* uint32_to_string(uint32_t number) { // convert uint32 -> string char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes) sprintf(to_str, "%u", number);