当前位置:网站首页>Migrating minini to littlefs

Migrating minini to littlefs

2022-06-11 10:47:00 *_ Look up at the stars_*

minIni

minIni Is a for reading and writing INI Document library .
github :
https://github.com/compuphase/minIni

minIni characteristic :

  • 1、minIni It's about 950 Line code ( Including comments ), It's a real “ mini ” INI File parser , It is very easy to migrate to various embedded platforms .

  • 2、minIni No standards required C/C++ In the library file I/O function , And it is allowed to configure the file to be selected through the macro I/O Interface .

  • 3、minIni Use only stack , Do not use dynamic memory at all (malloc).

  • 4、 Yes C++ binding, Can cooperate well C++ Use

Why use minIni :

Search for ini The file library will find many , Some also say that they support embedded systems , But when you look at the code, you will find that , Is to support complete C The standard file read / write interface can be used , In the use of RTOS and MCU The embedded system of can't be used at all , It will take a lot of time to transplant , Its file read / write interfaces are embedded in the function implementation , It's hard to transplant .
Through the above minIni The first 2 Just click minIni Yes ini file IO Interfaces are configured through macros , That is, transplantation , The portability is very good .

minIni Migration to littlefs

minIni There are many different files under the warehouse IO Good header file , Migration can refer to .

  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

in the light of littlefs Transplantation :

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 a line of text Terminator  */
#define INI_LINETERM "\n"

/*  Use the library's own 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);
}

file IO Interface littlefs There are basically ready-made , But there is no interface to get one line of file data , You have to implement it yourself , in the light of littlefs The implementation of is as shown above ,lfs_gets function , A row of data is represented by "\n" End of character

notes :

 Use the original minGlue.h Back it up minGlue-bk.h, And then minGlue-lfs.h Change to minGlue.h
原网站

版权声明
本文为[*_ Look up at the stars_*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111015271760.html