当前位置:网站首页>[open source] libinimini: a minimalist ini parsing library for single chip computers
[open source] libinimini: a minimalist ini parsing library for single chip computers
2022-06-13 02:06:00 【lovemengx】
Introduction
Recently, I based on XR872 I'm doing a little work practice , It has configurable functions , Choose to use ini As a configuration file . I investigated the common online ini Parsing library , Almost all involve fopen()/fgets().. as well as malloc().
Note that these open source libraries are only applicable to support complete C The system of language standard library , It doesn't apply to RTOS Or running naked . Because the former is C Standard file manipulation functions of the language , But in the single-chip microcomputer, the simplified version is basically used fatfs Interface , To introduce the use of single-chip computers , This means that the interface library needs to be modified . The latter involves memory management ,ram The occupation of ini The contents of the configuration file , signify ram The use of is uncontrollable , Highly susceptible to external factors , This is right ram Resources are extremely rare for the single-chip computer , It's unacceptable .
In a learning attitude , I designed a very simple ini Profile parsing library (libinimini), It has the following characteristics :
1. Memory space occupation is controllable ,libinimini Only a section of memory space specified by the user is used for parsing and returning results .
2. Don't care about the source of the data ,libinimini Each line of text will be retrieved by calling back the user's interface , It doesn't matter whether the text comes from a file or other communication interface .
3. Easy to use, easy to use , Users only need to implement the callback interface of text data in behavioral units , Then just wait libinimini The analysis result is enough .
Be careful : The interface itself passes the key value as a string , If you need to convert to numbers , The call is similar to atoi() Function conversion of .
Source code warehouse location :
https://github.com/lovemengx/libinimini
https://gitee.com/lovemengx/libinimini
Sample code
sys_config.ini( Part content )

MCU version ([email protected])
/**
******************************************************************************
* @ file main.c
* @ edition V1.0.0
* @ date
* @ Summary Used to illustrate libinimini How to apply in SCM
* @ author lmx
******************************************************************************
* @ Be careful
******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include "fs/fatfs/ff.h"
#include "kernel/os/os.h"
#include "../src/libinimini.h"
#include "common/framework/fs_ctrl.h"
#include "common/framework/platform_init.h"
// from libinimini Callback , Used to return the parsed result to
// Return value : LIB_INIMINI_STOP: Stop parsing LIB_INIMINI_KEEP: Continue to parse
static int inimini_result_cb(libinimini_data_t* data, void* context)
{
printf("section:%-20s keyname:%-30s strval:%-30s\n", data->section, data->keyname, data->strval);
if (strcmp(data->section, "compass_para") == 0 && strcmp(data->keyname, "compass_int") == 0) {
printf("-------------------------------------------------\n");
printf("[%s]\n", data->section);
printf("%s = %s\n", data->keyname, data->strval);
printf("-------------------------------------------------\n");
return LIB_INIMINI_STOP;
}
return LIB_INIMINI_KEEP;
}
// from libinimini Callback , Used to get the original text data of each line
// Return value : 0: No data available >0: Length of string data
static unsigned int inimini_getline_cb(char* buf, unsigned int size, void* context)
{
FIL* fp = (FIL*)context;
if (f_gets(buf, size, fp) == NULL) {
return LIB_INIMINI_STOP;
}
return (unsigned int)strlen(buf);
}
int main(void)
{
FIL file;
static char cache[512] = { 0 };
libinimini_parameter_t para;
platform_init();
if(fs_ctrl_mount(FS_MNT_DEV_TYPE_SDCARD, 0) != FS_MNT_STATUS_MOUNT_OK){
printf("fs mount failed\n");
while(1) OS_Sleep(1);
}
if(f_open(&file, "sys_config.ini", FA_READ | FA_OPEN_EXISTING) != FR_OK){
printf("open file failed\n");
while(1) OS_Sleep(1);
}
memset(¶, 0x00, sizeof(libinimini_parameter_t));
para.contex = &file;
para.result = inimini_result_cb;
para.ops.getline_cb = inimini_getline_cb;
libinimini_foreach(¶, cache, sizeof(cache));
f_close(&file);
printf("libinimini_foreach done...\n");
while(1) OS_Sleep(1);
return 0;
}

Windows/Linux edition
#include <stdio.h>
#include <string.h>
#include "libinimini.h"
// from libinimini Callback , Used to return the parsed result to
// Return value : LIB_INIMINI_STOP: Stop parsing LIB_INIMINI_KEEP: Continue to parse
static int inimini_result_cb(libinimini_data_t* data, void* context)
{
printf("section:%-20s keyname:%-30s strval:%-30s\n", data->section, data->keyname, data->strval);
if (strcmp(data->section, "compass_para") == 0 && strcmp(data->keyname, "compass_int") == 0) {
printf("-------------------------------------------------\n");
printf("[%s]\n", data->section);
printf("%s = %s\n", data->keyname, data->strval);
printf("-------------------------------------------------\n");
return LIB_INIMINI_STOP;
}
return LIB_INIMINI_KEEP;
}
// from libinimini Callback , Used to get the original text data of each line
// Return value : 0: No data available >0: Length of string data
static unsigned int inimini_getline_cb(char* buf, unsigned int size, void* contex)
{
FILE* fp = (FILE*)contex;
if (fgets(buf, size, fp) == NULL) {
return LIB_INIMINI_STOP;
}
return strlen(buf);
}
int main(void)
{
char inimini_cache[1024] = { 0 };
libinimini_parameter_t para;
FILE* fp = fopen("F:/sys_config.ini", "r");
if (NULL == fp) {
printf("open file failed\n");
return 0;
}
memset(¶, 0x00, sizeof(libinimini_parameter_t));
para.contex = fp;
para.result = inimini_result_cb;
para.ops.getline_cb = inimini_getline_cb;
int cnt = libinimini_foreach(¶, inimini_cache, sizeof(inimini_cache));
fclose(fp);
printf("libinimini_foreach done...\n");
return 0;
}
边栏推荐
- Devaxpress Chinese description --tdximageslider (picture rotation control)
- Sensor: MQ-5 gas module measures the gas value (code attached at the bottom)
- STM32 IIC protocol controls pca9685 steering gear drive board
- 6、 Implementation of warehouse out management function
- Compiling minicom-2.7.1 under msys2
- (no plug-in) summary of vim basic shortcut keys
- Simple ranging using Arduino and ultrasonic sensors
- Can't use typedef yet? C language typedef detailed usage summary, a solution to your confusion. (learning note 2 -- typedef setting alias)
- 华为设备配置私网IP路由FRR
- [the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
猜你喜欢

Qt实现思维导图功能(二)

Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path

How does Google's audience work?

Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)

移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
![[work with notes] NDK compiles the open source library ffmpeg](/img/24/ed33e12a07e001fc708e0c023e479c.jpg)
[work with notes] NDK compiles the open source library ffmpeg

Ruixing coffee moves towards "national consumption"

STM32 IIC protocol controls pca9685 steering gear drive board

分享三个关于CMDB的小故事

传感器:MQ-5燃气模块测量燃气值(底部附代码)
随机推荐
Configuring virtual private network FRR for Huawei equipment
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
Application and examples of C language structure
指针链表的实现
反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
Application and routine of C language typedef struct
VI keyboard diagram
Read routing table
Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)
Logging system in chromium
js获取元素
General IP address, account and password of mobile IPv6 optical cat login, and mobile optical cat is in bridging mode
Delphi Google API text to speech MP3 file
Pytoch freeze pre training weights (feature extraction and BN layer)
[unity] problems encountered in packaging webgl project and their solutions
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
华为设备配置IP和虚拟专用网混合FRR
rsync 传输排除目录
传感器:MQ-5燃气模块测量燃气值(底部附代码)
[printf function and scanf function] (learning note 5 -- standard i/o function)