ScummVM API documentation
parse_utils.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#ifndef HARVESTER_PARSE_UTILS_H
23#define HARVESTER_PARSE_UTILS_H
24
25#include <climits>
26
27#include "common/str.h"
28#include "common/util.h"
29
30namespace Harvester {
31
32// Mirrors atoi's permissive decimal parsing while relying on Common helpers.
33inline int parseAsciiIntOrZero(const Common::String &value) {
34 uint start = 0;
35 while (start < value.size() && Common::isSpace((byte)value[start]))
36 ++start;
37
38 bool negative = false;
39 if (start < value.size() && (value[start] == '+' || value[start] == '-')) {
40 negative = value[start] == '-';
41 ++start;
42 }
43
44 uint end = start;
45 while (end < value.size() && Common::isDigit((byte)value[end]))
46 ++end;
47
48 if (start == end)
49 return 0;
50
51 const uint64 magnitude = value.substr(start, end - start).asUint64();
52 if (negative) {
53 if (magnitude > (uint64)INT_MAX + 1)
54 return INT_MIN;
55 return -(int)magnitude;
56 }
57
58 if (magnitude > (uint64)INT_MAX)
59 return INT_MAX;
60 return (int)magnitude;
61}
62
63} // End of namespace Harvester
64
65#endif
uint64 asUint64() const
Definition: str.h:59
String substr(size_t pos=0, size_t len=npos) const
bool isSpace(int c)
bool isDigit(int c)