File: properties.cpp

package info (click to toggle)
glest 3.1.2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 3,496 kB
  • ctags: 6,107
  • sloc: cpp: 30,065; sh: 8,293; makefile: 48
file content (166 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2007 Martio 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 "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);
		
		if(lineBuffer[0]==';'){
			continue;
		}

		// 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){
			continue;
		}

		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();
}

bool Properties::getBool(const string &key) const{
	try{
		return strToBool(getString(key));
	}
	catch(exception &e){
		throw runtime_error("Error accessing value: " + key + " in: " + path+"\n" + e.what());
	}
}

int Properties::getInt(const string &key) const{
	try{
		return strToInt(getString(key));
	}
	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{
	int i= getInt(key);
	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{
	try{
		return strToFloat(getString(key));
	}
	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{
	float f= getFloat(key);
	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{
	PropertyMap::const_iterator it;
	it= propertyMap.find(key);
	if(it==propertyMap.end()){
		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;
}

}}//end namepsace