- fixed configurator to properly read / write mg ini files

This commit is contained in:
Mark Vejvoda 2011-04-17 07:04:38 +00:00
parent 734db6c7d4
commit 8dbef7d7ea
5 changed files with 60 additions and 33 deletions

View File

@ -1,8 +1,9 @@
<?xml version="1.0" standalone="yes"?>
<configuration>
<title value="Mega Glest"/>
<file-name value="glest.ini"/>
<icon value="true" path="megaglest.ico"/>
<file-name-main value="glest.ini"/>
<file-name value="$HOME/.megaglest/glestuser.ini"/>
<icon value="true" path="glest.ico"/>
<field-groups>
<field-group name="General">
<field type="Int">
@ -62,16 +63,6 @@
<min value="0"/>
<max value="20"/>
</field>
<field type="Enum">
<name value="Fog of war enabled"/>
<variable-name value="FogOfWar"/>
<description value=""/>
<default value="true"/>
<enums>
<enum value="true"/>
<enum value="false"/>
</enums>
</field>
</field-group>
<field-group name="Renderer">

View File

@ -4,12 +4,10 @@
#include "xml_parser.h"
#include "util.h"
#include "properties.h"
#include "conversion.h"
using namespace std;
using namespace Shared::Xml;
using namespace Shared::Util;
namespace Configurator{
@ -30,10 +28,16 @@ void Configuration::load(const string &path){
throw runtime_error("Cannot find file: " + path);
}
loadStructure(path);
if(fileExists(fileName) == false) {
throw runtime_error("Cannot find file: " + fileName);
if(fileExists(fileNameMain) == false) {
throw runtime_error("Cannot find file: " + fileNameMain);
}
loadValues(fileNameMain);
if(fileExists(fileName) == true) {
// throw runtime_error("Cannot find file: " + fileName);
loadValues(fileName,false);
}
loadValues(fileName);
}
void Configuration::loadStructure(const string &path){
@ -46,14 +50,20 @@ void Configuration::loadStructure(const string &path){
//title
title= configurationNode->getChild("title")->getAttribute("value")->getValue();
//fileNameMain
fileNameMain= configurationNode->getChild("file-name-main")->getAttribute("value")->getValue();
Properties::applyTagsToValue(fileNameMain);
//fileName
fileName= configurationNode->getChild("file-name")->getAttribute("value")->getValue();
Properties::applyTagsToValue(fileName);
//icon
XmlNode *iconNode= configurationNode->getChild("icon");
icon= iconNode->getAttribute("value")->getBoolValue();
if(icon){
iconPath= iconNode->getAttribute("path")->getValue();
Properties::applyTagsToValue(iconPath);
}
const XmlNode *fieldGroupsNode= configurationNode->getChild("field-groups");
@ -68,10 +78,11 @@ void Configuration::loadStructure(const string &path){
}
}
void Configuration::loadValues(const string &path){
Properties properties;
void Configuration::loadValues(const string &path,bool clearCurrentProperties) {
//printf("\nLoading [%s] clearCurrentProperties = %d\n",path.c_str(),clearCurrentProperties);
properties.load(path);
//Properties properties;
properties.load(path,clearCurrentProperties);
for(unsigned int i=0; i<fieldGroups.size(); ++i){
FieldGroup *fg= fieldGroups[i];
@ -82,10 +93,13 @@ void Configuration::loadValues(const string &path){
}
}
void Configuration::save(){
Properties properties;
void Configuration::save() {
//Properties properties;
properties.load(fileName);
//properties.load(fileNameMain);
//if(fileExists(fileName) == true) {
// properties.load(fileName,false);
//}
for(unsigned int i=0; i<fieldGroups.size(); ++i){
FieldGroup *fg= fieldGroups[i];

View File

@ -3,15 +3,15 @@
#include <string>
#include <vector>
#include <wx/wx.h>
#include "properties.h"
#include "xml_parser.h"
using std::string;
using std::vector;
using Shared::Xml::XmlNode;
using namespace Shared::Util;
namespace Configurator{
@ -32,10 +32,12 @@ public:
private:
string title;
string fileNameMain;
string fileName;
bool icon;
string iconPath;
FieldGroups fieldGroups;
Properties properties;
public:
~Configuration();
@ -43,7 +45,7 @@ public:
void load(const string &path);
void loadStructure(const string &path);
void loadValues(const string &path);
void loadValues(const string &path,bool clearCurrentProperties=true);
void save();

View File

@ -30,7 +30,7 @@ namespace Shared{ namespace Util{
/// ini-like file loader
// =====================================================
class Properties{
class Properties {
private:
static const int maxLine= 4096;
@ -45,13 +45,12 @@ private:
string path;
static string applicationPath;
bool applyTagsToValue(string &value);
public:
static void setApplicationPath(string value) { applicationPath=value; }
static string getApplicationPath() { return applicationPath; }
void clear();
void load(const string &path);
void load(const string &path,bool clearCurrentProperties=true);
void save(const string &path);
int getPropertyCount() const {return (int)propertyVector.size();}
@ -76,6 +75,8 @@ public:
void setFloat(const string &key, float value);
void setString(const string &key, const string &value);
static bool applyTagsToValue(string &value);
string getpath() const { return path;}
string toString();

View File

@ -37,7 +37,7 @@ string Properties::applicationPath = "";
// class Properties
// =====================================================
void Properties::load(const string &path){
void Properties::load(const string &path, bool clearCurrentProperties) {
ifstream fileStream;
char lineBuffer[maxLine]="";
@ -52,7 +52,9 @@ void Properties::load(const string &path){
throw runtime_error("Can't open propertyMap file: " + path);
}
propertyMap.clear();
if(clearCurrentProperties == true) {
propertyMap.clear();
}
while(!fileStream.eof()){
fileStream.getline(lineBuffer, maxLine);
lineBuffer[maxLine-1]='\0';
@ -76,8 +78,25 @@ void Properties::load(const string &path){
if(applyTagsToValue(value) == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Property key [%s] now has value [%s]\n",key.c_str(),value.c_str());
}
propertyMap.insert(PropertyPair(key, value));
propertyVector.push_back(PropertyPair(key, value));
bool replaceExisting = false;
if(propertyMap.find(key) != propertyMap.end()) {
replaceExisting = true;
}
propertyMap[key] = value;
if(replaceExisting == false) {
propertyVector.push_back(PropertyPair(key, value));
}
else {
for(unsigned int i = 0; i < propertyVector.size(); ++i) {
PropertyPair &currentPair = propertyVector[i];
if(currentPair.first == key) {
currentPair.second = value;
break;
}
}
}
}
}
}