00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022
00023
00024 #ifndef ALPHA_LINUX_CXX
00025 # include <cstdio>
00026 #endif
00027 #include "Configuration.h"
00028 #include <iostream>
00029 #include <string>
00030 #include <cassert>
00031 #include "common_types.h"
00032
00033 using std::string;
00034
00035 const std::string c_empty_string;
00036
00037 using std::cout;
00038 using std::cerr;
00039 using std::endl;
00040
00041 Configuration *config;
00042
00043 void dump_stringvec(std::vector<std::string> &vs,int expect=-2)
00044 {
00045 size_t n;
00046 cout << "vs is " << vs.size() << " entries" << endl;
00047 for(n=0;n<vs.size();n++)
00048 cout << n << " : " << vs[n] << endl;
00049 if(expect==-2)
00050 return;
00051 assert(static_cast<int>(vs.size())==expect);
00052 }
00053
00054 void test1(void)
00055 {
00056 cerr << "Reading config file: ./config.xml" << endl;
00057
00058 config->read_config_file("./config.xml");
00059
00060 cerr << "Config file read." << endl;
00061
00062 int n;
00063 std::string r;
00064
00065 cerr << "Dumping config to stdout." << endl;
00066
00067 config->dump(cout, "\t");
00068 cout << endl;
00069
00070 cerr << "Testing: config/audio/midi/device" << endl;
00071
00072 std::string test_device("config/audio/midi/device");
00073 config->value(test_device, n, -1);
00074 cout << "Returned from reference, \"" << test_device << "\". Got '" << n << "'" << endl;
00075 assert(n==5);
00076
00077 std::string test_enabled("config/audio/midi/enabled");
00078 config->value(test_enabled, r, "--nil--");
00079 cout << "Returned from reference, \"" << test_enabled << "\". Got '" << r << "'" << endl;
00080 assert(r=="yes");
00081
00082 std::string test_spaces("config/disk/u7path_with_spaces");
00083 config->value(test_spaces, r, "--nil--");
00084 cout << "Returned from reference, \"" << test_spaces << "\". Got '" << r << "'" << endl;
00085 assert(r=="d:\\ultima series\\ultima vii - the serpent isle");
00086
00087 config->set("config/something/something/else", "wibble", false);
00088
00089 std::string out=config->dump();
00090 cout << out << endl;
00091
00092 std::vector<std::string> vs;
00093
00094 vs=config->listkeys("config");
00095 dump_stringvec(vs,6);
00096
00097 vs=config->listkeys("config/audio");
00098 dump_stringvec(vs,4);
00099
00100 vs=config->listkeys("config/something",false);
00101 dump_stringvec(vs,1);
00102
00103 vs=config->listkeys("config/somenonexistantthing");
00104 dump_stringvec(vs,0);
00105
00106 config->clear();
00107
00108 assert(config->dump()=="<config>\n</config>\n");
00109
00110 Configuration config_slash(string(""), string("root"));
00111
00112 assert(config_slash.dump()=="<root>\n</root>\n");
00113
00114 config->clear("foo");
00115 assert(config->dump()=="<foo>\n</foo>\n");
00116
00117 Configuration confnew("./config.xml", "config");
00118
00119 Configuration::KeyTypeList ktl;
00120 string basekey("config/audio");
00121 confnew.getsubkeys(ktl, basekey);
00122
00123 cout << endl;
00124
00125 for(Configuration::KeyTypeList::iterator i=ktl.begin(); i!=ktl.end(); i++)
00126 cout << "Key:\t" << i->first << endl << "Value:\t" << i->second << endl;
00127 assert(ktl.size()==7);
00128
00129 }
00130
00131 void test2(void)
00132 {
00133 config->read_config_file("exult.cfg");
00134 config->dump(cout, "\t") << endl;
00135 }
00136
00137 int main(int argc, char *argv[])
00138 {
00139 config = new Configuration();
00140
00141 cerr << "Starting tests..." << endl;
00142 test1();
00143 if(argc>1)
00144 if(strcmp(argv[1], "--exult-cfg")==0)
00145 test2();
00146
00147 return 0;
00148 }