Trying to make an INI Parser - almost working!

I'm having trouble reading group names in my INI parser...

I have this code as part of my class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	std::map<std::string, std::map<std::string, std::string> > data;

	void read(std::string from, bool merge = false)
	{
		strip_r(from); //strips \r from string
		if(!merge){ data.clear(); }
		std::istringstream ss (from, std::istringstream::in|std::istringstream::binary);
		if(ss.good())
		{
			int ch, next;
			std::string group, item, value;
			bool IV = false;
			while((ch = ss.get()) != EOF)
			{
				next = ss.peek();
				if(char(ch) == '\n')
				{
					if(IV)
					{
						data[group][item] = value;
					}
					item = value = "";
					IV = false;
				}
				else if(char(ch) == '=')
				{
					IV = true;
				}
				else
				{
					(IV ? value : item) += char(ch);
				}

				if(char(ch) == '\n' && next != EOF)
				{
					if(char(next) == ';')
					{
						ss.ignore();
						ss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
					}
					else if(char(next) == '[') //I think the error is somewhere here
					{
						group = "";
						ss.ignore(2);
						while((ch = ss.get()) != EOF && char(ch) != ']')
						{
							group += char(ch);
						}
					}
				}
			}
			//
		}
	}
It loads the items and their values just fine but the group names are wrong. Here is the input file/string:
[Flash Policy Server]
Enabled=true
Port=843

[Relay Server]
Channel Listing=true
Port=6121
Welcome Message=lLacewing Server - liblacewing 0.2.3 (Windows/x86)

And this is the output I get when converting it back to a string:
[]
Enabled=true
Port=843

[elay Server]
Channel Listing=true
Port=6121
Welcome Message=lLacewing Server - liblacewing 0.2.3 (Windows/x86)
As you can see, it completely erased the first group's name and cut off the first letter of the second. I've been at this for days and I think I've stared at it too long for the error in my code to be obvious to me any more.

Note: I understand there are already libraries for this, but I need to practice parsing more human-readable formats. This is for learning ;)


EDIT: Another of my C++ friends helped me solve it; the file doesn't start with a new line first of all, so the first group name is blank ofc, and also (although I do not know why) I need to change the ignore(2) to ignore(1).
Last edited on
Topic archived. No new replies allowed.