File: properties.cpp

package info (click to toggle)
megaglest 3.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,844 kB
  • ctags: 18,191
  • sloc: cpp: 144,280; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (643 lines) | stat: -rw-r--r-- 22,313 bytes parent folder | download | duplicates (5)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// ==============================================================
//	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 <cstring>
#include <iostream>
#include <sstream>

#include "conversion.h"
#include "util.h"
#include "platform_common.h"
#include "platform_util.h"
#include "randomgen.h"

#ifdef WIN32

#include <shlwapi.h>
#include <shlobj.h>

#endif

#include "utf8.h"
#include "font.h"
#include "string_utils.h"
#include "leak_dumper.h"

using namespace std;
using namespace Shared::PlatformCommon;
using namespace Shared::Platform;
using namespace Shared::Graphics;

namespace Shared{ namespace Util{

string Properties::applicationPath = "";
string Properties::applicationDataPath = "";
string Properties::gameVersion = "";

string Properties::techtreePath = "";
string Properties::scenarioPath = "";
string Properties::tutorialPath = "";

// =====================================================
//	class Properties
// =====================================================

void Properties::load(const string &path, bool clearCurrentProperties) {

	char lineBuffer[maxLine]="";
	this->path= path;

	bool is_utf8_language = valid_utf8_file(path.c_str());

#if defined(WIN32) && !defined(__MINGW32__)
	wstring wstr = utf8_decode(path);
	FILE *fp = _wfopen(wstr.c_str(), L"r");
	//wifstream fileStream(fp);
	ifstream fileStream(fp);
#else
	//wifstream fileStream;
	ifstream fileStream;
	fileStream.open(path.c_str(), ios_base::in);
#endif
	
	if(fileStream.is_open() == false){
		if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str());
		throw megaglest_runtime_error("File NOT FOUND, can't open file: [" + path + "]");
	}

	if(clearCurrentProperties == true) {
		propertyMap.clear();
		propertyMapTmp.clear();
	}

	while(fileStream.eof() == false) {
		lineBuffer[0]='\0';
		fileStream.getline(lineBuffer, maxLine);
		lineBuffer[maxLine-1]='\0';

		processTextLine(is_utf8_language,lineBuffer);
	}

	fileStream.close();
#if defined(WIN32) && !defined(__MINGW32__)
	if(fp) {
		fclose(fp);
	}
#endif
}

void Properties::loadFromText(const string &text) {
	bool is_utf8_language = false;

	char lineBuffer[maxLine]="";

	std::istringstream textStream(text);
	while(textStream.eof() == false) {
		//lineBuffer[0]='\0';
		std::string lineText;
		getline(textStream, lineText);
		if(lineText.length() > 0) {
			memset(&lineBuffer[0],0,maxLine);
			memcpy(&lineBuffer[0],&lineText[0],lineText.length());

			//fileStream.getline(lineBuffer, maxLine);
			//lineBuffer[maxLine-1]='\0';

			processTextLine(is_utf8_language,&lineBuffer[0]);
		}
	}
}

void Properties::processTextLine(bool is_utf8_language, char *lineBuffer) {
	//printf("\n[%ls]\n",lineBuffer);
	//printf("\n[%s]\n",&lineBuffer[0]);

	string line, key, value, original_value;
	size_t pos=0;

	if(lineBuffer != NULL && lineBuffer[0] != '\0') {
		// If the file is NOT in UTF-8 format convert each line
		if(is_utf8_language == false && Font::forceLegacyFonts == false) {
			char *utfStr = ConvertToUTF8(&lineBuffer[0]);

			//printf("\nBefore [%s] After [%s]\n",&lineBuffer[0],utfStr);

			memset(&lineBuffer[0],0,maxLine);
			memcpy(&lineBuffer[0],&utfStr[0],strlen(utfStr));

			delete [] utfStr;
		}
		else if(is_utf8_language == true && Font::forceLegacyFonts == true) {
			char *asciiStr = ConvertFromUTF8(&lineBuffer[0]);

			//printf("\nBefore [%s] After [%s]\n",&lineBuffer[0],utfStr);

			memset(&lineBuffer[0],0,maxLine);
			memcpy(&lineBuffer[0],&asciiStr[0],strlen(asciiStr));

			delete [] asciiStr;
		}
	}

	//process line if it it not a comment
	if(lineBuffer != NULL && lineBuffer[0] != ';' && 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);
			original_value = value;

			if(applyTagsToValue(value) == true) {
				if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Property key [%s] now has value [%s] original_value [%s]\n",key.c_str(),value.c_str(),original_value.c_str());
			}

			bool replaceExisting = false;
			if(propertyMap.find(key) != propertyMap.end()) {
				replaceExisting = true;
			}
			propertyMap[key] = original_value;
			propertyMapTmp[key] = value;

			if(replaceExisting == false) {
				propertyVector.push_back(PropertyPair(key, original_value));
				propertyVectorTmp.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 = original_value;

						propertyVectorTmp[i].second = value;
						break;
					}
				}
			}
		}
	}
}

std::map<string,string> Properties::getTagReplacementValues(std::map<string,string> *mapExtraTagReplacementValues) {
	std::map<string,string> mapTagReplacementValues;

	//
	// #1
	// First add the standard tags
	//
#ifdef WIN32
	const char *homeDirX = getenv("USERPROFILE");
#else
	string home = getUserHome();
	const char *homeDirX = home.c_str();
#endif

	string homeDir = safeCharPtrCopy(homeDirX, 8096);

	mapTagReplacementValues["~/"] 				= homeDir;
	mapTagReplacementValues["$HOME"] 			= homeDir;
	mapTagReplacementValues["%%HOME%%"] 		= homeDir;
	mapTagReplacementValues["%%USERPROFILE%%"] 	= homeDir;
	mapTagReplacementValues["%%HOMEPATH%%"] 	= homeDir;
	mapTagReplacementValues["{HOMEPATH}"] 		= 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))) {

	   //const wchar_t *wBuf = &szPath[0];
	   //size_t size = MAX_PATH + 1;
	   //char pMBBuffer[MAX_PATH + 1]="";
	   //wcstombs_s(&size, &pMBBuffer[0], (size_t)size, wBuf, (size_t)size);// Convert to char* from TCHAR[]
  	   //string appPath="";
	   //appPath.assign(&pMBBuffer[0]); // Now assign the char* to the string, and there you have it!!! :) 
	   std::string appPath = utf8_encode(szPath);

	   //string appPath = szPath;
	   mapTagReplacementValues["$APPDATA"] = appPath;
	   mapTagReplacementValues["%%APPDATA%%"] = appPath;
	   mapTagReplacementValues["{APPDATA}"] = appPath;
   }
#endif

	//char *username = NULL;
   char *username = getenv("USERNAME");

	mapTagReplacementValues["$USERNAME"] = (username != NULL ? username : "");
	mapTagReplacementValues["%%USERNAME%%"] = (username != NULL ? username : "");
	mapTagReplacementValues["{USERNAME}"] = (username != NULL ? username : "");

	mapTagReplacementValues["$APPLICATIONPATH"] = Properties::applicationPath;
	mapTagReplacementValues["%%APPLICATIONPATH%%"] = Properties::applicationPath;
	mapTagReplacementValues["{APPLICATIONPATH}"] = Properties::applicationPath;

#if defined(CUSTOM_DATA_INSTALL_PATH)
	mapTagReplacementValues["$APPLICATIONDATAPATH"] = formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH));
	mapTagReplacementValues["%%APPLICATIONDATAPATH%%"] = formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH));
	mapTagReplacementValues["{APPLICATIONDATAPATH}"] = formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH));

#else
	mapTagReplacementValues["$APPLICATIONDATAPATH"] = Properties::applicationDataPath;
	mapTagReplacementValues["%%APPLICATIONDATAPATH%%"] = Properties::applicationDataPath;
	mapTagReplacementValues["{APPLICATIONDATAPATH}"] = Properties::applicationDataPath;

#endif

	mapTagReplacementValues["{TECHTREEPATH}"] = Properties::techtreePath;
	mapTagReplacementValues["{SCENARIOPATH}"] = Properties::scenarioPath;
	mapTagReplacementValues["{TUTORIALPATH}"] = Properties::tutorialPath;

	mapTagReplacementValues["$GAMEVERSION"] = Properties::gameVersion;

	//
	// #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::isValuePathVariable(const string &value) {
	if(value.find("~/") != value.npos ||
		value.find("$HOME") != value.npos ||
		value.find("%%HOME%%") != value.npos ||
		value.find("%%USERPROFILE%%") != value.npos ||
		value.find("%%HOMEPATH%%") != value.npos ||
		value.find("{HOMEPATH}") != value.npos ||
        value.find("$APPDATA") != value.npos ||
	    value.find("%%APPDATA%%") != value.npos ||
	    value.find("{APPDATA}") != value.npos ||
		value.find("$APPLICATIONPATH") != value.npos ||
		value.find("%%APPLICATIONPATH%%") != value.npos ||
		value.find("{APPLICATIONPATH}") != value.npos ||
		value.find("$APPLICATIONDATAPATH") != value.npos ||
		value.find("%%APPLICATIONDATAPATH%%") != value.npos ||
		value.find("{APPLICATIONDATAPATH}") != value.npos ||
		value.find("{TECHTREEPATH}") != value.npos ||
		value.find("{SCENARIOPATH}") != value.npos ||
		value.find("{TUTORIALPATH}") != value.npos) {

			return true;
	}
	return false;
}
void Properties::updateValuePathVariable(string &value, bool skipUpdatePathClimbingParts) {
	replaceAll(value,"//","/");
	replaceAll(value,"\\\\","\\");
	if(skipUpdatePathClimbingParts == false) {
		updatePathClimbingParts(value);
	}
}

bool Properties::applyTagsToValue(string &value, const std::map<string,string> *mapTagReplacementValues,bool skipUpdatePathClimbingParts) {
	string originalValue = value;
	bool valueRequiresPathUpdate = Properties::isValuePathVariable(value);
	//if(originalValue.find("$APPLICATIONDATAPATH") != string::npos) {
	//	printf("\nBEFORE SUBSTITUTE [%s] app [%s] mapTagReplacementValues [%p]\n",originalValue.c_str(),Properties::applicationPath.c_str(),mapTagReplacementValues);
	//}

	if(mapTagReplacementValues != NULL) {
		for(std::map<string,string>::const_iterator iterMap = mapTagReplacementValues->begin();
				iterMap != mapTagReplacementValues->end(); ++iterMap) {

//			if(value.find("{SCENARIOPATH}") != string::npos) {
//				printf("\n\n** WILL REPLACE [%s]\n",value.c_str());
//				replaceAll(value, "{SCENARIOPATH}",		Properties::scenarioPath);
//				printf("** REPLACED [%s]\n\n",value.c_str());
//			}
//			else {
			replaceAll(value, iterMap->first, iterMap->second);
//			}
		}
	}
	else {
#ifdef WIN32
	const char *homeDirX = getenv("USERPROFILE");
#else
	string home = getUserHome();
	const char *homeDirX = home.c_str();
#endif

	string homeDir = safeCharPtrCopy(homeDirX, 8096);

	replaceAll(value, "~/", 			homeDir);
	replaceAll(value, "$HOME", 			homeDir);
	replaceAll(value, "%%HOME%%", 		homeDir);
	replaceAll(value, "%%USERPROFILE%%",homeDir);
	replaceAll(value, "%%HOMEPATH%%",	homeDir);
	replaceAll(value, "{HOMEPATH}",		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))) {
	   //const wchar_t *wBuf = &szPath[0];
	   //size_t size = MAX_PATH + 1;
	   //char pMBBuffer[MAX_PATH + 1]="";
	   //wcstombs_s(&size, &pMBBuffer[0], (size_t)size, wBuf, (size_t)size);// Convert to char* from TCHAR[]
  	   //string appPath="";
	   //appPath.assign(&pMBBuffer[0]); // Now assign the char* to the string, and there you have it!!! :) 
	   std::string appPath = utf8_encode(szPath);

		//string appPath = szPath;
       replaceAll(value, "$APPDATA", 	appPath);
	   replaceAll(value, "%%APPDATA%%", appPath);
	   replaceAll(value, "{APPDATA}", 	appPath);
   }
#endif

	//char *username = NULL;
   char *username = getenv("USERNAME");
	replaceAll(value, "$USERNAME", 		(username != NULL ? username : ""));
	replaceAll(value, "%%USERNAME%%", 	(username != NULL ? username : ""));
	replaceAll(value, "{USERNAME}", 	(username != NULL ? username : ""));

	replaceAll(value, "$APPLICATIONPATH", 		Properties::applicationPath);
	replaceAll(value, "%%APPLICATIONPATH%%",	Properties::applicationPath);
	replaceAll(value, "{APPLICATIONPATH}",		Properties::applicationPath);

#if defined(CUSTOM_DATA_INSTALL_PATH)
	replaceAll(value, "$APPLICATIONDATAPATH", 		formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH)));
	replaceAll(value, "%%APPLICATIONDATAPATH%%",	formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH)));
	replaceAll(value, "{APPLICATIONDATAPATH}",		formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH)));

#else
	replaceAll(value, "$APPLICATIONDATAPATH", 		Properties::applicationDataPath);
	replaceAll(value, "%%APPLICATIONDATAPATH%%",	Properties::applicationDataPath);
	replaceAll(value, "{APPLICATIONDATAPATH}",		Properties::applicationDataPath);

#endif

	replaceAll(value, "{TECHTREEPATH}",		Properties::techtreePath);
//	if(value.find("{SCENARIOPATH}") != string::npos) {
//		printf("\n\n** WILL REPLACE [%s]\n",value.c_str());
	replaceAll(value, "{SCENARIOPATH}",		Properties::scenarioPath);
//		printf("** REPLACED [%s]\n\n",value.c_str());
//	}
	replaceAll(value, "{TUTORIALPATH}",		Properties::tutorialPath);

	replaceAll(value, "$GAMEVERSION",		Properties::gameVersion);
	}

	//if(originalValue != value || originalValue.find("$APPLICATIONDATAPATH") != string::npos) {
	//	printf("\nBEFORE SUBSTITUTE [%s] AFTER [%s]\n",originalValue.c_str(),value.c_str());
	//}

	if(valueRequiresPathUpdate == true) {
		Properties::updateValuePathVariable(value, skipUpdatePathClimbingParts);
	}
	return (originalValue != value);
}

void Properties::save(const string &path){
#if defined(WIN32) && !defined(__MINGW32__)
	FILE *fp = _wfopen(utf8_decode(path).c_str(), L"w");
	ofstream fileStream(fp);
#else
	ofstream fileStream;
	fileStream.open(path.c_str(), ios_base::out | ios_base::trunc);
#endif
	fileStream << "; === propertyMap File === \n";
	fileStream << '\n';

	for(PropertyMap::iterator pi= propertyMap.begin(); pi!=propertyMap.end(); ++pi){
		fileStream << pi->first << '=' << pi->second << '\n';
	}

	fileStream.close();
#if defined(WIN32) && !defined(__MINGW32__)
	if(fp) fclose(fp);
#endif
}

void Properties::clear(){
	propertyMap.clear();
	propertyMapTmp.clear();
	propertyVector.clear();
	propertyVectorTmp.clear();
}

int Properties::getPropertyCount() const {
	return (int)propertyVectorTmp.size();
}
string Properties::getKey(int i) const {
	return propertyVectorTmp[i].first;
}
string Properties::getString(int i) const {
	return propertyVectorTmp[i].second;
}

bool Properties::getBool(const string &key, const char *defaultValueIfNotFound) const{
	try{
		return strToBool(getString(key,defaultValueIfNotFound));
	}
	catch(exception &e){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + key + " in: " + path+"\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + key + " in: " + path+"\n[" + e.what() + "]");
	}
	return false;
}

int Properties::getInt(const string &key,const char *defaultValueIfNotFound) const{
	try{
		return strToInt(getString(key,defaultValueIfNotFound));
	}
	catch(exception &e){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
	}
	return 0;
}

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 megaglest_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{
	float result = 0.0;
	try{
		result = strToFloat(getString(key,defaultValueIfNotFound));
	}
	catch(exception &e){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + key + " in: " + path + "\n[" + e.what() + "]");
	}
	return result;
}

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 megaglest_runtime_error("Value out of range: " + key + ", min: " + floatToStr(min,16) + ", max: " + floatToStr(max,16));
	}
	return f;
}

const string Properties::getString(const string &key, const char *defaultValueIfNotFound) const{
	PropertyMap::const_iterator it = propertyMapTmp.find(key);
	if(it == propertyMapTmp.end()) {
	    if(defaultValueIfNotFound != NULL) {
	        //printf("In [%s::%s - %d]defaultValueIfNotFound = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,defaultValueIfNotFound);
	        return string(defaultValueIfNotFound);
	    }
	    else {
            //throw megaglest_runtime_error("Value not found in propertyMap: " + key + ", loaded from: " + path);
	    	throw runtime_error("Value not found in propertyMap: " + key + ", loaded from: " + path);
	    }
	}
	else{
		return (it->second != "" ? it->second : (defaultValueIfNotFound != NULL ? defaultValueIfNotFound : it->second));
	}
}

const string Properties::getRandomKey(const bool realrandom) const{
	PropertyMap::const_iterator it;
	int max=getPropertyCount();
	int randomIndex=-1;
	if(realrandom == true){
		Chrono seed(true);
		srand((unsigned int)seed.getCurTicks());

		randomIndex=rand()%max;
	}
	else{
		RandomGen randgen;
		randomIndex=randgen.randRange(0,max);
	}
	string s=getKey(randomIndex);
	return (s != "" ? s : "nothing found");
}

bool Properties::hasString(const string &key) const {
	PropertyMap::const_iterator it = propertyMapTmp.find(key);
	if(it == propertyMapTmp.end()) {
		return false;
	}
	return true;
}

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,6));
}

void Properties::setString(const string &key, const string &value){
	propertyMap.erase(key);
	propertyMap.insert(PropertyPair(key, value));

	propertyMapTmp.erase(key);
	propertyMapTmp.insert(PropertyPair(key, value));

}

string Properties::toString(){
	string rStr;

	for(PropertyMap::iterator pi= propertyMapTmp.begin(); pi!=propertyMapTmp.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){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + string(key) + " in: " + path+"\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + string(key) + " in: " + path+"\n[" + e.what() + "]");
	}
	return false;
}

int Properties::getInt(const char *key,const char *defaultValueIfNotFound) const{
	try{
		return strToInt(getString(key,defaultValueIfNotFound));
	}
	catch(exception &e){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
	}
	return 0;
}

float Properties::getFloat(const char *key, const char *defaultValueIfNotFound) const{
	float result = 0.0;
	try{
		result = strToFloat(getString(key,defaultValueIfNotFound));
	}
	catch(exception &e){
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what());
		//throw megaglest_runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
		throw runtime_error("Error accessing value: " + string(key) + " in: " + path + "\n[" + e.what() + "]");
	}
	return result;
}

const string Properties::getString(const char *key, const char *defaultValueIfNotFound) const{
	PropertyMap::const_iterator it = propertyMapTmp.find(key);
	if(it == propertyMapTmp.end()) {
	    if(defaultValueIfNotFound != NULL) {
	        //printf("In [%s::%s - %d]defaultValueIfNotFound = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,defaultValueIfNotFound);
	        return string(defaultValueIfNotFound);
	    }
	    else {
            //throw megaglest_runtime_error("Value not found in propertyMap: " + string(key) + ", loaded from: " + path);
	    	throw runtime_error("Value not found in propertyMap: " + string(key) + ", loaded from: " + path);
	    }
	}
	else{
		return (it->second != "" ? it->second : (defaultValueIfNotFound != NULL ? defaultValueIfNotFound : it->second));
	}
}


}}//end namepsace