File: conversion.cpp

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (245 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download | duplicates (6)
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱ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 "platform_common.h"
#include <sstream>
#include <iostream>
#include <locale>
#include "platform_util.h"
#include <limits>
#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 megaglest_runtime_error("Error converting string to bool, expected 0 or 1, found: [" + s + "]");
}

int strToInt(const string &s){
	char *endChar;
	setlocale(LC_NUMERIC, "C");
	int intValue= strtol(s.c_str(), &endChar, 10);

	if(*endChar!='\0'){
		throw megaglest_runtime_error("Error converting from string to int, found: [" + s + "]");
	}

	return intValue;
}

uint32 strToUInt(const string &s){
	char *endChar;
	setlocale(LC_NUMERIC, "C");
	uint32 intValue= strtoul(s.c_str(), &endChar, 10);

	if(*endChar!='\0'){
		throw megaglest_runtime_error("Error converting from string to uint, found: [" + s + "]");
	}

	return intValue;
}


float strToFloat(const string &s){
	char *endChar;

	setlocale(LC_NUMERIC, "C");
	float floatValue= static_cast<float>(strtod(s.c_str(), &endChar));

	if(*endChar!='\0'){
		throw megaglest_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;

	setlocale(LC_NUMERIC, "C");
	*i= strtol(s.c_str(), &endChar, 10);

	if(*endChar!='\0'){
		return false;
	}
	return true;
}

bool strToUInt(const string &s, uint32 *i) {
	char *endChar;

	setlocale(LC_NUMERIC, "C");
	*i= strtoul(s.c_str(), &endChar, 10);

	if(*endChar!='\0'){
		return false;
	}
	return true;
}

bool strToFloat(const string &s, float *f) {
	char *endChar;
	setlocale(LC_NUMERIC, "C");
	*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(const int64 value) {
	char str[strSize]="";
//	static int MAX_INT_VALUE = std::numeric_limits<int>::max();
//	if(value <= MAX_INT_VALUE) {
//		snprintf(str, strSize-1, "%d", (int)value);
//	}
//	else {
		snprintf(str, strSize-1, "%lld", (long long int)value);
//	}
	return (str[0] != '\0' ? str : "");
}

string uIntToStr(const uint64 value) {
	char str[strSize]="";
//	static unsigned int MAX_UNSIGNED_INT_VALUE = std::numeric_limits<unsigned int>::max();
//	if(value <= MAX_UNSIGNED_INT_VALUE) {
//		snprintf(str, strSize-1, "%u", (int)value);
//	}
//	else {
		snprintf(str, strSize-1, "%llu", (long long unsigned int)value);
//	}
	return (str[0] != '\0' ? str : "");
}

string intToHex(int i){
	char str[strSize]="";
	snprintf(str, strSize-1, "%x", i);
	return (str[0] != '\0' ? str : "");
}

string floatToStr(float f,int precsion) {
	setlocale(LC_NUMERIC, "C");

	char str[strSize]="";
	snprintf(str, strSize-1, "%.*f", precsion,f);
	return (str[0] != '\0' ? str : "");
}

string doubleToStr(double d,int precsion) {
	setlocale(LC_NUMERIC, "C");

	char str[strSize]="";
	snprintf(str, strSize-1, "%.*f", precsion,d);
	return (str[0] != '\0' ? str : "");
}

bool IsNumeric(const char *p, bool  allowNegative) {
	if(p == NULL) {
		return false;
	}
	if(strcmp(p,"-") == 0) {
		return false;
	}
    int index = 0;
    for ( ; *p; p++) {
      if (*p < '0' || *p > '9') {
    	  if(allowNegative == false || (*p != '-' && index == 0)) {
    		  return false;
    	  }
      }
      index++;
    }
    return true;
}

class Comma: public numpunct<char>// own facet class
{
     protected:
          char do_thousands_sep() const { return ','; }// use the comma
          string do_grouping() const { return "\3"; }//group 3 digits
};
string formatNumber(uint64 f) {

	locale myloc(  locale(),    // C++ default locale
	          new Comma);// Own numeric facet

	ostringstream out;
	out.imbue(myloc);
	out << f;
	return out.str();
}

string getTimeDuationString(int frames, int updateFps) {
	int framesleft = frames;
	int hours = (int)((int) frames / (float)updateFps / 3600.0f);
	framesleft = framesleft - hours * 3600 * updateFps;
	int minutes = (int)((int) framesleft / (float)updateFps / 60.0f);
	framesleft = framesleft - minutes * 60 * updateFps;
	int seconds = (int)((int) framesleft / (float)updateFps);
	//framesleft=framesleft-seconds*GameConstants::updateFps;

	string hourstr = intToStr(hours);
	if(hours < 10) {
		hourstr = "0" + hourstr;
	}

	string minutestr = intToStr(minutes);
	if(minutes < 10) {
		minutestr = "0" + minutestr;
	}

	string secondstr = intToStr(seconds);
	if(seconds < 10) {
		secondstr = "0" + secondstr;
	}

	return hourstr + ":" + minutestr + ":" + secondstr;
}

}}//end namespace