Home » Community » Newbie corner » Ini file
Re: Ini file [message #58110 is a reply to message #58109] |
Wed, 16 February 2022 12:31   |
jjacksonRIAB
Messages: 227 Registered: June 2011
|
Experienced Member |
|
|
KeyValueRef is already part of U++, it's contained in Core/Map.h. I posted the code to it for illustrative purposes but you don't want to recreate it in your program.
If Upp's KeyValueRef is not working it could be because you either haven't included Core/Core.h or you aren't using the Upp namespace.
#include <Core/Core.h>
using namespace Upp;
namespace Config {
INI_BOOL(flag1, false, "This is bool parameter 1")
INI_BOOL(flag2, true, "This is bool parameter 2")
INI_STRING(text, "default text", "Text parameter");
INI_INT(number, 123456, "Number parameter");
INI_INT64(size, 0, "Int64 parameter");
INI_DOUBLE(fp_number, 0.1, "Floating point parameter");
};
extern void ExternalFn();
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
SetIniFile(GetDataFile("test.ini"));
RDUMP(Config::flag1);
RDUMP(Config::flag2);
RDUMP(Config::text);
RDUMP(Config::number);
RDUMP(Config::fp_number);
RLOG(GetIniInfoFormatted());
Config::number = 321;
RDUMP(Config::number);
RLOG(GetIniInfoFormatted());
ExternalFn();
// contains key value pairs for ini file
VectorMap<String, String> kv = GetIniKeys();
// add a new key
kv.Add("testing", "new key");
// replace an existing key's value
kv.GetPut("text") = "non-default text";
// remove an existing key
kv.RemoveKey("number");
FileOut out("test.ini");
// write pairs to disk
for(KeyValueRef pair : ~kv) {
out << Format("%s=%s\n", pair.key, pair.value);
}
}
If you wanted to instantiate KeyValueRef directly instead of receiving it as a return from a function, you'd do it like this:
String foo = "foo";
String bar = "bar";
KeyValueRef<String, String> test = { foo, bar };
You'll have to read up on template classes and template functions to understand how they work.
I'd also direct you to https://www.ultimatepp.org/src$Core$VectorMap_en-us.html
and https://www.ultimatepp.org/srcdoc$Core$Tutorial_en-us.html#C hapter_3 so you can learn about NTL containers.
Once you understand how containers in U++ work then you have the key to understanding many of the features of U++. A basic understanding of Vector and VectorMap along with templates are essential because they are such a common part of U++ functions and classes. Another thing I have discovered is critical to be able to find undocumented features is to just Ctrl+click on known features and look at the code adjacent to that and its headers for undocumented features. GetIniKeys() is not documented by an example program (and should be), but it is discoverable.
Incidentally I would have not named it GetIniKeys() because it's not returning just an array of keys, it's returning an array of keys and values. I would have named it GetIniPairs() but whatever
[Updated on: Wed, 16 February 2022 13:06] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Thu May 01 06:06:40 CEST 2025
Total time taken to generate the page: 0.01050 seconds
|