From 6bcf419d2e8f739b432d4790d1ba9d48ab65365b Mon Sep 17 00:00:00 2001 From: fcolin Date: Fri, 18 Nov 2011 12:14:12 +0000 Subject: --- ARMFCaptureD3D/ConfigFile/example.cpp | 111 ++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 ARMFCaptureD3D/ConfigFile/example.cpp (limited to 'ARMFCaptureD3D/ConfigFile/example.cpp') diff --git a/ARMFCaptureD3D/ConfigFile/example.cpp b/ARMFCaptureD3D/ConfigFile/example.cpp new file mode 100644 index 0000000..e0d24c0 --- /dev/null +++ b/ARMFCaptureD3D/ConfigFile/example.cpp @@ -0,0 +1,111 @@ +// example.cpp +// Program to demonstrate ConfigFile class + +#include +#include +#include "ConfigFile.h" +#include "Triplet.h" + +using std::string; +using std::cout; +using std::endl; + +int main( void ) +{ + // A configuration file can be loaded with a simple + + ConfigFile config( "example.inp" ); + + // Values can be read from the file by name + + int apples; + config.readInto( apples, "apples" ); + cout << "The number of apples is " << apples << endl; + + double price; + config.readInto( price, "price" ); + cout << "The price is $" << price << endl; + + string title; + config.readInto( title, "title" ); + cout << "The title of the song is " << title << endl; + + // We can provide default values in case the name is not found + + int oranges; + config.readInto( oranges, "oranges", 0 ); + cout << "The number of oranges is " << oranges << endl; + + int fruit = 0; + fruit += config.read( "apples", 0 ); + fruit += config.read( "pears", 0 ); + fruit += config.read( "oranges", 0 ); + cout << "The total number of apples, pears, and oranges is "; + cout << fruit << endl; + + // Sometimes we must tell the compiler what data type we want to + // read when it's not clear from arguments given to read() + + int pears = config.read( "pears" ); + cout << "The number of pears is " << pears; + cout << ", but you knew that already" << endl; + + // The value is interpreted as the requested data type + + cout << "The weight is "; + cout << config.read("weight"); + cout << " as a string" << endl; + + cout << "The weight is "; + cout << config.read("weight"); + cout << " as a double" << endl; + + cout << "The weight is "; + cout << config.read("weight"); + cout << " as an integer" << endl; + + // When reading boolean values, a wide variety of words are + // recognized, including "true", "yes", and "1" + + if( config.read( "sale", false ) ) + cout << "The fruit is on sale" << endl; + else + cout << "The fruit is full price" << endl; + + // We can also read user-defined types, as long as the input and + // output operators, >> and <<, are defined + + Triplet point; + config.readInto( point, "zone" ); + cout << "The first point in the zone is " << point << endl; + + // The readInto() functions report whether the named value was found + + int pommes = 0; + if( config.readInto( pommes, "pommes" ) ) + cout << "The input file is in French: "; + else if( config.readInto( pommes, "apples" ) ) + cout << "The input file is in English: "; + cout << "The number of pommes (apples) is " << pommes << endl; + + // Named values can be added to a ConfigFile + + config.add( "zucchini", 12 ); + int zucchini = config.read( "zucchini", 0 ); + cout << "The number of zucchini was set to " << zucchini << endl; + + // And values can be removed + + config.remove( "pears" ); + if( config.readInto( pears, "pears" ) ) + cout << "The pears are ready" << endl; + else + cout << "The pears have been eaten" << endl; + + // An entire ConfigFile can written (and restored) + + cout << "Here is the modified configuration file:" << endl; + cout << config; + + return 0; +} -- cgit v1.1