Pine INI Reader

A simple INI file reading tool implemented in C89 aimed at teaching

## INI 格式 本解析器实现了 ini 格式的三种 pattern :`COMMENT `注释、`SECTION`段落和`PARAMETER`参数 ### Comment 注释 注释以`;`开头 ```ini ; This is a comment ``` ### Section 段落以`[`与`]`包围 ```ini [Section Name] ``` ### Parameter 参数以`=`分割`key`键与`value`值 ```ini KEY=VALUE ``` ### 等效 JSON ```ini ; 下列的 parameter 没有写明 section, ; 本库的处理方法是:自动创建一个名为 _default 的 section targetFps=10 [mount] root=card0 [file] ext=k3v ``` 上面的 ini 与下面的 json 等效 ```json { "_default": { "targetFps": "10" }, "mount": { "root": "card0" }, "file": { "ext": "k3v" } } ``` ## 使用范例 ```c PineIniError errorRet; PineIniFile* iniResult; int i, j; /* 解析 INI 文本 */ iniResult = PineIni_Parse(iniText, &errorRet); /* 返回NULL,从errorRet中读取错误信息 */ 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; } /* 打印 INI 结果中的 Section 数量 */ printf("Total sections = %d\n", iniResult->numSection); /* 打印 INI 结果中的 Section*/ for (i = 0; i < iniResult->numSection; ++i) { PineIniSection* section = iniResult->sections[i]; /* 打印 Section 的 Parameter 数量 */ printf("<%s> %d Parameters\n", section->name, section->numParam); /* 打印 Section 的所有 Parameter */ for (j = 0; j < section->numParam; ++j) { PineIniParameter* param = section->params[j]; printf(" %s: %s\n", param->key, param->value); } } /* 释放 INI 资源 */ PineIni_Destory(iniResult); ``` ### 输入测试文件 ```ini hello=1 ; General setting [General] sLanguage=ENGLISH uGridsToLoad=7 uExterior Cell Buffer=64 iPreloadSizeLimit=262144000 [Display] fShadowLODMaxStartFade=1000.0 fSpecularLODMaxStartFade=2000.0 fLightLODMaxStartFade=3500.0 iShadowMapResolutionPrimary=4096 bAllowScreenshot=1 fDefaultWorldFOV=80 fDefault1stPersonFOV=80.0000 [Audio] fMusicDuckingSeconds=6.0 fMusicUnDuckingSeconds=8.0 fMenuModeFadeOutTime=3.0 fMenuModeFadeInTime=1.0 ; 覆盖上面 General 的 sLanguage [General] sLanguage = "CHINESE" ``` ### 输出结果 ```text Total sections = 4 <_default> 1 Parameters hello: 1 4 Parameters sLanguage: CHINESE uGridsToLoad: 7 uExterior Cell Buffer: 64 iPreloadSizeLimit: 262144000 7 Parameters fShadowLODMaxStartFade: 1000.0 fSpecularLODMaxStartFade: 2000.0 fLightLODMaxStartFade: 3500.0 iShadowMapResolutionPrimary: 4096 bAllowScreenshot: 1 fDefaultWorldFOV: 80 fDefault1stPersonFOV: 80.0000