minIni 是一个用于读取和写入 INI 文件的库。
github :
https://github.com/compuphase/minIni
minIni特点:
-
1、minIni 大约是 950 行代码 (包括注释),是一个真正的 “迷你” INI 文件解析器,非常容易移植到各种嵌入式平台。
-
2、minIni 不需要标准 C/C++ 库中的 文件 I/O 函数,且允许通过宏配置要选择文件 I/O 接口。
-
3、minIni 仅使用 stack ,完全不使用动态内存(malloc)。
-
4、有 C++ binding, 能很好地配合 C++ 使用
为什么使用minIni :
搜索ini文件库会发现有很多,有些还说支持嵌入式系统,但是去看了代码就会发现,是要支持完整的C标准文件读写接口才可以,在使用RTOS和MCU的嵌入式系统根本不能用,要使用还要费很多时间去移植,其文件读写接口都是嵌入到函数实现里面的,移植起来很难受。
通过上述minIni第2点就可知minIni对ini文件IO接口通过宏进行配置,也就是移植,可知可移植性很好。
minIni仓库下有很多针对许多不同文件IO好的头文件,移植可以参考。
minGlue-bk.h
minGlue-ccs.h
minGlue-efsl.h
minGlue-FatFs.h
minGlue-ffs.h
minGlue-lfs.c
minGlue-Linux.h
minGlue-mdd.h
minGlue-stdio.h
minGlue.h
minIni.c
minIni.h
test.c
test.ini
test2.cc
testplain.ini
wxMinIni.h
针对littlefs的移植:
minGlue-lfs.h
/* Glue functions for the minIni library, based on the littlefs
* libraries, see https://github.com/littlefs-project/littlefs
*
* By guangjieMVP, 2022
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*
* (The littlefs libraries are copyright by ARM and licensed at
* its own terms.)
*/
#ifndef _MINGLUE_LFS_H_
#define _MINGLUE_LFS_H_
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
/* You must set FF_USE_STRFUNC to 1 or 2 in the include file ff.h (or tff.h)
* to enable the "string functions" fgets() and fputs().
*/
#include "lfs.h" /* include lfs.h for littlefs */
#include "stdio.h"
#include "string.h"
/* 定义一行文本结束符 */
#define INI_LINETERM "\n"
/* 使用库自带的strnicmp*/
#define PORTABLE_STRNICMP
extern lfs_t lfs;
char *lfs_gets(char *s, int size, lfs_file_t *file);
#define INI_FILETYPE lfs_file_t
#define ini_openread(filename, file) (lfs_file_open(&lfs, (file), (filename), LFS_O_RDONLY | LFS_O_CREAT) == LFS_ERR_OK)
#define ini_openwrite(filename, file) (lfs_file_open(&lfs, (file), (filename), LFS_O_WRONLY | LFS_O_CREAT) == LFS_ERR_OK)
#define ini_close(file) (lfs_file_close(&lfs, file) == LFS_ERR_OK)
#define ini_read(buffer, size, file) (lfs_gets((buffer), (size), (file)))
#define ini_write(buffer, file) (lfs_file_write(&lfs, (file), (buffer), strlen(buffer)))
#define ini_remove(filename) (lfs_remove(&lfs, filename) == LFS_ERR_OK)
#define ini_rename(source, dest) (lfs_rename(&lfs, (source), (dest)) == LFS_ERR_OK)
#define INI_FILEPOS lfs_soff_t
#define ini_tell(file, pos) (*(pos) = lfs_file_tell(&lfs, (file)))
#define ini_seek(file, pos) (lfs_file_seek(&lfs, (file), *(pos), LFS_SEEK_SET) == LFS_ERR_OK)
#endif
minGlue-lfs.c
#include "lfs.h" /* include lfs.h for littlefs */
#include "string.h"
extern lfs_t lfs;
char *lfs_gets(char *s, int size, lfs_file_t *file)
{
char *ptr;
int32_t numBytes;
int32_t pos;
if ((s == 0) || (size <= 0))
{
return(NULL);
}
/* See the current position before reading */
pos = lfs_file_seek(&lfs, file, 0, LFS_SEEK_CUR);
/* Read from the current position */
numBytes = lfs_file_read(&lfs, file, (s), (size - 1));
if (numBytes > 0)
{
/* Find the first/next new line */
ptr = strchr(s, '\n');
if (ptr)
{
ptr++;
*ptr = 0;
numBytes = strlen(s);
}
}
else
{
return(NULL);
}
assert(numBytes <= size);
/* Set the new position for the next read */
lfs_file_seek(&lfs, file, (pos + numBytes), LFS_SEEK_SET);
return(s);
}
文件IO接口littlefs基本都有现成的,但是获取文件一行数据的接口则没有,需要自己实现,针对littlefs的实现如上所示,lfs_gets
函数,一行数据以"\n"
为结束符
注:
使用的时候将原来的minGlue.h备份一下minGlue-bk.h,然后将minGlue-lfs.h改成minGlue.h
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)