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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* Copyright (C) 2000-2001 The Exult Team
*
* 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 Library 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
#include "common_types.h"
#include "Configuration.h"
#include "exceptions.h"
#include "utils.h"
#ifndef ALPHA_LINUX_CXX
# include <cassert>
# include <cstdio>
#endif
#include <iostream>
#include <fstream>
#ifdef HAVE_SSTREAM
#include <sstream>
using std::ostringstream;
#else
#include <strstream>
using std::ends;
using std::ostrstream;
typedef ostrstream ostringstream;
// NOTE: strstreams need to be 'ends' terminated, whilst strstreams don't.
#endif
using std::atoi;
using std::cerr;
using std::endl;
using std::snprintf;
using std::string;
using std::ostream;
// isspace could be a macro
#ifndef isspace
using std::isspace;
#endif
void Configuration::value(const string key,std::string &ret,const char *defaultvalue) const
{
const XMLnode *sub=xmltree->subtree(key);
if(sub)
ret = sub->value();
else
ret = defaultvalue;
}
void Configuration::value(const string key,bool &ret,bool defaultvalue) const
{
const XMLnode *sub=xmltree->subtree(key);
if(sub)
ret = (to_uppercase(sub->value()) == "YES");
else
ret = defaultvalue;
}
void Configuration::value(const string key,int &ret,int defaultvalue) const
{
const XMLnode *sub=xmltree->subtree(key);
if(sub)
ret = atoi(sub->value().c_str());
else
ret = defaultvalue;
}
void Configuration::set(std::string &key,std::string &value,bool write_out)
{
// Break k up into '/' separated elements.
// start advancing walk, one element at a time, creating nodes
// as needed.
// At the end of that, walk is the target node, and we
// can set the value.
// We must also properly encode the value before writing it out.
// Must remember that.
xmltree->xmlassign(key,value);
if(write_out)
write_back();
}
void Configuration::set(const char *key,const char *value,bool write_out)
{
std::string k(key),v(value);
set(k,v,write_out);
}
void Configuration::set(const char *key,const std::string &value,bool write_out)
{
std::string k(key),v(value);
set(k,v,write_out);
}
void Configuration::set(const char *key,int value,bool write_out)
{
std::string k(key),v;
char buf[32];
snprintf(buf,32,"%d",value);
v=buf;
set(k,v,write_out);
}
bool Configuration::read_config_string(const std::string &s)
{
std::string sbuf(s);
std::size_t nn=0;
while(isspace(s[nn])) ++nn;
assert(s[nn]=='<');
++nn;
xmltree->xmlparse(sbuf,nn);
is_file=false;
return true;
}
bool Configuration::read_config_file(const string input_filename, const string root)
{
clear(root);
filename=input_filename;
// Don't frob the filename if it starts with a dot and
// a slash.
// Or if it's not a relative path.
if((filename.find("./")!=0) && (filename[0]!='/'))
{
#if ((defined XWIN) || (defined BEOS))
const char *f1=getenv("HOME");
if(f1)
{
// User has a home directory
filename=f1;
#ifndef BEOS
filename+="/.";
#else
filename+="/config/settings/";
#endif
filename+=input_filename;
}
else
filename=input_filename;
#else
// Probably something to do with deteriming the username
// and generating a filename in their personal setup area.
// For now, just read file from current directory
filename=input_filename;
#endif
}
is_file=true; // set to file, even if file not found
std::ifstream ifile;
try {
U7open(ifile, filename.c_str(), true);
}
catch(exult_exception &e) {
try {
std::string fallback = "/etc/";
fallback += filename;
U7open(ifile, fallback.c_str(),"r");
} catch (...) {
// configuration file not found
return false;
}
}
if(ifile.fail())
return false;
std::string sbuf, line;
// copies the entire contents of the input file into sbuf
std::getline(ifile, line);
while (ifile.good())
{
sbuf += line + "\n";
std::getline(ifile, line);
}
ifile.close();
read_config_string(sbuf);
is_file=true;
return true;
}
std::string Configuration::dump(void)
{
return xmltree->dump();
}
ostream &Configuration::dump(ostream &o, string indentstr)
{
xmltree->dump(o, indentstr);
return o;
}
void Configuration::write_back(void)
{
if(!is_file)
return; // Don't write back if not from a file
std::ofstream ofile;
U7open(ofile, filename.c_str(), true);
if(ofile.fail())
{
std::perror("Failed to write configuration file");
return;
}
ofile << dump() << endl;
ofile.close();
}
std::vector<std::string> Configuration::listkeys(const std::string &key,bool longformat)
{
std::vector<std::string> vs;
const XMLnode *sub=xmltree->subtree(key);
if(sub)
sub->listkeys(key,vs,longformat);
return vs;
}
std::vector<std::string> Configuration::listkeys(const char *key,bool longformat)
{
std::string s(key);
return listkeys(s,longformat);
}
void Configuration::clear(std::string new_root)
{
delete xmltree;
if(new_root.size())
rootname=new_root;
xmltree = new XMLnode(rootname);
}
void Configuration::getsubkeys(KeyTypeList &ktl, const string basekey)
{
xmltree->searchpairs(ktl, basekey, string(), 0);
}
|