Ok, I know that there is the properties class and MapStream. But why isnt there a class for working with straight clean INI files? And is there an example for using MapStream? Cause I can't really figure it out.
Ok, I know that there is the properties class and MapStream.
FYI, Properties class is being deprecated in favor of MapStream, please use MapStream
[edit] I wrote this before seeing your other response, sorry for the redundancy...
DiscipleRayne wrote:
And is there an example for using MapStream? Cause I can't really figure it out.
Here is a simple one:
properties file:
property.name = value
property.name2 = value2
code to parse a file:
importtango.io.stream.MapStream;importtango.io.stream.FileStream;importtango.io.Stdout;voidmain(){char[][char[]]props;(newMapInput!(char)(newFileInput("mypropfile.props"))).load(props);// show all valuesforeach(key,value;props)Stdout.formatln("{} = {}",key,value);// get a single valueStdout.formatln("the value for property.name is {}",props["property.name"]);}
http://en.wikipedia.org/wiki/INI_file Yes its like that. I'm one of those lucky few that didn't choose ini files over xml but happen to have to read in someones ini that has same named properties in different sections and also re write values to the correct section so the other software still works.
A singleton pattern is exactly what I'm looking for, because I used it a lot in PHP, but I don't know how to create a singleton in D. Would be great to have an example.
I'm not sure about the constructor, as in my implementation I'm reading the configuration in constructor. Do I need to add the static constructor additionally?. I've just pasted the whole class below.
/lars
module lib.util.config_init;
/*******************************************************************************
imports
*******************************************************************************/
private import tango.io.stream.MapStream,
tango.io.stream.FileStream,
tango.io.FilePath;
private import tango.io.Stdout;
/*******************************************************************************
constant definitions
*******************************************************************************/
char[] CONF_FILE_NOT_FOUND = "Configuration file not found.";
char[] PRINT_END_INI_CONFIGURATION = "----- END INI Configuration --------\n\n";
char[] PRINT_INI_CONFIGURATION = "----- INI Configuration --------\nNumber of parameter(s):";
class Config_init
{
private char[] configuration_file = "etc/conf.ini";
static Config_init instance;
private char[][char[]] properties;
static this()
{
instance = new Config_init();
}
/**
* Constructor
*
* Reads the content of the INI file and stores it in the internal array.
*/
this ()
{
this.read();
}
/**
* Get function
*
* Params:
* key = name of the property to get
*
* Returns:
* The value of a property
*/
public char[] get(char[] key)
{
if (key in this.properties) {
return this.properties[key];
}
}
/**
* Set function
*
* Params:
* key = name of the property to set
* value = value of the property
*/
public void set(char[] key, char[] value)
{
this.properties[key] = value;
}
/**
* Reads all configuration parameter from INI file
*
*/
public void read()
{
this.properties = null;
if ((new FilePath)(this.configuration_file).exists) {
(new MapInput!(char)(new FileInput(this.configuration_file))).load(this.properties);
}
else {
Stdout.formatln(CONF_FILE_NOT_FOUND);
}
}
/**
* Writes all configuration parameter to INI file
*/
public void write()
{
auto map = new MapOutput!(char)(new FileOutput(this.configuration_file));
map.append(this.properties);
map.flush();
map.close();
}
/**
* Prints all configuration properties
*
*/
public void print()
{
Stdout.format("{} {}\n\n", PRINT_INI_CONFIGURATION, this.properties.length);
foreach(key, value; this.properties) {
Stdout.formatln("{} = {}", key, value);
}
Stdout.format(PRINT_END_INI_CONFIGURATION);
}
}
// end class