当前位置:网站首页>[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;
}
边栏推荐
- [programming idea] communication interface of data transmission and decoupling design of communication protocol
- Server installation jupyterab and remote login configuration
- pringboot之restfull接口规范注解(二)
- Application circuit and understanding of BAT54C as power supply protection
- 反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
- 回顾ITIL各版本历程,找到企业运维发展的关键点
- 移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
- How many smart bids does Google have?
- Functional translation
- [the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer
猜你喜欢

The new wild prospect of JD instant retailing from the perspective of "hour shopping"

DFS and BFS to solve Treasure Island exploration

Stm32 mpu6050 servo pan tilt support follow

STM32 IIC protocol controls pca9685 steering gear drive board

uniapp 预览功能

What is solid angle

指针链表的实现
![[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip](/img/d1/c4776e3db1b7560331fa569c40831a.jpg)
[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip

What is Google plus large text ads? How to use it?

回顾ITIL各版本历程,找到企业运维发展的关键点
随机推荐
TensorFlow 2. X multi graphics card distributed training
Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
(no plug-in) summary of vim basic shortcut keys
Padavan mounts SMB sharing and compiles ffmpeg
Anti crawling strategy (IP proxy, setting random sleep time, bilbili video information crawling, obtaining real URLs, processing special characters, processing timestamp, and multithreading)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register
Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
3、 Upload fabric photos to SQL server and provide name to display fabric photos
Uniapp preview function
Configuring virtual private network FRR for Huawei equipment
Implementation of pointer linked list
Application circuit and understanding of BAT54C as power supply protection
js获取元素
Restful interface specification annotation of pringboot (2)
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
Pyflink implements custom sourcefunction
Introduction to Google unit testing tools GTEST and gmoke
Installing Oracle with docker for Mac
Simple ranging using Arduino and ultrasonic sensors