annotate dep/animia/src/util.cc @ 199:9f3534f6b8c4

dep/animia: initial Wayland support, drop non-working kvm fd plugin
author Paper <mrpapersonic@gmail.com>
date Tue, 02 Jan 2024 02:34:27 -0500
parents 80d6b28eb29f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
1 #include <algorithm>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
2 #include <fstream>
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
3 #include <regex>
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
4 #include <sstream>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
5 #include <string>
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
6
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
7 #include "animia/util.h"
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
8
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
9 namespace animia::internal::util {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
10
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
11 bool ReadFile(const std::string& path, std::string& data) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
12 std::ifstream file(path.c_str(), std::ios::in | std::ios::binary);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
13 if (!file)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
14 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
15
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
16 std::ostringstream string;
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
17 string << file.rdbuf();
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
18 file.close();
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
19
158
80d6b28eb29f dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
20 data = string.str();
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
21
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
22 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
23 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
24
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
25 /* this assumes ASCII... which really should be the case for what we need, anyway */
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
26 bool EqualStrings(const std::string& str1, const std::string& str2) {
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
27 auto tolower = [](const char c) -> char { return ('A' <= c && c <= 'Z') ? c + ('a' - 'A') : c; };
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
28
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
29 auto equal_chars = [&tolower](const char c1, const char c2) -> bool { return tolower(c1) == tolower(c2); };
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
30
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
31 return str1.length() == str2.length() && std::equal(str1.begin(), str1.end(), str2.begin(), equal_chars);
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
32 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
33
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
34 bool Stem(const std::string& filename, std::string& stem) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
35 unsigned long long pos = filename.find_last_of(".");
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
36 if (pos != std::string::npos)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
37 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
38
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
39 stem = filename.substr(0, pos);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
40 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
41 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
42
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
43 bool CheckPattern(const std::string& pattern, const std::string& str) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
44 if (pattern.empty())
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
45 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
46 if (pattern.front() == '^' && std::regex_match(str, std::regex(pattern)))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
47 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
48 return util::EqualStrings(pattern, str);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
49 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents: 138
diff changeset
50
138
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
51 bool TrimLeft(std::string& str, const char* chars) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
52 if (str.empty())
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
53 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
54
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
55 const auto found = str.find_first_not_of(chars);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
56
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
57 if (found == 0)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
58 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
59
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
60 if (found == std::string::npos)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
61 str.clear();
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
62 else
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
63 str.erase(0, found);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
64
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
65 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
66 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
67
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
68 bool TrimRight(std::string& str, const char* chars) {
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
69 if (str.empty())
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
70 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
71
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
72 const auto found = str.find_last_not_of(chars);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
73
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
74 if (found == str.size() - 1)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
75 return false;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
76
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
77 if (found == std::string::npos)
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
78 str.clear();
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
79 else
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
80 str.resize(found + 1);
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
81
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
82 return true;
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
83 }
28842a8d0c6b dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents: 137
diff changeset
84
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
85 } // namespace animia::internal::util