Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

http://www.dsource.org/projects/tango/docs/current/tango.io.stream.MapStream.html

The class MapStream is used to load property streams. Or any file/medium that contains lines of text in a name=value format.

It can be used in the following format.

private import tango.io.stream.FileStream;
private import tango.io.stream.MapStream;

void main()
{
	char[][char[]] props;
	(new MapInput!(char)(new FileInput("thepropfile.props"))).load(props);
}

That will load a property file from the current directory of the compiled exe. thepropfile.props.

You can iterate through the lines of that file using a foreach like this.

private import tango.io.Stdout;
private import tango.io.stream.FileStream;
private import tango.io.stream.MapStream;

void main()
{
        char[][char[]] props;
	(new MapInput!(char)(new FileInput("thepropfile.props"))).load(props);
	foreach(key, value; props)
	{
		Stdout.formatln("key = {}. value = {}.", key, value);
	}
}

You can also get a single value using an associative array like so.

private import tango.io.Stdout;
private import tango.io.stream.FileStream;
private import tango.io.stream.MapStream;

void main()
{
        char[][char[]] props;
	(new MapInput!(char)(new FileInput("thepropfile.props"))).load(props);

        char[] value = props["key"]; // This will return the value of 'key', assuming it exists.
}

These files can be used pretty much the same way INI files are, it's very useful for simple human configurations.

-Rayne