Refine tabs; Add utils functions; Update document
This commit is contained in:
14
README.md
14
README.md
@@ -224,6 +224,20 @@ test_Ini_Param_Exceed();
|
|||||||
PutsTitle("Test: Ini Success");
|
PutsTitle("Test: Ini Success");
|
||||||
test_Ini_Success();
|
test_Ini_Success();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* INI 测试6:从INI文件获取值
|
||||||
|
* 测试7种情况
|
||||||
|
* 1. 从 ini 文件通过 section.key 获取值,section、key都存在
|
||||||
|
* 2. 从 ini 文件通过 section.key 获取值,key不存在,使用默认值
|
||||||
|
* 3. 从 ini 文件通过 section.key 获取值,section不存在,使用默认值
|
||||||
|
* 4. 从 section 通过 key 获取值,key存在
|
||||||
|
* 5. 从 section 通过 key 获取值,key不存在,使用默认值
|
||||||
|
* 6. 从 ini 文件通过 section.key 获取整数,section、key都存在
|
||||||
|
* 7. 从 ini 文件通过 section.key 获取整数,key不存在,使用默认值
|
||||||
|
*/
|
||||||
|
PutsTitle("Test: Get value from INI");
|
||||||
|
test_Ini_Get_Value();
|
||||||
|
|
||||||
PutsTitle("Test completed!");
|
PutsTitle("Test completed!");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
75
entry.c
75
entry.c
@@ -173,6 +173,78 @@ void test_Ini_Param_Exceed() {
|
|||||||
test_Ini(iniText);
|
test_Ini(iniText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_Ini_Get_Value() {
|
||||||
|
const char* iniText =
|
||||||
|
"vtargetFps=10\r\n"
|
||||||
|
"[mount]\r\n"
|
||||||
|
"root=card0\r\n"
|
||||||
|
"max_read=1024\r\n"
|
||||||
|
"[file]\r\n"
|
||||||
|
"ext=k3v\r\n";
|
||||||
|
|
||||||
|
PineIniError errorRet;
|
||||||
|
PineIniFile* iniResult;
|
||||||
|
PineIniSection* section;
|
||||||
|
char* sectionName;
|
||||||
|
char* key;
|
||||||
|
const char* szValue;
|
||||||
|
int intValue;
|
||||||
|
|
||||||
|
iniResult = PineIni_Parse(iniText, &errorRet);
|
||||||
|
|
||||||
|
// Test: get from ini file
|
||||||
|
// section - exist
|
||||||
|
// key - exist
|
||||||
|
sectionName = "mount";
|
||||||
|
key = "root";
|
||||||
|
szValue = PineIni_GetString(iniResult, sectionName, key, "fls0");
|
||||||
|
printf("Get String from <%s>.<%s> = %s\n", sectionName, key, szValue);
|
||||||
|
|
||||||
|
// Test: get from ini file
|
||||||
|
// section - exist
|
||||||
|
// key - not exist
|
||||||
|
sectionName = "mount";
|
||||||
|
key = "alt_root";
|
||||||
|
szValue = PineIni_GetString(iniResult, sectionName, key, "fls0");
|
||||||
|
printf("Get String from <%s>.<%s> = %s\n", sectionName, key, szValue);
|
||||||
|
|
||||||
|
// Test: get from ini file
|
||||||
|
// section - not exist
|
||||||
|
sectionName = "app-info";
|
||||||
|
key = "author";
|
||||||
|
szValue = PineIni_GetString(iniResult, sectionName, key, "anderain");
|
||||||
|
printf("Get String from <%s>.<%s> = %s\n", sectionName, key, szValue);
|
||||||
|
|
||||||
|
sectionName = "file";
|
||||||
|
section = PineIni_Find(iniResult, sectionName);
|
||||||
|
|
||||||
|
// Test: get from section
|
||||||
|
// key - exist
|
||||||
|
key = "ext";
|
||||||
|
szValue = PineIni_GetString(iniResult, sectionName, key, "k2v");
|
||||||
|
printf("Get String from <%s>.<%s> = %s\n", sectionName, key, szValue);
|
||||||
|
|
||||||
|
// Test: get from section
|
||||||
|
// key - not exist
|
||||||
|
key = "default_play";
|
||||||
|
szValue = PineIni_GetString(iniResult, sectionName, key, "video");
|
||||||
|
printf("Get String from <%s>.<%s> = %s\n", sectionName, key, szValue);
|
||||||
|
|
||||||
|
// Test: get int value
|
||||||
|
sectionName = "mount";
|
||||||
|
key = "max_read";
|
||||||
|
intValue = PineIni_GetInt(iniResult, sectionName, key, 100);
|
||||||
|
printf("Get Integer from <%s>.<%s> = %d\n", sectionName, key, intValue);
|
||||||
|
|
||||||
|
// Test: get int value
|
||||||
|
sectionName = "mount";
|
||||||
|
key = "min_read";
|
||||||
|
intValue = PineIni_GetInt(iniResult, sectionName, key, 1);
|
||||||
|
printf("Get Integer from <%s>.<%s> = %d\n", sectionName, key, intValue);
|
||||||
|
|
||||||
|
PineIni_Destory(iniResult);
|
||||||
|
}
|
||||||
|
|
||||||
void PutsTitle(const char* title) {
|
void PutsTitle(const char* title) {
|
||||||
int padding = 6;
|
int padding = 6;
|
||||||
int length = strlen(title) + padding * 2;
|
int length = strlen(title) + padding * 2;
|
||||||
@@ -217,6 +289,9 @@ int main(int argc, char* argv) {
|
|||||||
PutsTitle("Test: Ini Success");
|
PutsTitle("Test: Ini Success");
|
||||||
test_Ini_Success();
|
test_Ini_Success();
|
||||||
|
|
||||||
|
PutsTitle("Test: Get value from INI");
|
||||||
|
test_Ini_Get_Value();
|
||||||
|
|
||||||
PutsTitle("Test completed!");
|
PutsTitle("Test completed!");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
48
pine-ini.c
48
pine-ini.c
@@ -227,7 +227,7 @@ PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
|||||||
return iniResult;
|
return iniResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
PineIniSection* PineIni_Find(PineIniFile* file, const char* sectionName) {
|
PineIniSection* PineIni_Find(const PineIniFile* file, const char* sectionName) {
|
||||||
if (file) {
|
if (file) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < file->numSection; ++i) {
|
for (i = 0; i < file->numSection; ++i) {
|
||||||
@@ -272,7 +272,7 @@ PineIniSection* PineIni_Section_New(const char* sectionName) {
|
|||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
PineIniParameter* PineIni_Section_Find(PineIniSection* section, const char* key) {
|
PineIniParameter* PineIni_Section_Find(const PineIniSection* section, const char* key) {
|
||||||
if (section) {
|
if (section) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < section->numParam; ++i) {
|
for (i = 0; i < section->numParam; ++i) {
|
||||||
@@ -345,6 +345,50 @@ void PineIni_Parameter_Destory(PineIniParameter* param) {
|
|||||||
|
|
||||||
// Utils functions
|
// Utils functions
|
||||||
|
|
||||||
|
const char* PineIni_Section_GetString(const PineIniSection* section, const char* key, const char* defaultValue) {
|
||||||
|
const PineIniParameter* param = PineIni_Section_Find(section, key);
|
||||||
|
|
||||||
|
// key not found or value is null
|
||||||
|
if (!param || !param->value) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return param->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* PineIni_GetString(const PineIniFile* file, const char* sectionName, const char* key, const char* defaultValue) {
|
||||||
|
const PineIniSection* section = PineIni_Find(file, sectionName);
|
||||||
|
|
||||||
|
// sectionName not found
|
||||||
|
if (!section) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return PineIni_Section_GetString(section, key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int PineIni_Section_GetInt(const PineIniSection* section, const char* key, int defaultValue) {
|
||||||
|
const char* szIntVal = PineIni_Section_GetString(section, key, NULL);
|
||||||
|
|
||||||
|
if (szIntVal == NULL) {
|
||||||
|
return defaultValue;
|
||||||
|
} else {
|
||||||
|
return atol(szIntVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int PineIni_GetInt(const PineIniFile* file, const char* sectionName, const char* key, int defaultValue) {
|
||||||
|
const char* szIntVal = PineIni_GetString(file, sectionName, key, NULL);
|
||||||
|
|
||||||
|
if (szIntVal == NULL) {
|
||||||
|
return defaultValue;
|
||||||
|
} else {
|
||||||
|
return atol(szIntVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String functions
|
||||||
|
|
||||||
int PineIni_FindChar(const char* src, const char find) {
|
int PineIni_FindChar(const char* src, const char find) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; src[i]; ++i) {
|
for (i = 0; src[i]; ++i) {
|
||||||
|
|||||||
13
pine-ini.h
13
pine-ini.h
@@ -76,16 +76,23 @@ PineIniParameter* PineIni_Parameter_Assign (PineIniParameter* param, const
|
|||||||
void PineIni_Parameter_Destory (PineIniParameter* param);
|
void PineIni_Parameter_Destory (PineIniParameter* param);
|
||||||
|
|
||||||
PineIniSection* PineIni_Section_New (const char* sectionName);
|
PineIniSection* PineIni_Section_New (const char* sectionName);
|
||||||
PineIniParameter* PineIni_Section_Find (PineIniSection* section, const char* key);
|
PineIniParameter* PineIni_Section_Find (const PineIniSection* section, const char* key);
|
||||||
PineIniSection* PineIni_Section_Append (PineIniSection* section, const char* key, const char* value);
|
PineIniSection* PineIni_Section_Append (PineIniSection* section, const char* key, const char* value);
|
||||||
void PineIni_Section_Destory (PineIniSection* section);
|
void PineIni_Section_Destory (PineIniSection* section);
|
||||||
|
|
||||||
PineIniFile* PineIni_Parse (const char * iniText, PineIniError* errorRet);
|
PineIniFile* PineIni_Parse (const char * iniText, PineIniError* errorRet);
|
||||||
PineIniSection* PineIni_Find (PineIniFile* file, const char* sectionName);
|
PineIniSection* PineIni_Find (const PineIniFile* file, const char* sectionName);
|
||||||
PineIniFile* PineIni_Append (PineIniFile* file, PineIniSection* section);
|
PineIniFile* PineIni_Append (PineIniFile* file, PineIniSection* section);
|
||||||
void PineIni_Destory (PineIniFile* file);
|
void PineIni_Destory (PineIniFile* file);
|
||||||
|
|
||||||
// Utils functions
|
// Utils function
|
||||||
|
|
||||||
|
const char* PineIni_Section_GetString (const PineIniSection* section, const char* key, const char* defaultValue);
|
||||||
|
const char* PineIni_GetString (const PineIniFile* file, const char* sectionName, const char* key, const char* defaultValue);
|
||||||
|
int PineIni_Section_GetInt (const PineIniSection* section, const char* key, int defaultValue);
|
||||||
|
int PineIni_GetInt (const PineIniFile* file, const char* sectionName, const char* key, int defaultValue);
|
||||||
|
|
||||||
|
// String functions
|
||||||
|
|
||||||
char* PineIni_ToSubstring (char* src, int left, int right);
|
char* PineIni_ToSubstring (char* src, int left, int right);
|
||||||
int PineIni_FindChar (const char* src, const char find);
|
int PineIni_FindChar (const char* src, const char find);
|
||||||
|
|||||||
Reference in New Issue
Block a user