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.h
Views: 1401
1
#pragma once
2
3
#include <string>
4
#include <sstream>
5
#include <cstdint>
6
7
#undef major
8
#undef minor
9
10
// Parses version strings of the format "Major.Minor.Sub" and lets you interact with them conveniently.
11
struct Version {
12
Version() : major(0), minor(0), sub(0) {}
13
Version(const std::string &str) {
14
if (!ParseVersionString(str)) {
15
major = -1;
16
minor = -1;
17
sub = -1;
18
}
19
}
20
21
int major;
22
int minor;
23
int sub;
24
25
bool IsValid() const {
26
return sub >= 0 && minor >= 0 && major >= 0;
27
}
28
29
bool operator == (const Version &other) const {
30
return major == other.major && minor == other.minor && sub == other.sub;
31
}
32
bool operator != (const Version &other) const {
33
return !(*this == other);
34
}
35
36
bool operator <(const Version &other) const {
37
if (major < other.major) return true;
38
if (major > other.major) return false;
39
if (minor < other.minor) return true;
40
if (minor > other.minor) return false;
41
if (sub < other.sub) return true;
42
if (sub > other.sub) return false;
43
return false;
44
}
45
46
bool operator >=(const Version &other) const {
47
return !(*this < other);
48
}
49
50
std::string ToString() const;
51
int ToInteger() const;
52
private:
53
bool ParseVersionString(std::string str);
54
};
55
56
bool ParseMacAddress(const std::string &str, uint8_t macAddr[6]);
57
58
bool TryParse(const std::string &str, bool *const output);
59
bool TryParse(const std::string &str, uint32_t *const output);
60
bool TryParse(const std::string &str, uint64_t *const output);
61
62
template <typename N>
63
static bool TryParse(const std::string &str, N *const output) {
64
std::istringstream iss(str);
65
66
N tmp = 0;
67
if (iss >> tmp) {
68
*output = tmp;
69
return true;
70
} else
71
return false;
72
}
73
74
void NiceSizeFormat(uint64_t size, char *out, size_t bufSize);
75
76
std::string NiceSizeFormat(uint64_t size);
77
78