Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Little .ini reading request
Little .ini reading request [message #24273] Fri, 08 January 2010 13:49 Go to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
The current Ini file reading util function is very handy, but it is unable to cope with sections that some people put in .Ini files. These are usually deliminated by [SECTION_NAME], but people have nasty habit of just making it up as they go along Smile

I have a requirement to read such a file and wondered whether the following minor change could be committed for me so that I don't have to duplicate code:

Util.cpp:
VectorMap<String, String> LoadIniFile(const char *filename) {
	FileIn in(filename);
	if(!in) return VectorMap<String, String>();
	return LoadIniStream(in);
}

VectorMap<String, String> LoadIniStream(Stream &in) {
	VectorMap<String, String> key;
	int c;
	if((c = in.Get()) < 0) return key;
	for(;;) {
		String k, v;
		for(;;) {
			if(IsAlNum(c) || c == '_')
				k.Cat(c);
			else
				break;
			if((c = in.Get()) < 0) return key;
		}
		for(;;) {
			if(c != '=' && c != ' ') break;
			if((c = in.Get()) < 0) return key;
		}
		for(;;) {
			if(c < ' ') break;
			v.Cat(c);
			if((c = in.Get()) < 0) break;
		}
		if(!k.IsEmpty())
			key.Add(k, v);
		if(k == "LINK") {
			in.Close();
			if(!in.Open(v) || (c = in.Get()) < 0) return key;
		}
		else
			for(;;) {
				if(IsAlNum(c) || c == '_') break;
				if((c = in.Get()) < 0) return key;
			}
	}
}


Util.h:
VectorMap<String, String> LoadIniFile(const char *filename);
VectorMap<String, String> LoadIniStream(Stream &in);

This means that I can just read .Ini file to correct section myself and then call LoadIniStream.

Cheers!

[Updated on: Fri, 08 January 2010 13:51]

Report message to a moderator

Re: Little .ini reading request [message #24284 is a reply to message #24273] Fri, 08 January 2010 18:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mrjt wrote on Fri, 08 January 2010 07:49

The current Ini file reading util function is very handy, but it is unable to cope with sections that some people put in .Ini files. These are usually deliminated by [SECTION_NAME], but people have nasty habit of just making it up as they go along Smile

I have a requirement to read such a file and wondered whether the following minor change could be committed for me so that I don't have to duplicate code:

Util.cpp:
VectorMap<String, String> LoadIniFile(const char *filename) {
	FileIn in(filename);
	if(!in) return VectorMap<String, String>();
	return LoadIniStream(in);
}

VectorMap<String, String> LoadIniStream(Stream &in) {
	VectorMap<String, String> key;
	int c;
	if((c = in.Get()) < 0) return key;
	for(;;) {
		String k, v;
		for(;;) {
			if(IsAlNum(c) || c == '_')
				k.Cat(c);
			else
				break;
			if((c = in.Get()) < 0) return key;
		}
		for(;;) {
			if(c != '=' && c != ' ') break;
			if((c = in.Get()) < 0) return key;
		}
		for(;;) {
			if(c < ' ') break;
			v.Cat(c);
			if((c = in.Get()) < 0) break;
		}
		if(!k.IsEmpty())
			key.Add(k, v);
		if(k == "LINK") {
			in.Close();
			if(!in.Open(v) || (c = in.Get()) < 0) return key;
		}
		else
			for(;;) {
				if(IsAlNum(c) || c == '_') break;
				if((c = in.Get()) < 0) return key;
			}
	}
}


Util.h:
VectorMap<String, String> LoadIniFile(const char *filename);
VectorMap<String, String> LoadIniStream(Stream &in);

This means that I can just read .Ini file to correct section myself and then call LoadIniStream.

Cheers!


Why not.

BTW, what about adding some "real" support. Maybe we can detect sections and create keys like "SECTION.KEY"?

Mirek
Re: Little .ini reading request [message #24309 is a reply to message #24273] Sun, 10 January 2010 14:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
[quote title=mrjt wrote on Fri, 08 January 2010 07:49]The current Ini file reading util function is very handy, but it is unable to cope with sections that some people put in .Ini files. These are usually deliminated by [SECTION_NAME], but people have nasty habit of just making it up as they go along Smile

I have a requirement to read such a file and wondered whether the following minor change could be committed for me so that I don't have to duplicate code:

Util.cpp:
VectorMap<String, String> LoadIniStream(Stream &in) {
....
			if(!in.Open(v) || (c = in.Get()) < 0) }


It is really weird, but Stream does not have Open method....

(That said, the code compiled just fine until now).

Re: Little .ini reading request [message #24335 is a reply to message #24309] Mon, 11 January 2010 12:51 Go to previous message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
luzr wrote on Sun, 10 January 2010 13:35

mrjt wrote on Fri, 08 January 2010 07:49

The current Ini file reading util function is very handy, but it is unable to cope with sections that some people put in .Ini files. These are usually deliminated by [SECTION_NAME], but people have nasty habit of just making it up as they go along Smile

I have a requirement to read such a file and wondered whether the following minor change could be committed for me so that I don't have to duplicate code:

Util.cpp:
VectorMap<String, String> LoadIniStream(Stream &in) {
....
			if(!in.Open(v) || (c = in.Get()) < 0) }



It is really weird, but Stream does not have Open method....

(That said, the code compiled just fine until now).



Sorry, that was my fault. My brain clearly wasn't working very well that day. It isn't even a good solution to the problem!

I see you've fixed it and committed it though. Thanks a lot. I think I'll work on an optionally section compatible version and get back to you.

[Updated on: Mon, 11 January 2010 13:38]

Report message to a moderator

Previous Topic: File/Folder deleting, Recycle Bin and Read only files
Next Topic: Pick problems
Goto Forum:
  


Current Time: Fri Apr 26 14:14:04 CEST 2024

Total time taken to generate the page: 0.02859 seconds