CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Text/Parsers.cpp
Views: 1401
1
#include <climits>
2
#include <cstdio>
3
#include <string>
4
5
#include "Common/Data/Text/Parsers.h"
6
#include "Common/StringUtils.h"
7
8
// Not strictly a parser...
9
void NiceSizeFormat(uint64_t size, char *out, size_t bufSize) {
10
int s = 0;
11
int frac = 0;
12
while (size >= 1024) {
13
s++;
14
frac = (int)size & 1023;
15
size /= 1024;
16
}
17
float f = (float)size + ((float)frac / 1024.0f);
18
if (s == 0)
19
snprintf(out, bufSize, "%d B", (int)size);
20
else {
21
static const char* const sizes[] = { "B","KB","MB","GB","TB","PB","EB" };
22
snprintf(out, bufSize, "%3.2f %s", f, sizes[s]);
23
}
24
}
25
26
std::string NiceSizeFormat(uint64_t size) {
27
char buffer[16];
28
NiceSizeFormat(size, buffer, sizeof(buffer));
29
return std::string(buffer);
30
}
31
32
bool Version::ParseVersionString(std::string str) {
33
if (str.empty())
34
return false;
35
if (str[0] == 'v')
36
str = str.substr(1);
37
if (3 != sscanf(str.c_str(), "%i.%i.%i", &major, &minor, &sub)) {
38
sub = 0;
39
if (2 != sscanf(str.c_str(), "%i.%i", &major, &minor))
40
return false;
41
}
42
return true;
43
}
44
45
std::string Version::ToString() const {
46
char temp[128];
47
snprintf(temp, sizeof(temp), "%i.%i.%i", major, minor, sub);
48
return std::string(temp);
49
}
50
51
int Version::ToInteger() const {
52
// This allows for ~2000 major versions, ~100 minor versions, and ~10000 sub versions.
53
return major * 1000000 + minor * 10000 + sub;
54
}
55
56
bool ParseMacAddress(const std::string &str, uint8_t macAddr[6]) {
57
unsigned int mac[6];
58
if (6 != sscanf(str.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5])) {
59
return false;
60
}
61
for (int i = 0; i < 6; i++) {
62
macAddr[i] = mac[i];
63
}
64
return true;
65
}
66
67
static bool TryParseUnsigned32(const std::string &str, uint32_t *const output) {
68
char *endptr = NULL;
69
70
// Holy crap this is ugly.
71
72
// Reset errno to a value other than ERANGE
73
errno = 0;
74
75
unsigned long value = strtoul(str.c_str(), &endptr, 0);
76
77
if (!endptr || *endptr)
78
return false;
79
80
if (errno == ERANGE)
81
return false;
82
83
if (ULONG_MAX > UINT_MAX) {
84
#ifdef _MSC_VER
85
#pragma warning (disable:4309)
86
#endif
87
// Note: The typecasts avoid GCC warnings when long is 32 bits wide.
88
if (value >= static_cast<unsigned long>(0x100000000ull)
89
&& value <= static_cast<unsigned long>(0xFFFFFFFF00000000ull))
90
return false;
91
}
92
93
*output = static_cast<uint32_t>(value);
94
return true;
95
}
96
97
bool TryParse(const std::string &str, uint32_t *const output) {
98
if (str[0] != '#') {
99
return TryParseUnsigned32(str, output);
100
} else {
101
// Parse it as "#RGBA" and convert to a ABGR interger
102
std::string s = ReplaceAll(str, "#", "0x");
103
if (TryParseUnsigned32(s, output)) {
104
int a = (*output >> 24) & 0xff;
105
int b = (*output >> 16) & 0xff;
106
int g = (*output >> 8) & 0xff;
107
int r = *output & 0xff;
108
*output = (r << 24) | (g << 16) | (b << 8) | a;
109
return true;
110
} else {
111
return false;
112
}
113
}
114
}
115
116
bool TryParse(const std::string &str, uint64_t *const output) {
117
char *endptr = NULL;
118
119
// Holy crap this is ugly.
120
121
// Reset errno to a value other than ERANGE
122
errno = 0;
123
124
uint64_t value = strtoull(str.c_str(), &endptr, 0);
125
126
if (!endptr || *endptr)
127
return false;
128
129
if (errno == ERANGE)
130
return false;
131
132
*output = value;
133
return true;
134
}
135
136
bool TryParse(const std::string &str, bool *const output) {
137
if ("1" == str || !strcasecmp("true", str.c_str()))
138
*output = true;
139
else if ("0" == str || !strcasecmp("false", str.c_str()))
140
*output = false;
141
else
142
return false;
143
144
return true;
145
}
146
147