提交 22cb2ada 编写于 作者: S Shengliang Guan

config

上级 392d3df9
...@@ -46,6 +46,9 @@ typedef enum { ...@@ -46,6 +46,9 @@ typedef enum {
CFG_DTYPE_STRING, CFG_DTYPE_STRING,
CFG_DTYPE_IPSTR, CFG_DTYPE_IPSTR,
CFG_DTYPE_DIR, CFG_DTYPE_DIR,
CFG_DTYPE_LOCALE,
CFG_DTYPE_CHARSET,
CFG_DTYPE_TIMEZONE
} ECfgDataType; } ECfgDataType;
typedef enum { typedef enum {
...@@ -71,8 +74,6 @@ typedef struct SConfigItem { ...@@ -71,8 +74,6 @@ typedef struct SConfigItem {
int64_t int64Val; int64_t int64Val;
float floatVal; float floatVal;
char *strVal; char *strVal;
char *ipstrVal;
char *dirVal;
}; };
union { union {
int64_t minIntVal; int64_t minIntVal;
...@@ -82,7 +83,6 @@ typedef struct SConfigItem { ...@@ -82,7 +83,6 @@ typedef struct SConfigItem {
int64_t maxIntVal; int64_t maxIntVal;
double maxFloatVal; double maxFloatVal;
}; };
} SConfigItem; } SConfigItem;
typedef struct SConfig SConfig; typedef struct SConfig SConfig;
...@@ -110,6 +110,9 @@ int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double ...@@ -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 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 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 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 *cfgStypeStr(ECfgSrcType type);
const char *cfgDtypeStr(ECfgDataType type); const char *cfgDtypeStr(ECfgDataType type);
......
...@@ -196,6 +196,7 @@ void taosSetAllDebugFlag() { ...@@ -196,6 +196,7 @@ void taosSetAllDebugFlag() {
} }
int32_t taosCfgDynamicOptions(char *msg) { int32_t taosCfgDynamicOptions(char *msg) {
#if 0
char *option, *value; char *option, *value;
int32_t olen, vlen; int32_t olen, vlen;
int32_t vint = 0; int32_t vint = 0;
...@@ -265,6 +266,7 @@ int32_t taosCfgDynamicOptions(char *msg) { ...@@ -265,6 +266,7 @@ int32_t taosCfgDynamicOptions(char *msg) {
} }
} }
#endif
return false; return false;
} }
......
...@@ -262,10 +262,6 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name ...@@ -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 (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
if (pItem->dtype == CFG_DTYPE_STRING) { if (pItem->dtype == CFG_DTYPE_STRING) {
free(pItem->strVal); 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); free(pItem->name);
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
...@@ -367,8 +363,8 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ...@@ -367,8 +363,8 @@ int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal,
} }
SConfigItem item = {.dtype = CFG_DTYPE_IPSTR}; SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
item.ipstrVal = strdup(defaultVal); item.strVal = strdup(defaultVal);
if (item.ipstrVal == NULL) { if (item.strVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1; return -1;
} }
...@@ -394,9 +390,9 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { ...@@ -394,9 +390,9 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
return -1; return -1;
} }
tfree(pItem->dirVal); tfree(pItem->strVal);
pItem->dirVal = strdup(fullDir); pItem->strVal = strdup(fullDir);
if (pItem->dirVal == NULL) { if (pItem->strVal == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1; return -1;
} }
...@@ -409,6 +405,67 @@ int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, EC ...@@ -409,6 +405,67 @@ int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, EC
if (cfgCheckAndSetDir(&item, defaultVal) != 0) { if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
return -1; 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); return cfgAddItem(pConfig, &item, name, utype);
} }
...@@ -455,6 +512,12 @@ const char *cfgDtypeStr(ECfgDataType type) { ...@@ -455,6 +512,12 @@ const char *cfgDtypeStr(ECfgDataType type) {
return "ipstr"; return "ipstr";
case CFG_DTYPE_DIR: case CFG_DTYPE_DIR:
return "dir"; return "dir";
case CFG_DTYPE_LOCALE:
return "locale";
case CFG_DTYPE_CHARSET:
return "charset";
case CFG_DTYPE_TIMEZONE:
return "timezone";
default: default:
return "invalid"; return "invalid";
} }
......
...@@ -102,10 +102,10 @@ TEST_F(CfgTest, 02_Basic) { ...@@ -102,10 +102,10 @@ TEST_F(CfgTest, 02_Basic) {
printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal); printf("index:%d, cfg:%s value:%s\n", size, pItem->name, pItem->strVal);
break; break;
case CFG_DTYPE_IPSTR: 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; break;
case CFG_DTYPE_DIR: 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; break;
default: default:
printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype); printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype);
...@@ -172,14 +172,14 @@ TEST_F(CfgTest, 02_Basic) { ...@@ -172,14 +172,14 @@ TEST_F(CfgTest, 02_Basic) {
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_IPSTR); EXPECT_EQ(pItem->dtype, CFG_DTYPE_IPSTR);
EXPECT_STREQ(pItem->name, "test_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"); pItem = cfgGetItem(pConfig, "test_dir");
EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT);
EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE);
EXPECT_EQ(pItem->dtype, CFG_DTYPE_DIR); EXPECT_EQ(pItem->dtype, CFG_DTYPE_DIR);
EXPECT_STREQ(pItem->name, "test_dir"); EXPECT_STREQ(pItem->name, "test_dir");
EXPECT_STREQ(pItem->dirVal, "/tmp"); EXPECT_STREQ(pItem->strVal, "/tmp");
cfgCleanup(pConfig); cfgCleanup(pConfig);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册