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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/***************************************************************************
rsettings.cpp - description
-------------------
begin : Sun Sep 12 1999
copyright : (C) 1999 by Andreas Mustun
email : andrew@ribbonsoft.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "rsettings.h"
#include "rconfig.h"
#include "rprgdef.h"
#include <stdio.h>
#include <qfile.h>
#include <qregexp.h>
#include <qstring.h>
#include <qtextstream.h>
#include "rfileparser.h"
/**
* \class RSettings
* Class RSettings is a base class for different kind of settings
* (identifying strings and values). RSettings provides a method for reading
* settings from a file if they're stored in this way:<br>
* [section] { $setting = "value" }<br>
* or<br>
* [section] { <setting> {list of values} }<br>
*/
/**
* Constructor for settings stored like:<br>
* [section] { $name="value" $name2="value2" }<br>
* (4 separators)<br>
* Gets parsed into a (name|value) - list
*
* \param sectionName Name of the section where these settings are stored (e.g.: "[variables]").
* \param nameStarter Character at the start of the identifying name.
* \param nameStopper Character at the end of the identifying name.
* \param valueStarter Character at the start of the value.
* \param valueStopper Character at the end of the value.
*/
RSettings::RSettings(QString _sectionName,
QChar _nameStarter, QChar _nameStopper,
QChar _valueStarter, QChar _valueStopper)
{
sectionName = _sectionName;
nameStarter = _nameStarter;
nameStopper = _nameStopper;
valueStarter = _valueStarter;
valueStopper = _valueStopper;
settingList.setAutoDelete(true);
}
/**
* Constructor for settings stored like:<br>
* [section] { <name param="value" param2="value2"> <name2 param="value" param2="value2"> }<br>
* (4 separators)<br>
* Gets parsed into a (name|param="value" param="value") - list
*
* \param sectionName Name of the section where these settings are stored (e.g.: "[variables]").
* \param nameStarter Character at the start of the identifying name.
* \param separator Character between identifying name and value.
* \param valueStopper Character at the end of the value.
*/
RSettings::RSettings(QString _sectionName,
QChar _nameStarter,
QChar _separator,
QChar _valueStopper)
{
sectionName = _sectionName;
nameStarter = _nameStarter;
nameStopper = _separator;
valueStopper = _valueStopper;
valueStarter=0;
settingList.setAutoDelete(true);
}
/**
* Constructor for settings stored like:<br>
* param="value" param2="value2"<br>
* (3 separators)<br>
* Gets parsed into a (param|value) - list
*
* \param nameStopper Character at the start of the identifying name.
* \param valueStarter Character between identifying name and value.
* \param valueStopper Character at the end of the value.
*/
RSettings::RSettings(QChar _nameStopper, QChar _valueStarter,
QChar _valueStopper)
{
sectionName = "";
nameStarter = 0;
nameStopper = _nameStopper;
valueStarter = _valueStarter;
valueStopper = _valueStopper;
settingList.setAutoDelete(true);
}
/**
* Destructor.
*/
RSettings::~RSettings()
{
}
/**
* Clears all settings.
*/
void
RSettings::clear()
{
settingList.clear();
}
/**
* Adds a setting. If a setting with the given name is already defined it
* gets overwritten.
*
* \param name Setting name (unique).
* \param value Value of this setting as a string separated by ';'.
*/
void
RSettings::addSetting(QString _name, QString _value, bool _autoValue)
{
// Remove all settings with the same name:
//
removeSettings(_name.simplifyWhiteSpace());
// Add new setting:
//
RSetting* newSetting = new RSetting(_name.simplifyWhiteSpace(),
_value.simplifyWhiteSpace(),
_autoValue);
settingList.append(newSetting);
}
/**
* Adds an int setting. If a setting with the given name is already defined it
* gets overwritten.
*
* \param name Setting name (unique).
* \param value Value of this setting as an int.
*/
void
RSettings::addSetting(QString _name, int _value, bool _autoValue)
{
QString strVal;
strVal.setNum(_value);
addSetting(_name, strVal, _autoValue);
}
/**
* Adds a float setting. If a setting with the given name is already defined it
* gets overwritten.
*
* \param name Setting name (unique).
* \param value Value of this setting as a float.
*/
void
RSettings::addSetting(QString _name, float _value, bool _autoValue)
{
QString strVal;
strVal.setNum(_value);
addSetting(_name, strVal, _autoValue);
}
/**
* Removes all setting with the given name (should be unique anyway).
*
* \param name Setting name (unique).
* \return true: if a setting was removed.
* false: otherwise.
*/
bool
RSettings::removeSettings(QString name)
{
RSetting* s;
bool ret=false;
for(s=settingList.first(); s!=0;) {
if(s->getName()==name) {
settingList.remove(s);
s=settingList.current();
ret=true;
}
else {
s=settingList.next();
}
}
return ret;
}
/**
* Removes all setting which are not automatically generated.
*/
void
RSettings::removeNonAutoSettings()
{
RSetting* s;
for(s=settingList.first(); s!=0;) {
if(!s->isAutoValue()) {
settingList.remove(s);
s=settingList.current();
}
else {
s=settingList.next();
}
}
}
/**
* Gets the setting value of the given setting.
*
* \param name Setting name (unique).
*/
QString
RSettings::getSetting(QString name)
{
RSetting* s;
for(s=settingList.first(); s!=0; s=settingList.next()) {
if(s->getName()==name) return s->getValue();
}
return "";
}
/**
* Gets the setting value of the given setting as an integer.
*
* \param name Setting name (unique).
* \param ok Pointer to flag, which tells us if the conversion from string to int was successful.
* \param base Base for conversion.
*/
int
RSettings::getSettingInt(QString name, bool* ok, int base)
{
return getSetting(name).toInt(ok, base);
}
/**
* Gets the setting value of the given setting as a double.
*
* \param name Setting name (unique).
* \param ok Pointer to flag, which tells us if the conversion from string to int was successful.
*/
double
RSettings::getSettingDouble(QString name, bool* ok)
{
return getSetting(name).toDouble(ok);
}
/**
* Reads settings from a file. Settings already stored won't get lost except
* they've the same name. Only settings in the section stored in 'sectionName'
* are read.
*
* \param fileName Name of the file to read.
* \return 'true' if we've read some settings successfully.
* 'false' if the file doesn't exist or there was no section [settings].
*/
bool
RSettings::readFromFile(QString fileName)
{
QString s; // Buffer for the whole section
s = RFileParser::getSection(RFileParser::getContents(fileName), sectionName); // The whole section in a string
s = RFileParser::removeComments(s);
if(s.length()==0) {
return false;
}
return readFromString(s);
}
/**
* Reads the settings from a string. The string should NOT contain sections.
*
* \param s String to read from.
*/
bool
RSettings::readFromString(QString s)
{
bool done=false; // Have we finished
QString settingName; // Name of current setting
QString settingValue; // Value of current setting
int i=0; // Current index
while(!done) {
// No name starter given:
//
if(nameStarter==0) {
settingName=RFileParser::getNextStringUntil(s, i, nameStopper);
}
// Name starter given:
//
else {
settingName=RFileParser::getNextStringBetween(s, i, nameStarter, nameStopper);
}
if(!settingName.isEmpty()) {
i+=settingName.length();
// No value starter given -> use nameStopper as separator!
//
if(valueStarter==0) {
settingValue=RFileParser::getNextStringUntil(s, i, valueStopper);
}
// Value starter given:
//
else {
settingValue=RFileParser::getNextStringBetween(s, i, valueStarter, valueStopper);
}
i+=settingValue.length();
addSetting(settingName, settingValue, false);
}
else {
done=true;
}
}
return true;
}
/**
* Replaces variables in the given string by their values.
*
* \param str The original string.
* \return The new string.
*/
QString
RSettings::replace(QString str)
{
if(!str.contains('$')) return str;
RSetting* t;
QString result=str.copy();
int i;
bool found;
// Replace until we have no occurences of variables or recursive counter>16:
//
do {
found=false;
for(t=settingList.first(); t!=0; t=settingList.next()) {
if(result.contains('$')) {
i=0;
do {
i = result.find(QRegExp("\\$" + t->getName() + "[^a-zA-z0-9_]"), i);
if(i!=-1) {
result.replace(i, (int)t->getName().length()+1, t->getValue());
found=true;
}
}while(i!=-1);
}
else goto end;
}
} while(found);
end:
return result;
}
// EOF
|