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 » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Trouble iterrating over VectorMap<String, String> - general StoreIniFile
Trouble iterrating over VectorMap<String, String> - general StoreIniFile [message #18548] Tue, 07 October 2008 21:22 Go to next message
blueapples is currently offline  blueapples
Messages: 10
Registered: October 2008
Promising Member
I am trying to make a general StoreIniFile function that will take a VectorMap<String, String> and save it's contents as a INI formatted string.

This is what I have so far, which fails on the call to cfg.Get() with a failed assert like "i > 0 && i < size"...

int StoreIniFile(VectorMap<String, String> cfg, const char *filename) {
	int i = 0;
	String cfgstring;
	Vector<String> keys = cfg.GetKeys();
	
	if(cfg.GetCount() > 0) {
		PromptOK(Format("found %i items", cfg.GetCount()));
		for(i = 0; i < cfg.GetCount(); i++)
		{
			PromptOK(Format("Key %i: %s Value: %s", i, keys[i], cfg.Get(keys[i])));
			// cfgstring << keys[i] << "=" << cfg.Get(keys[i]) << "\n";
		}
	}
	//SaveFile(filename, cfgstring);
	return true;
}


The particular map that I am trying to save is loaded like this:

VectorMap<String, String> cfg;
String cfgfile;
cfgfile = ConfigFile();

PromptOK(Format("Config file: %s", cfgfile));

if(FileExists(cfgfile))
{
	//LoadFromFile(cfg, cfgfile);
	cfg = LoadIniFile(cfgfile);
	PromptOK(Format("Loaded testsetting = %s", cfg.Get("testsetting")));
} else {
	// Set default settings
	cfg.Add("testsetting", "test setting value");
}



Any help would be appreciated. I have used C++ in the past but not really extensively. I'm looking for a new development platform and trying to convert some of my projects over to Upp to see if it will be a good solution. So far, it seems like it has everything, but I seem to have some gaps in my C++ knowledge, and struggle with the relatively light documentation for Upp... it still might be the solution for me though as I really like reading library code Wink.
Re: Trouble iterrating over VectorMap<String, String> - general StoreIniFile [message #18549 is a reply to message #18548] Tue, 07 October 2008 21:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
blueapples wrote on Tue, 07 October 2008 15:22

I am trying to make a general StoreIniFile function that will take a VectorMap<String, String> and save it's contents as a INI formatted string.

This is what I have so far, which fails on the call to cfg.Get() with a failed assert like "i > 0 && i < size"...

int StoreIniFile(VectorMap<String, String> cfg, const char *filename) {
	int i = 0;
	String cfgstring;
	Vector<String> keys = cfg.GetKeys();




This would "pick" cfg, effectively destrying its content. (Same applies to the function parameter cfg!) You can use

[code]
const Vector<String>& keys = cfg.GetKeys();
[/quote]

if you need to "see" keys as Vector, anyway, I believe you really do not:

int StoreIniFile(const VectorMap<String, String>& cfg, const char *filename) {
	int i = 0;
	String cfgstring;
	if(cfg.GetCount() > 0) {
		PromptOK(Format("found %i items", cfg.GetCount()));
		for(i = 0; i < cfg.GetCount(); i++)
		{
			PromptOK(Format("Key %i: %s Value: %s", i, cfg.GetKey(i), cfg[i]));
			cfgstring << cfg.GetKey(i) << "=" << cfg[i] << "\n";
		}
	}
	//SaveFile(filename, cfgstring);
	return true;
}


Also notice the change in function signature...

Quote:


I seem to have some gaps in my C++ knowledge,



Actually, you need U++ knowledge here, this is not quite a traditional way of C++.

Mirek
Re: Trouble iterrating over VectorMap<String, String> - general StoreIniFile [message #18550 is a reply to message #18549] Tue, 07 October 2008 23:00 Go to previous messageGo to next message
blueapples is currently offline  blueapples
Messages: 10
Registered: October 2008
Promising Member
Awesome, that worked... I guess... I don't understand why though. Is there a list of U++ specific concepts that I can refer to?
Re: Trouble iterrating over VectorMap<String, String> - general StoreIniFile [message #18551 is a reply to message #18550] Tue, 07 October 2008 23:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
http://www.ultimatepp.org/srcdoc$Core$Caveats$en-us.html
http://www.ultimatepp.org/srcdoc$Core$pick_$en-us.html
http://www.ultimatepp.org/srcdoc$Core$PickTypes$en-us.html
http://www.ultimatepp.org/srcdoc$Core$Moveable$en-us.html
http://www.ultimatepp.org/srcdoc$Core$Tutorial$en-us.html

are the most related to this problem.

Mirek

[Updated on: Tue, 07 October 2008 23:23]

Report message to a moderator

Re: Trouble iterrating over VectorMap<String, String> - general StoreIniFile [message #18555 is a reply to message #18551] Wed, 08 October 2008 03:17 Go to previous message
blueapples is currently offline  blueapples
Messages: 10
Registered: October 2008
Promising Member
Oh! Thank you. I guess I didn't look hard enough, they are sort of right there aren't they. Sorry!
Previous Topic: Collaborative U++ Projects
Next Topic: Drawing raw data to an Image / Draw object?
Goto Forum:
  


Current Time: Fri Apr 19 16:31:49 CEST 2024

Total time taken to generate the page: 0.04253 seconds