benhoyt / inih

Simple .INI file parser in C, good for embedded systems
Other
2.46k stars 490 forks source link

"ini_parse_file" to support FatFs #39

Closed sagonzal closed 8 years ago

sagonzal commented 9 years ago

First my gratitude for you efficient and brilliant work.

This is the results of your library, ported to an ATMEL SAM4S:

Start SD card install                                                      
SD/MMC card ready                           
Mount disk (f_mount)...                         
[OK]
Create a file (f_open)...
[OK]
Write to test file (f_puts)...
[OK]
Test is successful.

Read test ini file...

Config loaded from 'Setup.ini': version=6, name=Bob Smith,
 email=bob@smith.com

Please unplug the card.

I did some, very small modifications to the function "ini_parse_file" to add support to FatFs (FAT file system module include file R0.09) in my ATMEL SAM embedded project, as you can see in the attached image:

inih ini not invented here 20151014

Plus, the addition of 'ff.h' include in the top of the file. If you can I would love to help in your project but I have no idea how to add a fork in this repository. I can, of course, upload the modified file. You tell me.

Warmest regards and again, a big THANK YOU!.

benhoyt commented 9 years ago

Hi @sagonzal, I recently (in r32), added ini_parse_stream() to allow this kind of custom I/O. ini_parse_stream() takes an ini_reader function, which is basically the version of fgets. In fact, it looks like FatFs's f_gets() has an identical signature to fgets(), which makes it very simple.

Just define your own ini_parse_fatfs() function and use ini_parse_stream(), something like so (untested code):

int ini_parse_fatfs(const char* filename, ini_handler handler, void* user)
{
    FRESULT result;
    FIL file;
    int error;

    result = f_open(&file, filename, FA_OPEN_EXISTING | FA_READ);
    if (result != FR_OK)
        return -1;
    error = ini_parse_stream((ini_reader)f_gets, &file, handler, user);
    f_close(&file);
    return error;
}

If the signature of the reader function differs from the standard fgets, you have to wrap it in another function, but in this case I think you're fine.

sagonzal commented 9 years ago

Thank you Ben, incredible fast response!. You are absolutely right and your way does have to mess around with the code.

benhoyt commented 9 years ago

Great! If you create a stand-alone example that show-cases this, I'd be happy to include in the examples directory (examples/ini_fatfs.c or something). Please follow the coding style shown in examples/ini_buffer.c and examples/ini_example.c.

sagonzal commented 9 years ago

Sure thing. Stay tuned.

benhoyt commented 8 years ago

Hi @sagonzal, any update on an example that show-cases this? If not, I might close this issue for now.

benhoyt commented 8 years ago

Per above, closing for now. But if @sagonzal gets back with an example, happy to include it in examples/