1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
Copyright (C) 2000 Dancer A.L Vesperman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifndef ALPHA_LINUX_CXX
# include <cstdio>
#endif
#include "Configuration.h"
#include <iostream>
#include <string>
#include <cassert>
#include "common_types.h"
using std::string;
const std::string c_empty_string;
using std::cout;
using std::cerr;
using std::endl;
Configuration *config;
void dump_stringvec(std::vector<std::string> &vs,int expect=-2)
{
size_t n;
cout << "vs is " << vs.size() << " entries" << endl;
for(n=0;n<vs.size();n++)
cout << n << " : " << vs[n] << endl;
if(expect==-2)
return;
assert(static_cast<int>(vs.size())==expect);
}
void test1(void)
{
cerr << "Reading config file: ./config.xml" << endl;
config->read_config_file("./config.xml");
cerr << "Config file read." << endl;
int n;
std::string r;
cerr << "Dumping config to stdout." << endl;
config->dump(cout, "\t");
cout << endl;
cerr << "Testing: config/audio/midi/device" << endl;
std::string test_device("config/audio/midi/device");
config->value(test_device, n, -1);
cout << "Returned from reference, \"" << test_device << "\". Got '" << n << "'" << endl;
assert(n==5);
std::string test_enabled("config/audio/midi/enabled");
config->value(test_enabled, r, "--nil--");
cout << "Returned from reference, \"" << test_enabled << "\". Got '" << r << "'" << endl;
assert(r=="yes");
std::string test_spaces("config/disk/u7path_with_spaces");
config->value(test_spaces, r, "--nil--");
cout << "Returned from reference, \"" << test_spaces << "\". Got '" << r << "'" << endl;
assert(r=="d:\\ultima series\\ultima vii - the serpent isle");
config->set("config/something/something/else", "wibble", false);
std::string out=config->dump();
cout << out << endl;
std::vector<std::string> vs;
vs=config->listkeys("config");
dump_stringvec(vs,6);
vs=config->listkeys("config/audio");
dump_stringvec(vs,4);
vs=config->listkeys("config/something",false);
dump_stringvec(vs,1);
vs=config->listkeys("config/somenonexistantthing");
dump_stringvec(vs,0);
config->clear();
//cout << endl << config->dump() << endl;
assert(config->dump()=="<config>\n</config>\n");
Configuration config_slash(string(""), string("root"));
//cout << endl << config_slash.dump() << endl;
assert(config_slash.dump()=="<root>\n</root>\n");
config->clear("foo");
assert(config->dump()=="<foo>\n</foo>\n");
Configuration confnew("./config.xml", "config");
Configuration::KeyTypeList ktl;
string basekey("config/audio");
confnew.getsubkeys(ktl, basekey);
cout << endl;
for(Configuration::KeyTypeList::iterator i=ktl.begin(); i!=ktl.end(); i++)
cout << "Key:\t" << i->first << endl << "Value:\t" << i->second << endl;
assert(ktl.size()==7);
}
void test2(void)
{
config->read_config_file("exult.cfg");
config->dump(cout, "\t") << endl;
}
int main(int argc, char *argv[])
{
config = new Configuration();
cerr << "Starting tests..." << endl;
test1();
if(argc>1)
if(strcmp(argv[1], "--exult-cfg")==0)
test2();
return 0;
}
|