Files
pine-ini-reader/entry.c
2023-03-17 11:41:52 +08:00

298 lines
8.1 KiB
C

#include <stdio.h>
#include "pine-ini.h"
void test_PineIni_StringTrim() {
const char *origin;
char buf[100];
// case 1
origin = "123";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
// case 2
origin = "\t 123";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
// case 3
origin = "\t 1z2 3 \t";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
// case 4
origin = "1asd 12edc 3\t";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
// case 5
origin = "\t \t \t \t";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringTrim(buf));
}
void test_PineIni_StringRemoveQuotes() {
const char *origin;
char buf[100];
// case 1
origin = " \" ";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringRemoveQuotes(buf));
// case 1
origin = "\"\" ";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringRemoveQuotes(buf));
// case 1
origin = " \"123\" ";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringRemoveQuotes(buf));
// case 1
origin = " \"1367' ";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringRemoveQuotes(buf));
// case 1
origin = " 'asd' ";
strcpy(buf, origin);
printf("<%s> : <%s>\n", origin, PineIni_StringRemoveQuotes(buf));
}
void test_Ini(const char * iniText) {
PineIniError errorRet;
PineIniFile* iniResult;
int i, j;
printf("------------ Input INI text: ------------\n%s\n------------ INI Result ------------\n", iniText);
iniResult = PineIni_Parse(iniText, &errorRet);
// Error
if (iniResult == NULL) {
printf("!!! ERROR !!!\n");
printf("errorMessage: %s\n", PINE_INI_ERRMSG[errorRet.errorCode]);
printf("errorCode: %d\n", errorRet.errorCode);
printf("lineNumber: %d\n", errorRet.lineNumber);
printf("line: \"%s\"\n", errorRet.lineContent);
return;
}
printf("Total sections = %d\n", iniResult->numSection);
// Print all sections in iniResult
for (i = 0; i < iniResult->numSection; ++i) {
PineIniSection* section = iniResult->sections[i];
printf("<%s> %d Parameters\n", section->name, section->numParam);
// Print all parameters in section
for (j = 0; j < section->numParam; ++j) {
PineIniParameter* param = section->params[j];
printf(" %s: %s\n", param->key, param->value);
}
}
PineIni_Destory(iniResult);
}
void test_Ini_Success() {
const char * iniText =
"hello=1\r\n"
"; General setting\r\n"
"[General]\r\n"
"sLanguage=ENGLISH\r\n"
"uGridsToLoad=7\r\n"
"uExterior Cell Buffer=64\r\n"
"iPreloadSizeLimit=262144000\r\n"
"\r\n"
"[Display]\r\n"
"fShadowLODMaxStartFade=1000.0\r\n"
"fSpecularLODMaxStartFade=2000.0\r\n"
"fLightLODMaxStartFade=3500.0\r\n"
"iShadowMapResolutionPrimary=4096\r\n"
"bAllowScreenshot=1\r\n"
"fDefaultWorldFOV=80\r\n"
"fDefault1stPersonFOV=80.0000\r\n"
"\r\n"
"[Audio]\r\n"
"fMusicDuckingSeconds=6.0\r\n"
"fMusicUnDuckingSeconds=8.0\r\n"
"fMenuModeFadeOutTime=3.0\r\n"
"fMenuModeFadeInTime=1.0\r\n"
"\r\n"
"; Override previous general setting\r\n"
"[General]\r\n"
" sLanguage = \"CHINESE\"";
test_Ini(iniText);
}
void test_Ini_IllegalPattern() {
const char * iniText =
"hello=1\r\n"
"; General setting\r\n"
"- [General]\r\n";
test_Ini(iniText);
}
void test_Ini_EmptyKey() {
const char * iniText =
"hello=1\r\n"
"; General setting\r\n"
"=asd\r\n";
test_Ini(iniText);
}
void test_Ini_SectionExceed() {
const char * iniText =
"[Section01]\na=1\n[Section02]\na=1\n[Section03]\na=1\n[Section04]\na=1\n[Section05]\na=1\n[Section06]\na=1\n"
"[Section07]\na=1\n[Section08]\na=1\n[Section09]\na=1\n[Section10]\na=1\n[Section11]\na=1\n[Section12]\na=1\n"
"[Section13]\na=1\n[Section14]\na=1\n[Section15]\na=1\n[Section16]\na=1\n[Section17]\na=1\n[Section18]\na=1\n"
"[Section19]\na=1\n[Section20]\na=1\n[Section21]\na=1\n[Section22]\na=1\n[Section23]\na=1\n[Section24]\na=1\n"
"[Section25]\na=1\n";
test_Ini(iniText);
}
void test_Ini_Param_Exceed() {
const char* iniText =
"b0=0\n"
"[variables]\n"
"v01=01\nv02=02\nv03=03\nv04=04\nv05=05\nv06=06\n"
"v07=07\nv08=08\nv09=09\nv10=10\nv11=11\nv12=12\n"
"v13=13\nv14=14\nv15=15\nv16=16\nv17=17\nv18=18\n"
"v19=19\nv20=20\nv21=21\nv22=22\nv23=23\nv24=24\n"
"v25=25\nv26=26\n";
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_Section_GetString(section, 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_Section_GetString(section, 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) {
int padding = 6;
int length = strlen(title) + padding * 2;
int i;
for (i = 0; i < length; ++i) {
putchar('-');
}
putchar('\n');
for (i = 0; i < padding; ++i) {
putchar(' ');
}
printf("%s\n", title);
for (i = 0; i < length; ++i) {
putchar('-');
}
putchar('\n');
}
int main(int argc, char* argv) {
PutsTitle("Test: StringTrim");
test_PineIni_StringTrim();
PutsTitle("Test: StringRemoveQuotes");
test_PineIni_StringRemoveQuotes();
PutsTitle("Test: Ini Illegal Pattern");
test_Ini_IllegalPattern();
PutsTitle("Test: Ini Empty Key");
test_Ini_EmptyKey();
PutsTitle("Test: Ini Section Exceed");
test_Ini_SectionExceed();
PutsTitle("Test: Ini Param Exceed");
test_Ini_Param_Exceed();
PutsTitle("Test: Ini Success");
test_Ini_Success();
PutsTitle("Test: Get value from INI");
test_Ini_Get_Value();
PutsTitle("Test completed!");
return 0;
}