From 22cb2ada73c759e78991f3940a399eeb91191627 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 22 Feb 2022 14:22:56 +0800 Subject: [PATCH] config --- include/libs/config/config.h | 9 ++-- source/common/src/tglobal.c | 2 + source/libs/config/src/config.c | 81 +++++++++++++++++++++++++---- source/libs/config/test/cfgTest.cpp | 8 +-- 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/include/libs/config/config.h b/include/libs/config/config.h index d603595cbf..cf8daae72b 100644 --- a/include/libs/config/config.h +++ b/include/libs/config/config.h @@ -46,6 +46,9 @@ typedef enum { CFG_DTYPE_STRING, CFG_DTYPE_IPSTR, CFG_DTYPE_DIR, + CFG_DTYPE_LOCALE, + CFG_DTYPE_CHARSET, + CFG_DTYPE_TIMEZONE } ECfgDataType; typedef enum { @@ -71,8 +74,6 @@ typedef struct SConfigItem { int64_t int64Val; float floatVal; char *strVal; - char *ipstrVal; - char *dirVal; }; union { int64_t minIntVal; @@ -82,7 +83,6 @@ typedef struct SConfigItem { int64_t maxIntVal; double maxFloatVal; }; - } SConfigItem; typedef struct SConfig SConfig; @@ -110,6 +110,9 @@ int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddLocale(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddCharset(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); +int32_t cfgAddTimezone(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype); const char *cfgStypeStr(ECfgSrcType type); const char *cfgDtypeStr(ECfgDataType type); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 364820ba07..398ded034c 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -196,6 +196,7 @@ void taosSetAllDebugFlag() { } int32_t taosCfgDynamicOptions(char *msg) { + #if 0 char *option, *value; int32_t olen, vlen; int32_t vint = 0; @@ -265,6 +266,7 @@ int32_t taosCfgDynamicOptions(char *msg) { } } +#endif return false; } diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 41d59d20a5..a40783eaef 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -262,10 +262,6 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) { if (pItem->dtype == CFG_DTYPE_STRING) { free(pItem->strVal); - } else if (pItem->dtype == CFG_DTYPE_IPSTR) { - free(pItem->ipstrVal); - } else if (pItem->dtype == CFG_DTYPE_DIR) { - free(pItem->dirVal); } free(pItem->name); terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -367,8 +363,8 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, } SConfigItem item = {.dtype = CFG_DTYPE_IPSTR}; - item.ipstrVal = strdup(defaultVal); - if (item.ipstrVal == NULL) { + item.strVal = strdup(defaultVal); + if (item.strVal == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -394,9 +390,9 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { return -1; } - tfree(pItem->dirVal); - pItem->dirVal = strdup(fullDir); - if (pItem->dirVal == NULL) { + tfree(pItem->strVal); + pItem->strVal = strdup(fullDir); + if (pItem->strVal == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -409,6 +405,67 @@ int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, EC if (cfgCheckAndSetDir(&item, defaultVal) != 0) { return -1; } + + return cfgAddItem(pConfig, &item, name, utype); +} + +static int32_t cfgCheckAndSetLocale(SConfigItem *pItem, const char *locale) { + tfree(pItem->strVal); + pItem->strVal = strdup(locale); + if (pItem->strVal == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + return 0; +} + +int32_t cfgAddLocale(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_LOCALE}; + if (cfgCheckAndSetLocale(&item, defaultVal) != 0) { + return -1; + } + + return cfgAddItem(pConfig, &item, name, utype); +} + +static int32_t cfgCheckAndSetCharset(SConfigItem *pItem, const char *charset) { + tfree(pItem->strVal); + pItem->strVal = strdup(charset); + if (pItem->strVal == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + return 0; +} + +int32_t cfgAddCharset(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_CHARSET}; + if (cfgCheckAndSetCharset(&item, defaultVal) != 0) { + return -1; + } + + return cfgAddItem(pConfig, &item, name, utype); +} + +static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) { + tfree(pItem->strVal); + pItem->strVal = strdup(timezone); + if (pItem->strVal == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + return 0; +} + +int32_t cfgAddTimezone(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) { + SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE}; + if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) { + return -1; + } + return cfgAddItem(pConfig, &item, name, utype); } @@ -455,6 +512,12 @@ const char *cfgDtypeStr(ECfgDataType type) { return "ipstr"; case CFG_DTYPE_DIR: return "dir"; + case CFG_DTYPE_LOCALE: + return "locale"; + case CFG_DTYPE_CHARSET: + return "charset"; + case CFG_DTYPE_TIMEZONE: + return "timezone"; default: return "invalid"; } diff --git a/source/libs/config/test/cfgTest.cpp b/source/libs/config/test/cfgTest.cpp index 8610fa3643..62ed5764ac 100644 --- a/source/libs/config/test/cfgTest.cpp +++ b/source/libs/config/test/cfgTest.cpp @@ -102,10 +102,10 @@ TEST_F(CfgTest, 02_Basic) { printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal); break; case CFG_DTYPE_IPSTR: - printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->ipstrVal); + printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal); break; case CFG_DTYPE_DIR: - printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->dirVal); + printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal); break; default: printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype); @@ -172,14 +172,14 @@ TEST_F(CfgTest, 02_Basic) { EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); EXPECT_EQ(pItem->dtype, CFG_DTYPE_IPSTR); EXPECT_STREQ(pItem->name, "test_ipstr"); - EXPECT_STREQ(pItem->ipstrVal, "192.168.0.1"); + EXPECT_STREQ(pItem->strVal, "192.168.0.1"); pItem = cfgGetItem(pConfig, "test_dir"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); EXPECT_EQ(pItem->dtype, CFG_DTYPE_DIR); EXPECT_STREQ(pItem->name, "test_dir"); - EXPECT_STREQ(pItem->dirVal, "/tmp"); + EXPECT_STREQ(pItem->strVal, "/tmp"); cfgCleanup(pConfig); } -- GitLab