00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "Aria.h"
00027
00038 class ConfigExample
00039 {
00040 ArConfig* myConfig;
00041 int myIntParam;
00042 double myDoubleParam;
00043 bool myBoolParam;
00044 char myStringParam[256];
00045 ArRetFunctorC<bool, ConfigExample> myProcessConfigCB;
00046
00047 public:
00048 ConfigExample():
00049 myIntParam(0),
00050 myDoubleParam(0.5),
00051 myBoolParam(false),
00052 myProcessConfigCB(this, &ConfigExample::processConfigFile)
00053 {
00054
00055
00056
00057
00058
00059
00060
00061 ArConfig* config = Aria::getConfig();
00062 config->setSectionComment("Example Section", "Contains parameters created by the configExample");
00063
00064
00065 config->addParam( ArConfigArg("ExampleIntegerParameter", &myIntParam, "Example parameter integer.", -10, 10), "Example Section", ArPriority::NORMAL);
00066
00067
00068 config->addParam( ArConfigArg("ExampleDoubleParameter", &myDoubleParam, "Example double precision floating point number.", 0.0, 1.0), "Example Section", ArPriority::NORMAL);
00069
00070
00071 config->addParam( ArConfigArg("ExampleBoolParameter", &myBoolParam, "Example boolean parameter."), "Example Section", ArPriority::IMPORTANT);
00072
00073
00074 myStringParam[0] = '\0';
00075 config->addParam( ArConfigArg("ExampleStringParameter", myStringParam, "Example string parameter.", 256), "Example Section", ArPriority::TRIVIAL);
00076
00077
00078
00079 config->addProcessFileCB(&myProcessConfigCB, 0);
00080 }
00081
00082
00083
00084
00085 bool processConfigFile()
00086 {
00087 ArLog::log(ArLog::Normal, "configExample: Config changed. New values: int=%d, float=%f, bool=%s, string=\"%s\".", myIntParam, myDoubleParam, myBoolParam?"true":"false", myStringParam);
00088 return true;
00089 }
00090 };
00091
00092 int main(int argc, char **argv)
00093 {
00094 Aria::init();
00095 ArArgumentParser argParser(&argc, argv);
00096 argParser.loadDefaultArguments();
00097 if (argc < 2 || !Aria::parseArgs() || argParser.checkArgument("-help"))
00098 {
00099 ArLog::log(ArLog::Terse, "configExample usage: configExample <config file>.\nFor example, \"configExample examples/configExample.cfg\".");
00100 Aria::logOptions();
00101 Aria::shutdown();
00102 return 1;
00103 }
00104
00105
00106 ConfigExample configExample;
00107
00108
00109
00110
00111
00112 char error[512];
00113 const char* filename = argParser.getArg(1);
00114 ArConfig* config = Aria::getConfig();
00115 ArLog::log(ArLog::Normal, "configExample: loading configuration file \"%s\"...", filename);
00116 if (! config->parseFile(filename, true, false, error, 512) )
00117 {
00118 ArLog::log(ArLog::Terse, "configExample: Error loading configuration file \"%s\" %s. Try \"examples/configExample.cfg\".", filename, error);
00119 Aria::shutdown();
00120 return -1;
00121 }
00122
00123 ArLog::log(ArLog::Normal, "configExample: Loaded configuration file \"%s\".", filename);
00124
00125
00126 ArConfigSection* section = config->findSection("Example Section");
00127 if (section)
00128 {
00129 ArConfigArg* arg = section->findParam("ExampleBoolParameter");
00130 if (arg)
00131 {
00132 arg->setBool(!arg->getBool());
00133 if (! config->callProcessFileCallBacks(false, error, 512) )
00134 {
00135 ArLog::log(ArLog::Terse, "configExample: Error processing modified config: %s.", error);
00136 }
00137 else
00138 {
00139 ArLog::log(ArLog::Normal, "configExample: Successfully modified config and invoked callbacks.");
00140 }
00141 }
00142 }
00143
00144
00145 ArLog::log(ArLog::Normal, "configExample: Saving configuration...");
00146 if(!config->writeFile(filename))
00147 {
00148 ArLog::log(ArLog::Terse, "configExample: Error saving configuration to file \"%s\"!", filename);
00149 }
00150
00151
00152 ArLog::log(ArLog::Normal, "configExample: end of program.");
00153 Aria::shutdown();
00154 return 0;
00155 }