inifile-cpp icon indicating copy to clipboard operation
inifile-cpp copied to clipboard

Comments and line separators getting deleted while saving

Open sukesh-ak opened this issue 2 years ago • 5 comments

@Rookfighter Thank you for this useful library.

I noticed that after updating the values and calling save option, all the comments and empty lines in the ini file gets removed. Since most of these files are edited manually by users, retaining comments and white spaces are essential while saving.

Sample ini file

# Send a message to the world
[world]
message=Hello

# Just foo :)
[foo]
f1=123

Sample code

#include <inicpp.h>
int main()
{
    ini::IniFile myIni;
    myIni.load("some/ini/path");

    myIni["world"]["message"] = "Hello world";

    myIni.save("some/ini/path");
    
}

Result after saving the file

[world]
message=Hello world
[foo]
f1=123

Do let me know if there is already an option to avoid them being removed.

sukesh-ak avatar Aug 28 '22 06:08 sukesh-ak

Hi @sukesh-ak,

thanks for using inifile-cpp and sorry for the late response. I totally missed your issue here.

If it still of importance for you:

Currently there is no option for this in inifile-cpp. The library does not preserve the insertion order of sections or value entries. Comments are also not preserved, because this would require to preserve the order of values and sections, so inifile-cpp would know where to place the comment when saving the file. Essentially the library strips off anything which is not relevant for interpreting the file contents. This design decision keeps the parser very small and efficient (being small and easy to use is the main design goal by inifile-cpp).

The reasoning behind this design decision is that inifiles written / modified by humans are kept separate from inifiles which are written / modified by machines / programs. As a result, your program would only read the files, which are modified by humans. Therefore formatting, order of contents and comments will be preserved (e.g. application config files). On the other hand, formatting and comments do not matter for files which are only modified by machines. Only the semantic contents are relevant (e.g. saving the internal state of your program on shutdown).

So, this is a feature which will likely not be supported by inifile-cpp as it contradicts the design goals of this library. There is one exception though: if at some point there are enough people asking for such a feature, I would consider integrating it.

Rookfighter avatar Feb 13 '23 07:02 Rookfighter

@sukesh-ak same problem. This project broke any saved ini file, and it can't be fixed because author does not want his program to save ini configuration structure.

UnrealKaraulov avatar Feb 13 '23 09:02 UnrealKaraulov

Hi @sukesh-ak,

thanks for using inifile-cpp and sorry for the late response. I totally missed your issue here.

If it still of importance for you:

Currently there is no option for this in inifile-cpp. The library does not preserve the insertion order of sections or value entries. Comments are also not preserved, because this would require to preserve the order of values and sections, so inifile-cpp would know where to place the comment when saving the file. Essentially the library strips off anything which is not relevant for interpreting the file contents. This design decision keeps the parser very small and efficient (being small and easy to use is the main design goal by inifile-cpp).

The reasoning behind this design decision is that inifiles written / modified by humans are kept separate from inifiles which are written / modified by machines / programs. As a result, your program would only read the files, which are modified by humans. Therefore formatting, order of contents and comments will be preserved (e.g. application config files). On the other hand, formatting and comments do not matter for files which are only modified by machines. Only the semantic contents are relevant (e.g. saving the internal state of your program on shutdown).

So, this is a feature which will likely not be supported by inifile-cpp as it contradicts the design goals of this library. There is one exception though: if at some point there are enough people asking for such a feature, I would consider integrating it.

Thanks for the response. No worries I did workaround for my use case. If required in future, will either inherit and override load/save portion or write my own library.

sukesh-ak avatar Feb 13 '23 10:02 sukesh-ak

For windows I just use default WINAPI functions :) IniReaderWriter.zip

UnrealKaraulov avatar Feb 13 '23 10:02 UnrealKaraulov

Hi @sukesh-ak, thanks for using inifile-cpp and sorry for the late response. I totally missed your issue here. If it still of importance for you: Currently there is no option for this in inifile-cpp. The library does not preserve the insertion order of sections or value entries. Comments are also not preserved, because this would require to preserve the order of values and sections, so inifile-cpp would know where to place the comment when saving the file. Essentially the library strips off anything which is not relevant for interpreting the file contents. This design decision keeps the parser very small and efficient (being small and easy to use is the main design goal by inifile-cpp). The reasoning behind this design decision is that inifiles written / modified by humans are kept separate from inifiles which are written / modified by machines / programs. As a result, your program would only read the files, which are modified by humans. Therefore formatting, order of contents and comments will be preserved (e.g. application config files). On the other hand, formatting and comments do not matter for files which are only modified by machines. Only the semantic contents are relevant (e.g. saving the internal state of your program on shutdown). So, this is a feature which will likely not be supported by inifile-cpp as it contradicts the design goals of this library. There is one exception though: if at some point there are enough people asking for such a feature, I would consider integrating it.

Thanks for the response. No worries I did workaround for my use case. If required in future, will either inherit and override load/save portion or write my own library.

Alright thanks and sorry again for the late response :+1: Feel free to fork inifile-cpp. If you implement a reusable solution for other users, I can add a reference (in the README.md) to your implementation. So users interested in preserving the original file structure can use your library.

Rookfighter avatar Feb 14 '23 07:02 Rookfighter