stevemarple / IniFile

Arduino library to parse ini files.
GNU Lesser General Public License v2.1
87 stars 45 forks source link

[Feature request] create set-functions #4

Closed slevon closed 10 years ago

slevon commented 10 years ago

Hi, first of all: I like your project very much and I will definitely use it.

I think it would be great to write back some ini values, so one would be able to write changed values to a certain section to the ini file, or even create new entries.

I usually would store settings in the eeprom but as I saw your library I thought it would be great to have all the settings in a single file so one could transfer them to a new Arduino and user-selections will be stored on the SD-Card.

Do you plan to have that feature in yout lib?

Best regards!

stevemarple commented 10 years ago

It's a feature that has been requested on several occasions but I'm not using the IniFile library myself these days so its unlikely I'll add that feature any time soon. Feel free to add it and send me a pull request. I sent the comment below in response to a previous request. You, or anyone else planning to add write support, might find it useful.

One goal of my implementation is to limit the amount of memory used; malloc and new are deliberately not used and I don't think they are required at all for this library. Another goal is that tasks which take a longer duration have been broken down into smaller chunks of work, eg IniFile::getValue(const char* section, const char* key, char* buffer, int len, IniFileState &state). This is because I wanted my WwwServer library, which uses IniFile, to avoid interfering with time-critical code.

I'm not sure that write support can meet the time-critical goal but that doesn't prevent its inclusion. I think the way I would choose to implement write support is to use IniFile::findKey() to find where the desired key is located in the file. I'd then copy everything up to that point to a temporary file, insert a line for the value and new key, skip the current line in the existing file (using IniFile::readline()) and then write out the reminder of the existing file into the temporary file. I'd like to move/rename the temporary file over the existing file but the Arduino SD library doesn't provide this functionality; I'd probably just copy the temporary file over the old one and then delete the temporary one.

I'd implement support for setting non-char values such as the ini.setValue("network", "mac", mac) you requested as wrappers to call ini.setValue(const char* section, const char* key, const char* value)

I wrote the code under a standard linux environment, using a compatibility header file to mimic the SD library. This was far more convenient than doing everything on the Arduino and enabled me to use gdb to debug the code and core dumps. You'll find arduino_compat.h inside the test directory, along with a Makefile which can be used for regression testing.