Made changes to properly isolate new unit rotation code so that it is disabled by default

This commit is contained in:
Mark Vejvoda
2010-03-13 21:10:45 +00:00
parent c983eab0af
commit 2012b7e22c
27 changed files with 9174 additions and 58 deletions

View File

@@ -0,0 +1,126 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code 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 "conversion.h"
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include "leak_dumper.h"
using namespace std;
namespace Shared{ namespace Util{
const int strSize = 256;
bool strToBool(const string &s){
if (s=="0" || s=="false"){
return false;
}
if (s=="1" || s=="true"){
return true;
}
throw runtime_error("Error converting string to bool, expected 0 or 1, found: [" + s + "]");
}
int strToInt(const string &s){
char *endChar;
int intValue= strtol(s.c_str(), &endChar, 10);
if(*endChar!='\0'){
throw runtime_error("Error converting from string to int, found: [" + s + "]");
}
return intValue;
}
float strToFloat(const string &s){
char *endChar;
float floatValue= static_cast<float>(strtod(s.c_str(), &endChar));
if(*endChar!='\0'){
throw runtime_error("Error converting from string to float, found: [" + s + "]");
}
return floatValue;
}
bool strToBool(const string &s, bool *b){
if (s=="0" || s=="false"){
*b= false;
return true;
}
if (s=="1" || s=="true"){
*b= true;
return true;
}
return false;
}
bool strToInt(const string &s, int *i){
char *endChar;
*i= strtol(s.c_str(), &endChar, 10);
if(*endChar!='\0'){
return false;
}
return true;
}
bool strToFloat(const string &s, float *f){
char *endChar;
*f= static_cast<float>(strtod(s.c_str(), &endChar));
if(*endChar!='\0'){
return false;
}
return true;
}
string boolToStr(bool b){
if(b){
return "1";
}
else{
return "0";
}
}
string intToStr(int i){
char str[strSize];
sprintf(str, "%d", i);
return str;
}
string intToHex(int i){
char str[strSize];
sprintf(str, "%x", i);
return str;
}
string floatToStr(float f){
char str[strSize];
sprintf(str, "%.2f", f);
return str;
}
string doubleToStr(double d){
char str[strSize];
sprintf(str, "%.2f", d);
return str;
}
}}//end namespace

View File

@@ -0,0 +1,224 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2007 Marti<74>o Figueroa
//
// You can redistribute this code 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 "properties.h"
#include <fstream>
#include <stdexcept>
#include <cstring>
#include "conversion.h"
#include "leak_dumper.h"
using namespace std;
namespace Shared{ namespace Util{
// =====================================================
// class Properties
// =====================================================
void Properties::load(const string &path){
ifstream fileStream;
char lineBuffer[maxLine];
string line, key, value;
int pos;
this->path= path;
fileStream.open(path.c_str(), ios_base::in);
if(fileStream.fail()){
throw runtime_error("Can't open propertyMap file: " + path);
}
propertyMap.clear();
while(!fileStream.eof()){
fileStream.getline(lineBuffer, maxLine);
lineBuffer[maxLine-1]='\0';
//process line if it it not a comment
if(lineBuffer[0]!=';'){
// gracefully handle win32 \r\n line endings
size_t len= strlen(lineBuffer);
if(len > 0 && lineBuffer[len-1] == '\r'){
lineBuffer[len-1]= 0;
}
line= lineBuffer;
pos= line.find('=');
if(pos != string::npos){
key= line.substr(0, pos);
value= line.substr(pos+1);
propertyMap.insert(PropertyPair(key, value));
propertyVector.push_back(PropertyPair(key, value));
}
}
}
fileStream.close();
}
void Properties::save(const string &path){
ofstream fileStream;
fileStream.open(path.c_str(), ios_base::out | ios_base::trunc);
fileStream << "; === propertyMap File === \n";
fileStream << '\n';
for(PropertyMap::iterator pi= propertyMap.begin(); pi!=propertyMap.end(); ++pi){
fileStream << pi->first << '=' << pi->second << '\n';
}
fileStream.close();
}
void Properties::clear(){
propertyMap.clear();
propertyVector.clear();
}
bool Properties::getBool(const string &key, const char *defaultValueIfNotFound) const{
try{
return strToBool(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + key + " in: " + path+"\n[" + e.what() + "]");
}
}
int Properties::getInt(const string &key,const char *defaultValueIfNotFound) const{
try{
return strToInt(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
}
}
int Properties::getInt(const string &key, int min, int max,const char *defaultValueIfNotFound) const{
int i= getInt(key,defaultValueIfNotFound);
if(i<min || i>max){
throw runtime_error("Value out of range: " + key + ", min: " + intToStr(min) + ", max: " + intToStr(max));
}
return i;
}
float Properties::getFloat(const string &key, const char *defaultValueIfNotFound) const{
try{
return strToFloat(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
}
}
float Properties::getFloat(const string &key, float min, float max, const char *defaultValueIfNotFound) const{
float f= getFloat(key,defaultValueIfNotFound);
if(f<min || f>max){
throw runtime_error("Value out of range: " + key + ", min: " + floatToStr(min) + ", max: " + floatToStr(max));
}
return f;
}
const string Properties::getString(const string &key, const char *defaultValueIfNotFound) const{
PropertyMap::const_iterator it;
it= propertyMap.find(key);
if(it==propertyMap.end()){
if(defaultValueIfNotFound != NULL) {
return string(defaultValueIfNotFound);
}
else {
throw runtime_error("Value not found in propertyMap: " + key + ", loaded from: " + path);
}
}
else{
return it->second;
}
}
void Properties::setInt(const string &key, int value){
setString(key, intToStr(value));
}
void Properties::setBool(const string &key, bool value){
setString(key, boolToStr(value));
}
void Properties::setFloat(const string &key, float value){
setString(key, floatToStr(value));
}
void Properties::setString(const string &key, const string &value){
propertyMap.erase(key);
propertyMap.insert(PropertyPair(key, value));
}
string Properties::toString(){
string rStr;
for(PropertyMap::iterator pi= propertyMap.begin(); pi!=propertyMap.end(); pi++)
rStr+= pi->first + "=" + pi->second + "\n";
return rStr;
}
bool Properties::getBool(const char *key, const char *defaultValueIfNotFound) const{
try{
return strToBool(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + string(key) + " in: " + path+"\n[" + e.what() + "]");
}
}
int Properties::getInt(const char *key,const char *defaultValueIfNotFound) const{
try{
return strToInt(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
}
}
float Properties::getFloat(const char *key, const char *defaultValueIfNotFound) const{
try{
return strToFloat(getString(key,defaultValueIfNotFound));
}
catch(exception &e){
throw runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
}
}
const string Properties::getString(const char *key, const char *defaultValueIfNotFound) const{
PropertyMap::const_iterator it;
it= propertyMap.find(key);
if(it==propertyMap.end()){
if(defaultValueIfNotFound != NULL) {
return string(defaultValueIfNotFound);
}
else {
throw runtime_error("Value not found in propertyMap: " + string(key) + ", loaded from: " + path);
}
}
else{
return it->second;
}
}
}}//end namepsace