ScummVM API documentation
numstr.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * This code is based on the CRAB engine
24 *
25 * Copyright (c) Arvind Raja Yadav
26 *
27 * Licensed under MIT
28 *
29 */
30
31#ifndef CRAB_NUMSTR_H
32#define CRAB_NUMSTR_H
33
34namespace Crab {
35
36template<typename T>
37inline Common::String numberToString(T number) {
38 return Common::String::format("%d", number);
39}
40
41template<>
42inline Common::String numberToString<float>(float number) {
43 return Common::String::format("%f", number);
44}
45
46template<typename T>
47inline T stringToNumber(char *text) {
48 int res = 0;
49 if (sscanf(text, "%d", &res) > 0)
50 return static_cast<T>(res); // static cast to deal with signed to unsigned conversions
51 return 0;
52}
53
54template<>
55inline float stringToNumber<float>(char *text) {
56 float res = 0.0f;
57 if (sscanf(text, "%f", &res) > 0)
58 return res;
59 return 0.0f;
60}
61
62template<>
63inline double stringToNumber<double>(char *text) {
64 double res = 0.0;
65 if (sscanf(text, "%lf", &res) > 0)
66 return res;
67 return 0.0;
68}
69
70template<typename T>
71T stringToNumber(const Common::String &text) {
72 return stringToNumber<T>(const_cast<char *>(text.c_str()));
73}
74
75template<typename T>
76void getPoint(T &v, Common::String &coords) {
77 int comma = coords.findFirstOf(',');
78
79 v._x = stringToNumber<int>(coords.substr(0, comma));
80 v._y = stringToNumber<int>(coords.substr(comma + 1));
81}
82
83} // End of namespace Crab
84
85#endif // CRAB_NUMSTR_H
size_t findFirstOf(value_type c, size_t pos=0) const
Definition: str.h:59
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
String substr(size_t pos=0, size_t len=npos) const