Mercurial > minori
comparison dep/animia/src/util.cc @ 138:28842a8d0c6b
dep/animia: huge refactor (again...)
but this time, it actually compiles! and it WORKS! (on win32... not sure about
other platforms...)
configuring players is still not supported: at some point I'll prune something
up...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 12 Nov 2023 04:53:19 -0500 |
parents | 69db40272acd |
children | 8700806c2cc2 |
comparison
equal
deleted
inserted
replaced
137:69db40272acd | 138:28842a8d0c6b |
---|---|
1 #include <algorithm> | |
2 #include <fstream> | |
3 #include <sstream> | |
4 #include <string> | |
5 #include <cctype> | |
6 | |
7 #include "animia/util.h" | |
8 | |
9 namespace animia::internal::util { | |
10 | |
11 bool ReadFile(const std::string& path, std::string& data) { | |
12 std::ifstream file(path.c_str(), std::ios::in | std::ios::binary); | |
13 | |
14 if (!file) | |
15 return false; | |
16 | |
17 file.seekg(0, std::ios::end); | |
18 data.resize(static_cast<size_t>(file.tellg())); | |
19 file.seekg(0, std::ios::beg); | |
20 | |
21 file.read(&data.front(), data.size()); | |
22 file.close(); | |
23 | |
24 return true; | |
25 } | |
26 | |
27 bool EqualStrings(const std::string& str1, const std::string& str2) { | |
28 auto equal_chars = [](const char c1, const char c2) -> bool { | |
29 return std::tolower(static_cast<unsigned char>(c1)) == std::tolower(static_cast<unsigned char>(c2)); | |
30 }; | |
31 | |
32 return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), equal_chars); | |
33 } | |
34 | |
35 bool TrimLeft(std::string& str, const char* chars) { | |
36 if (str.empty()) | |
37 return false; | |
38 | |
39 const auto found = str.find_first_not_of(chars); | |
40 | |
41 if (found == 0) | |
42 return false; | |
43 | |
44 if (found == std::string::npos) | |
45 str.clear(); | |
46 else | |
47 str.erase(0, found); | |
48 | |
49 return true; | |
50 } | |
51 | |
52 bool TrimRight(std::string& str, const char* chars) { | |
53 if (str.empty()) | |
54 return false; | |
55 | |
56 const auto found = str.find_last_not_of(chars); | |
57 | |
58 if (found == str.size() - 1) | |
59 return false; | |
60 | |
61 if (found == std::string::npos) | |
62 str.clear(); | |
63 else | |
64 str.resize(found + 1); | |
65 | |
66 return true; | |
67 } | |
68 | |
69 } // namespace anisthesia::detail::util |