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!");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
81
entry.c
81
entry.c
@@ -2,11 +2,11 @@
|
|||||||
#include "pine-ini.h"
|
#include "pine-ini.h"
|
||||||
|
|
||||||
void test_PineIni_StringTrim() {
|
void test_PineIni_StringTrim() {
|
||||||
const char *origin;
|
const char *origin;
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
|
||||||
// case 1
|
// case 1
|
||||||
origin = "123";
|
origin = "123";
|
||||||
strcpy(buf, origin);
|
strcpy(buf, origin);
|
||||||
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
|
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ void test_PineIni_StringTrim() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void test_PineIni_StringRemoveQuotes() {
|
void test_PineIni_StringRemoveQuotes() {
|
||||||
const char *origin;
|
const char *origin;
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
|
||||||
// case 1
|
// case 1
|
||||||
@@ -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;
|
||||||
|
|||||||
70
pine-ini.c
70
pine-ini.c
@@ -48,7 +48,7 @@ static void PineIni_Parser_Destory(PineIniParser* parser) {
|
|||||||
static int PineIni_Parser_Gets(PineIniParser* parser, char* buf) {
|
static int PineIni_Parser_Gets(PineIniParser* parser, char* buf) {
|
||||||
char *pBuf = buf;
|
char *pBuf = buf;
|
||||||
|
|
||||||
parser->lineNumber++;
|
parser->lineNumber++;
|
||||||
|
|
||||||
while (*parser->currentPointer != '\n' && *parser->currentPointer != '\0') {
|
while (*parser->currentPointer != '\n' && *parser->currentPointer != '\0') {
|
||||||
*(pBuf++) = *(parser->currentPointer++);
|
*(pBuf++) = *(parser->currentPointer++);
|
||||||
@@ -60,8 +60,8 @@ static int PineIni_Parser_Gets(PineIniParser* parser, char* buf) {
|
|||||||
return PARSER_ACTION_END;
|
return PARSER_ACTION_END;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Skip LF
|
// Skip LF
|
||||||
parser->currentPointer++;
|
parser->currentPointer++;
|
||||||
return PARSER_ACTION_CONTINUE;
|
return PARSER_ACTION_CONTINUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ static PINE_BOOL PineIni_Line_IsParameter(const char* line, const int length) {
|
|||||||
PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
||||||
char lineBuffer[PINE_INI_LINE_MAX_LEN];
|
char lineBuffer[PINE_INI_LINE_MAX_LEN];
|
||||||
int lineLength;
|
int lineLength;
|
||||||
int getsRetCode;
|
int getsRetCode;
|
||||||
PineIniParser* parser;
|
PineIniParser* parser;
|
||||||
PineIniFile* iniResult;
|
PineIniFile* iniResult;
|
||||||
PineIniSection* currentSection;
|
PineIniSection* currentSection;
|
||||||
@@ -136,8 +136,8 @@ PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
|||||||
strcpy(errorRet->lineContent, "");
|
strcpy(errorRet->lineContent, "");
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Get line from parser
|
// Get line from parser
|
||||||
getsRetCode = PineIni_Parser_Gets(parser, lineBuffer);
|
getsRetCode = PineIni_Parser_Gets(parser, lineBuffer);
|
||||||
|
|
||||||
// Trim white spaces
|
// Trim white spaces
|
||||||
PineIni_StringTrim(lineBuffer);
|
PineIni_StringTrim(lineBuffer);
|
||||||
@@ -158,7 +158,7 @@ PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
|||||||
char sectionName[PINE_INI_LINE_MAX_LEN];
|
char sectionName[PINE_INI_LINE_MAX_LEN];
|
||||||
PineIniSection* section;
|
PineIniSection* section;
|
||||||
|
|
||||||
PineIni_Substring(sectionName, lineBuffer, 1, lineLength - 2);
|
PineIni_Substring(sectionName, lineBuffer, 1, lineLength - 2);
|
||||||
|
|
||||||
section = PineIni_Find(iniResult, sectionName);
|
section = PineIni_Find(iniResult, sectionName);
|
||||||
|
|
||||||
@@ -224,10 +224,10 @@ PineIniFile* PineIni_Parse(const char* iniText, PineIniError* errorRet) {
|
|||||||
|
|
||||||
PineIni_Parser_Destory(parser);
|
PineIni_Parser_Destory(parser);
|
||||||
|
|
||||||
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) {
|
||||||
@@ -370,9 +414,9 @@ char* PineIni_Substring(char* dest, const char* src, int left, int right) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* PineIni_ToSubstring (char* src, int left, int right) {
|
char* PineIni_ToSubstring (char* src, int left, int right) {
|
||||||
int length = strlen(src);
|
int length = strlen(src);
|
||||||
char* buf = (char *)malloc(length + 1);
|
char* buf = (char *)malloc(length + 1);
|
||||||
int newLength = 0;
|
int newLength = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = left; i <= right; ++i) {
|
for (i = left; i <= right; ++i) {
|
||||||
|
|||||||
17
pine-ini.h
17
pine-ini.h
@@ -62,8 +62,8 @@ typedef struct tagPineIni {
|
|||||||
extern const char * PINE_INI_ERRMSG[];
|
extern const char * PINE_INI_ERRMSG[];
|
||||||
|
|
||||||
typedef struct tagPineIniError {
|
typedef struct tagPineIniError {
|
||||||
int errorCode;
|
int errorCode;
|
||||||
int lineNumber;
|
int lineNumber;
|
||||||
char lineContent[PINE_INI_LINE_MAX_LEN];
|
char lineContent[PINE_INI_LINE_MAX_LEN];
|
||||||
} PineIniError;
|
} PineIniError;
|
||||||
|
|
||||||
@@ -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