mirror of
https://github.com/glest/glest-source.git
synced 2025-08-25 01:05:50 +02:00
- updates to commondata handling. From now on commondata tag specified the commondata folder under a techtree making it easier to share data and portable since you just need to copy the techtree and its contents, this means no sharing data between techtrees (which is good since we don't want such dependencies)
This commit is contained in:
@@ -175,6 +175,7 @@ bool renameFile(string oldFile, string newFile);
|
||||
void removeFolder(const string path);
|
||||
off_t getFileSize(string filename);
|
||||
bool searchAndReplaceTextInFile(string fileName, string findText, string replaceText, bool simulateOnly);
|
||||
void copyFileTo(string fromFileName, string toFileName);
|
||||
|
||||
int getScreenW();
|
||||
int getScreenH();
|
||||
|
@@ -75,7 +75,8 @@ public:
|
||||
void setFloat(const string &key, float value);
|
||||
void setString(const string &key, const string &value);
|
||||
|
||||
static bool applyTagsToValue(string &value);
|
||||
static bool applyTagsToValue(string &value, std::map<string,string> *mapTagReplacementValues=NULL);
|
||||
static std::map<string,string> getTagReplacementValues(std::map<string,string> *mapExtraTagReplacementValues=NULL);
|
||||
|
||||
string getpath() const { return path;}
|
||||
|
||||
|
@@ -15,10 +15,10 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
#include <map>
|
||||
#include "leak_dumper.h"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using namespace std;
|
||||
|
||||
namespace XERCES_CPP_NAMESPACE{
|
||||
class DOMImplementation;
|
||||
@@ -29,7 +29,7 @@ namespace XERCES_CPP_NAMESPACE{
|
||||
|
||||
namespace Shared{ namespace Xml{
|
||||
|
||||
const int strSize= 256;
|
||||
const int strSize= 4096;
|
||||
|
||||
class XmlIo;
|
||||
class XmlTree;
|
||||
@@ -53,7 +53,7 @@ private:
|
||||
public:
|
||||
static XmlIo &getInstance();
|
||||
~XmlIo();
|
||||
XmlNode *load(const string &path);
|
||||
XmlNode *load(const string &path, std::map<string,string> mapTagReplacementValues);
|
||||
void save(const string &path, const XmlNode *node);
|
||||
};
|
||||
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
~XmlTree();
|
||||
|
||||
void init(const string &name);
|
||||
void load(const string &path);
|
||||
void load(const string &path, std::map<string,string> mapTagReplacementValues);
|
||||
void save(const string &path);
|
||||
|
||||
XmlNode *getRootNode() const {return rootNode;}
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
// class XmlNode
|
||||
// =====================================================
|
||||
|
||||
class XmlNode{
|
||||
class XmlNode {
|
||||
private:
|
||||
string name;
|
||||
string text;
|
||||
@@ -96,7 +96,7 @@ private:
|
||||
void operator =(XmlNode&);
|
||||
|
||||
public:
|
||||
XmlNode(XERCES_CPP_NAMESPACE::DOMNode *node);
|
||||
XmlNode(XERCES_CPP_NAMESPACE::DOMNode *node, std::map<string,string> mapTagReplacementValues);
|
||||
XmlNode(const string &name);
|
||||
~XmlNode();
|
||||
|
||||
@@ -115,7 +115,7 @@ public:
|
||||
|
||||
|
||||
XmlNode *addChild(const string &name);
|
||||
XmlAttribute *addAttribute(const string &name, const string &value);
|
||||
XmlAttribute *addAttribute(const string &name, const string &value, std::map<string,string> mapTagReplacementValues);
|
||||
|
||||
XERCES_CPP_NAMESPACE::DOMElement *buildElement(XERCES_CPP_NAMESPACE::DOMDocument *document) const;
|
||||
|
||||
@@ -133,14 +133,15 @@ private:
|
||||
string name;
|
||||
bool skipRestrictionCheck;
|
||||
bool usesCommondata;
|
||||
std::map<string,string> mapTagReplacementValues;
|
||||
|
||||
private:
|
||||
XmlAttribute(XmlAttribute&);
|
||||
void operator =(XmlAttribute&);
|
||||
|
||||
public:
|
||||
XmlAttribute(XERCES_CPP_NAMESPACE::DOMNode *attribute);
|
||||
XmlAttribute(const string &name, const string &value);
|
||||
XmlAttribute(XERCES_CPP_NAMESPACE::DOMNode *attribute, std::map<string,string> mapTagReplacementValues);
|
||||
XmlAttribute(const string &name, const string &value, std::map<string,string> mapTagReplacementValues);
|
||||
|
||||
public:
|
||||
const string getName() const {return name;}
|
||||
|
@@ -1807,16 +1807,18 @@ bool searchAndReplaceTextInFile(string fileName, string findText, string replace
|
||||
|
||||
while(fgets(buffer,MAX_LEN_SINGLE_LINE + 2,fp1)) {
|
||||
buff_ptr = buffer;
|
||||
while ((find_ptr = strstr(buff_ptr,findText.c_str()))) {
|
||||
//printf("Replacing text [%s] with [%s] in file [%s]\n",findText.c_str(),replaceText.c_str(),fileName.c_str());
|
||||
if(findText != "") {
|
||||
while ((find_ptr = strstr(buff_ptr,findText.c_str()))) {
|
||||
//printf("Replacing text [%s] with [%s] in file [%s]\n",findText.c_str(),replaceText.c_str(),fileName.c_str());
|
||||
|
||||
while(buff_ptr < find_ptr) {
|
||||
fputc((int)*buff_ptr++,fp2);
|
||||
while(buff_ptr < find_ptr) {
|
||||
fputc((int)*buff_ptr++,fp2);
|
||||
}
|
||||
fputs(replaceText.c_str(),fp2);
|
||||
|
||||
buff_ptr += find_len;
|
||||
replacedText = true;
|
||||
}
|
||||
fputs(replaceText.c_str(),fp2);
|
||||
|
||||
buff_ptr += find_len;
|
||||
replacedText = true;
|
||||
}
|
||||
fputs(buff_ptr,fp2);
|
||||
}
|
||||
@@ -1835,6 +1837,24 @@ bool searchAndReplaceTextInFile(string fileName, string findText, string replace
|
||||
return replacedText;
|
||||
}
|
||||
|
||||
void copyFileTo(string fromFileName, string toFileName) {
|
||||
const int MAX_LEN_SINGLE_LINE = 4096;
|
||||
char buffer[MAX_LEN_SINGLE_LINE+2];
|
||||
char *buff_ptr, *find_ptr;
|
||||
FILE *fp1, *fp2;
|
||||
|
||||
fp1 = fopen(fromFileName.c_str(),"r");
|
||||
fp2 = fopen(toFileName.c_str(),"w");
|
||||
|
||||
while(fgets(buffer,MAX_LEN_SINGLE_LINE + 2,fp1)) {
|
||||
buff_ptr = buffer;
|
||||
fputs(buff_ptr,fp2);
|
||||
}
|
||||
|
||||
fclose(fp2);
|
||||
fclose(fp1);
|
||||
}
|
||||
|
||||
// =====================================
|
||||
// ModeInfo
|
||||
// =====================================
|
||||
|
@@ -104,8 +104,87 @@ void Properties::load(const string &path, bool clearCurrentProperties) {
|
||||
fileStream.close();
|
||||
}
|
||||
|
||||
bool Properties::applyTagsToValue(string &value) {
|
||||
std::map<string,string> Properties::getTagReplacementValues(std::map<string,string> *mapExtraTagReplacementValues) {
|
||||
std::map<string,string> mapTagReplacementValues;
|
||||
|
||||
//
|
||||
// #1
|
||||
// First add the standard tags
|
||||
//
|
||||
char *homeDir = NULL;
|
||||
#ifdef WIN32
|
||||
homeDir = getenv("USERPROFILE");
|
||||
#else
|
||||
homeDir = getenv("HOME");
|
||||
#endif
|
||||
|
||||
mapTagReplacementValues["~/"] = (homeDir != NULL ? homeDir : "");
|
||||
mapTagReplacementValues["$HOME"] = (homeDir != NULL ? homeDir : "");
|
||||
mapTagReplacementValues["%%HOME%%"] = (homeDir != NULL ? homeDir : "");
|
||||
mapTagReplacementValues["%%USERPROFILE%%"] = (homeDir != NULL ? homeDir : "");
|
||||
mapTagReplacementValues["%%HOMEPATH%%"] = (homeDir != NULL ? homeDir : "");
|
||||
|
||||
// For win32 we allow use of the appdata variable since that is the recommended
|
||||
// place for application data in windows platform
|
||||
#ifdef WIN32
|
||||
TCHAR szPath[MAX_PATH]="";
|
||||
// Get path for each computer, non-user specific and non-roaming data.
|
||||
if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_APPDATA,
|
||||
NULL, 0, szPath))) {
|
||||
string appPath = szPath;
|
||||
mapTagReplacementValues["$APPDATA"] = appPath;
|
||||
mapTagReplacementValues["%%APPDATA%%"] = appPath;
|
||||
}
|
||||
#endif
|
||||
|
||||
char *username = NULL;
|
||||
username = getenv("USERNAME");
|
||||
|
||||
mapTagReplacementValues["$USERNAME"] = (username != NULL ? username : "");
|
||||
mapTagReplacementValues["%%USERNAME%%"] = (username != NULL ? username : "");
|
||||
|
||||
mapTagReplacementValues["$APPLICATIONPATH"] = Properties::applicationPath;
|
||||
mapTagReplacementValues["%%APPLICATIONPATH%%"] = Properties::applicationPath;
|
||||
|
||||
#if defined(CUSTOM_DATA_INSTALL_PATH)
|
||||
mapTagReplacementValues["$APPLICATIONDATAPATH"] = CUSTOM_DATA_INSTALL_PATH;
|
||||
mapTagReplacementValues["%%APPLICATIONDATAPATH%%"] = CUSTOM_DATA_INSTALL_PATH;
|
||||
|
||||
//mapTagReplacementValues["$COMMONDATAPATH", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
//mapTagReplacementValues["%%COMMONDATAPATH%%", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
|
||||
#else
|
||||
mapTagReplacementValues["$APPLICATIONDATAPATH"] = Properties::applicationPath;
|
||||
mapTagReplacementValues["%%APPLICATIONDATAPATH%%"] = Properties::applicationPath;
|
||||
|
||||
//mapTagReplacementValues["$COMMONDATAPATH", Properties::applicationPath + "/commondata/");
|
||||
//mapTagReplacementValues["%%COMMONDATAPATH%%", Properties::applicationPath + "/commondata/");
|
||||
#endif
|
||||
|
||||
//
|
||||
// #2
|
||||
// Next add the extra tags if passed in
|
||||
//
|
||||
if(mapExtraTagReplacementValues != NULL) {
|
||||
for(std::map<string,string>::iterator iterMap = mapExtraTagReplacementValues->begin();
|
||||
iterMap != mapExtraTagReplacementValues->end(); ++iterMap) {
|
||||
mapTagReplacementValues[iterMap->first] = iterMap->second;
|
||||
}
|
||||
}
|
||||
|
||||
return mapTagReplacementValues;
|
||||
}
|
||||
|
||||
bool Properties::applyTagsToValue(string &value, std::map<string,string> *mapTagReplacementValues) {
|
||||
string originalValue = value;
|
||||
|
||||
if(mapTagReplacementValues != NULL) {
|
||||
for(std::map<string,string>::iterator iterMap = mapTagReplacementValues->begin();
|
||||
iterMap != mapTagReplacementValues->end(); ++iterMap) {
|
||||
replaceAll(value, iterMap->first, iterMap->second);
|
||||
}
|
||||
}
|
||||
else {
|
||||
char *homeDir = NULL;
|
||||
#ifdef WIN32
|
||||
homeDir = getenv("USERPROFILE");
|
||||
@@ -144,17 +223,19 @@ bool Properties::applyTagsToValue(string &value) {
|
||||
replaceAll(value, "$APPLICATIONDATAPATH", CUSTOM_DATA_INSTALL_PATH);
|
||||
replaceAll(value, "%%APPLICATIONDATAPATH%%", CUSTOM_DATA_INSTALL_PATH);
|
||||
|
||||
replaceAll(value, "$COMMONDATAPATH", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
replaceAll(value, "%%COMMONDATAPATH%%", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
//replaceAll(value, "$COMMONDATAPATH", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
//replaceAll(value, "%%COMMONDATAPATH%%", string(CUSTOM_DATA_INSTALL_PATH) + "/commondata/");
|
||||
|
||||
#else
|
||||
replaceAll(value, "$APPLICATIONDATAPATH", Properties::applicationPath);
|
||||
replaceAll(value, "%%APPLICATIONDATAPATH%%", Properties::applicationPath);
|
||||
|
||||
replaceAll(value, "$COMMONDATAPATH", Properties::applicationPath + "/commondata/");
|
||||
replaceAll(value, "%%COMMONDATAPATH%%", Properties::applicationPath + "/commondata/");
|
||||
//replaceAll(value, "$COMMONDATAPATH", Properties::applicationPath + "/commondata/");
|
||||
//replaceAll(value, "%%COMMONDATAPATH%%", Properties::applicationPath + "/commondata/");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
//printf("\nBEFORE SUBSTITUTE [%s] AFTER [%s]\n",originalValue.c_str(),value.c_str());
|
||||
return (originalValue != value);
|
||||
}
|
||||
|
@@ -88,7 +88,7 @@ XmlIo::~XmlIo(){
|
||||
XMLPlatformUtils::Terminate();
|
||||
}
|
||||
|
||||
XmlNode *XmlIo::load(const string &path){
|
||||
XmlNode *XmlIo::load(const string &path, std::map<string,string> mapTagReplacementValues) {
|
||||
|
||||
try{
|
||||
ErrorHandler errorHandler;
|
||||
@@ -111,7 +111,7 @@ XmlNode *XmlIo::load(const string &path){
|
||||
throw runtime_error("Can not parse URL: " + path);
|
||||
}
|
||||
|
||||
XmlNode *rootNode= new XmlNode(document->getDocumentElement());
|
||||
XmlNode *rootNode= new XmlNode(document->getDocumentElement(),mapTagReplacementValues);
|
||||
parser->release();
|
||||
return rootNode;
|
||||
}
|
||||
@@ -167,8 +167,8 @@ void XmlTree::init(const string &name){
|
||||
this->rootNode= new XmlNode(name);
|
||||
}
|
||||
|
||||
void XmlTree::load(const string &path){
|
||||
this->rootNode= XmlIo::getInstance().load(path);
|
||||
void XmlTree::load(const string &path, std::map<string,string> mapTagReplacementValues) {
|
||||
this->rootNode= XmlIo::getInstance().load(path, mapTagReplacementValues);
|
||||
}
|
||||
|
||||
void XmlTree::save(const string &path){
|
||||
@@ -183,7 +183,7 @@ XmlTree::~XmlTree(){
|
||||
// class XmlNode
|
||||
// =====================================================
|
||||
|
||||
XmlNode::XmlNode(DOMNode *node) {
|
||||
XmlNode::XmlNode(DOMNode *node, std::map<string,string> mapTagReplacementValues) {
|
||||
if(node == NULL || node->getNodeName() == NULL) {
|
||||
throw runtime_error("XML structure seems to be corrupt!");
|
||||
}
|
||||
@@ -203,7 +203,7 @@ XmlNode::XmlNode(DOMNode *node) {
|
||||
for(unsigned int i = 0; i < node->getChildNodes()->getLength(); ++i) {
|
||||
DOMNode *currentNode= node->getChildNodes()->item(i);
|
||||
if(currentNode != NULL && currentNode->getNodeType()==DOMNode::ELEMENT_NODE){
|
||||
XmlNode *xmlNode= new XmlNode(currentNode);
|
||||
XmlNode *xmlNode= new XmlNode(currentNode, mapTagReplacementValues);
|
||||
children.push_back(xmlNode);
|
||||
}
|
||||
}
|
||||
@@ -215,7 +215,7 @@ XmlNode::XmlNode(DOMNode *node) {
|
||||
for(unsigned int i = 0; i < domAttributes->getLength(); ++i) {
|
||||
DOMNode *currentNode= domAttributes->item(i);
|
||||
if(currentNode->getNodeType() == DOMNode::ATTRIBUTE_NODE) {
|
||||
XmlAttribute *xmlAttribute= new XmlAttribute(domAttributes->item(i));
|
||||
XmlAttribute *xmlAttribute= new XmlAttribute(domAttributes->item(i), mapTagReplacementValues);
|
||||
attributes.push_back(xmlAttribute);
|
||||
}
|
||||
}
|
||||
@@ -321,8 +321,8 @@ XmlNode *XmlNode::addChild(const string &name){
|
||||
return node;
|
||||
}
|
||||
|
||||
XmlAttribute *XmlNode::addAttribute(const string &name, const string &value) {
|
||||
XmlAttribute *attr= new XmlAttribute(name, value);
|
||||
XmlAttribute *XmlNode::addAttribute(const string &name, const string &value, std::map<string,string> mapTagReplacementValues) {
|
||||
XmlAttribute *attr= new XmlAttribute(name, value, mapTagReplacementValues);
|
||||
attributes.push_back(attr);
|
||||
return attr;
|
||||
}
|
||||
@@ -371,27 +371,30 @@ string XmlNode::getTreeString() const {
|
||||
// class XmlAttribute
|
||||
// =====================================================
|
||||
|
||||
XmlAttribute::XmlAttribute(DOMNode *attribute) {
|
||||
skipRestrictionCheck = false;
|
||||
usesCommondata = false;
|
||||
char str[strSize]="";
|
||||
XmlAttribute::XmlAttribute(DOMNode *attribute, std::map<string,string> mapTagReplacementValues) {
|
||||
skipRestrictionCheck = false;
|
||||
usesCommondata = false;
|
||||
this->mapTagReplacementValues = mapTagReplacementValues;
|
||||
char str[strSize] = "";
|
||||
|
||||
XMLString::transcode(attribute->getNodeValue(), str, strSize-1);
|
||||
value= str;
|
||||
usesCommondata = ((value.find("$COMMONDATAPATH") != string::npos) || (value.find("%%COMMONDATAPATH%%") != string::npos));
|
||||
skipRestrictionCheck = Properties::applyTagsToValue(this->value);
|
||||
skipRestrictionCheck = Properties::applyTagsToValue(this->value,&this->mapTagReplacementValues);
|
||||
|
||||
XMLString::transcode(attribute->getNodeName(), str, strSize-1);
|
||||
name= str;
|
||||
}
|
||||
|
||||
XmlAttribute::XmlAttribute(const string &name, const string &value) {
|
||||
skipRestrictionCheck = false;
|
||||
usesCommondata = false;
|
||||
this->name= name;
|
||||
this->value= value;
|
||||
XmlAttribute::XmlAttribute(const string &name, const string &value, std::map<string,string> mapTagReplacementValues) {
|
||||
skipRestrictionCheck = false;
|
||||
usesCommondata = false;
|
||||
this->mapTagReplacementValues = mapTagReplacementValues;
|
||||
this->name = name;
|
||||
this->value = value;
|
||||
|
||||
usesCommondata = ((value.find("$COMMONDATAPATH") != string::npos) || (value.find("%%COMMONDATAPATH%%") != string::npos));
|
||||
skipRestrictionCheck = Properties::applyTagsToValue(this->value);
|
||||
skipRestrictionCheck = Properties::applyTagsToValue(this->value,&this->mapTagReplacementValues);
|
||||
}
|
||||
|
||||
bool XmlAttribute::getBoolValue() const {
|
||||
|
Reference in New Issue
Block a user