config
This commit is contained in:
parent
392d3df98f
commit
22cb2ada73
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue