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 Go to next message](theme/default/images/down.png) |
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 .
|
|
|
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 message Go to previous message](theme/default/images/up.png) ![Go to next message Go to next message](theme/default/images/down.png) |
![](https://www.ultimatepp.org/forums/images/custom_avatars/3.png) |
mirek
Messages: 14003 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
|
|
|
|
|
|
Goto Forum:
Current Time: Sun Sep 01 07:47:34 CEST 2024
Total time taken to generate the page: 0.00788 seconds
|