Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Text/Parsers.h
3187 views
1
#pragma once
2
3
#include <string>
4
#include <cstring>
5
#include <sstream>
6
#include <cstdint>
7
8
#undef major
9
#undef minor
10
11
// Parses version strings of the format "Major.Minor.Sub" and lets you interact with them conveniently.
12
struct Version {
13
Version() : major(0), minor(0), sub(0) {}
14
Version(const std::string &str) {
15
if (!ParseVersionString(str)) {
16
major = -1;
17
minor = -1;
18
sub = -1;
19
}
20
}
21
22
int major;
23
int minor;
24
int sub;
25
26
bool IsValid() const {
27
return sub >= 0 && minor >= 0 && major >= 0;
28
}
29
30
bool operator == (const Version &other) const {
31
return major == other.major && minor == other.minor && sub == other.sub;
32
}
33
bool operator != (const Version &other) const {
34
return !(*this == other);
35
}
36
37
bool operator <(const Version &other) const {
38
if (major < other.major) return true;
39
if (major > other.major) return false;
40
if (minor < other.minor) return true;
41
if (minor > other.minor) return false;
42
if (sub < other.sub) return true;
43
if (sub > other.sub) return false;
44
return false;
45
}
46
47
bool operator >=(const Version &other) const {
48
return !(*this < other);
49
}
50
51
std::string ToString() const;
52
int ToInteger() const;
53
private:
54
bool ParseVersionString(std::string str);
55
};
56
57
bool ParseMacAddress(const std::string &str, uint8_t macAddr[6]);
58
59
bool TryParse(const std::string &str, bool *const output);
60
bool TryParse(const std::string &str, uint32_t *const output);
61
bool TryParse(const std::string &str, uint64_t *const output);
62
63
template <typename N>
64
static bool TryParse(const std::string &str, N *const output) {
65
std::istringstream iss(str);
66
67
N tmp = 0;
68
if (iss >> tmp) {
69
*output = tmp;
70
return true;
71
} else
72
return false;
73
}
74
75
void NiceSizeFormat(uint64_t size, char *out, size_t bufSize);
76
77
std::string NiceSizeFormat(uint64_t size);
78
79
// seconds, or minutes, or hours.
80
// Uses I18N strings.
81
std::string NiceTimeFormat(int seconds);
82
83
// Not a parser, needs a better location.
84
// Simplified version of ShaderWriter. Would like to have that inherit from this but can't figure out how
85
// due to the return value chaining.
86
// TODO: We actually also have Buffer, with .Printf(), which is almost as convenient. Should maybe improve that instead?
87
class StringWriter {
88
public:
89
StringWriter(char *buffer, size_t bufSize) : start_(buffer), p_(buffer), bufSize_(bufSize) {
90
buffer[0] = '\0';
91
}
92
template<size_t sz>
93
explicit StringWriter(char (&buffer)[sz]) : start_(buffer), p_(buffer), bufSize_(sz) {
94
buffer[0] = '\0';
95
}
96
StringWriter(const StringWriter &) = delete;
97
98
std::string_view as_view() const {
99
return std::string_view(start_, p_ - start_);
100
}
101
102
size_t size() const {
103
return p_ - start_;
104
}
105
106
// Assumes the input is zero-terminated.
107
// C: Copies a string literal (which always are zero-terminated, the count includes the zero) directly to the stream.
108
template<size_t T>
109
StringWriter &C(const char(&text)[T]) {
110
const size_t remainder = bufSize_ - (p_ - start_);
111
if (remainder <= T)
112
return *this;
113
memcpy(p_, text, T);
114
p_ += T - 1;
115
return *this;
116
}
117
// Assumes the input is zero-terminated.
118
// C: Copies a string literal (which always are zero-terminated, the count includes the zero) directly to the stream.
119
StringWriter &B(bool b) {
120
return W(b ? "true" : "false");
121
}
122
123
// W: Writes a string_view to the stream.
124
StringWriter &W(std::string_view text) {
125
const size_t remainder = bufSize_ - (p_ - start_);
126
if (remainder <= text.length())
127
return *this;
128
memcpy(p_, text.data(), text.length());
129
p_ += text.length();
130
*p_ = '\0';
131
return *this;
132
}
133
134
// F: Formats into the buffer.
135
StringWriter &F(const char *format, ...);
136
StringWriter &endl() {
137
const size_t remainder = bufSize_ - (p_ - start_);
138
if (remainder <= 2)
139
return *this;
140
141
return C("\n");
142
}
143
144
void Rewind(size_t offset) {
145
p_ -= offset;
146
}
147
148
private:
149
char *start_;
150
char *p_;
151
size_t bufSize_;
152
};
153
154