Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys are not saved correctly under root(empty) section, if not added first #1

Closed
brofield opened this issue Sep 28, 2013 · 1 comment

Comments

@brofield
Copy link
Owner

Reported by MatthewRDutton, Jul 26, 2010

What steps will reproduce the problem?

  1. Add a key/value pair under a section with a name (e.g. "Foo")
  2. Add a key/value pair under the empty section (i.e. SectionName == "")
  3. Save the file (I used CSimpleIniA::SaveFile(char *).)
  4. Load the file (I used CSimpleIniA::LoadFile(char *).)
  5. The second key (incorrectly) appears under the named section (e.g. "Foo")

What is the expected output? What do you see instead?
I expect a file like:
RootKey=RootValue
[Foo]
FooKey=FooValue
but I get
[Foo]
FooKey=FooValue
RootKey=RootValue
which causes the RootKey to incorrectly be placed under the Foo section.

What version of the product are you using? On what operating system?
Version 4.13 (2010-04-19). Win XP SP2.

Please provide any additional information below.
Built with VS2008 RTM.

For example:
WritePrivateProfileString("Foo", "FooKey", "FooValue", "Test.ini");
WritePrivateProfileString("", "RootKey", "RootValue", "Test.ini");
produces:
[Foo]
FooKey=FooValue
[]
RootKey=RootValue

but:
CSimpleIniA test;
test.SetValue("Foo", "FooKey", "FooValue");
test.SetValue("", "RootKey", "RootValue");
test.SaveFile("Test.ini");
produces:
[Foo]
FooKey=FooValue
[]
RootKey=RootValue

Even if you don't intend to match WritePrivateProfileString, the current implementation causes empty section to be merged with the previous section.

Example test case:
CSimpleIniA test;
test.SetValue("Foo", "FooKey", "FooValue");
test.SetValue("", "RootKey", "RootValue");
test.SaveFile("Test.ini");
test.Reset();
test.LoadFile("Test.ini");
assert( strcmp(test.GetValue("Foo", "RootKey"), "RootValue") != 0 );
assert( strcmp(test.GetValue("", "RootKey"), "RootValue") == 0 );

Jul 26, 2010 Delete comment
#1 MatthewRDutton

I made a typo in my examples at the end. Corrected version:
<>
but:
CSimpleIniA test;
test.SetValue("Foo", "FooKey", "FooValue");
test.SetValue("", "RootKey", "RootValue");
test.SaveFile("Test.ini");
produces:
[Foo]
FooKey=FooValue

RootKey=RootValue

<>

Jul 28, 2010 Delete comment
Project Member #2 brofield

I haven't tested this but it seems correct that this bug would exist. It is due to the sorting of the data for output according to the order that it was created. This is done so that Save will save the data in the same order as the original file. Obviously this breaks down when data is added directly, and the root section is used.

What is needed is that all items in the root section are sorted before all other sections. I'll get to this bug in time.

Status: Accepted
Owner: brofield

@brofield
Copy link
Owner Author

Fixed in the latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant