diff 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
line wrap: on
line diff
--- a/dep/animia/src/util.cc	Fri Nov 10 13:52:47 2023 -0500
+++ b/dep/animia/src/util.cc	Sun Nov 12 04:53:19 2023 -0500
@@ -0,0 +1,69 @@
+#include <algorithm>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <cctype>
+
+#include "animia/util.h"
+
+namespace animia::internal::util {
+
+bool ReadFile(const std::string& path, std::string& data) {
+	std::ifstream file(path.c_str(), std::ios::in | std::ios::binary);
+
+	if (!file)
+		return false;
+
+	file.seekg(0, std::ios::end);
+	data.resize(static_cast<size_t>(file.tellg()));
+	file.seekg(0, std::ios::beg);
+
+	file.read(&data.front(), data.size());
+	file.close();
+
+	return true;
+}
+
+bool EqualStrings(const std::string& str1, const std::string& str2) {
+	auto equal_chars = [](const char c1, const char c2) -> bool {
+		return std::tolower(static_cast<unsigned char>(c1)) == std::tolower(static_cast<unsigned char>(c2));
+	};
+
+	return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), equal_chars);
+}
+
+bool TrimLeft(std::string& str, const char* chars) {
+	if (str.empty())
+		return false;
+
+	const auto found = str.find_first_not_of(chars);
+
+	if (found == 0)
+		return false;
+
+	if (found == std::string::npos)
+		str.clear();
+	else
+		str.erase(0, found);
+
+	return true;
+}
+
+bool TrimRight(std::string& str, const char* chars) {
+	if (str.empty())
+		return false;
+
+	const auto found = str.find_last_not_of(chars);
+
+	if (found == str.size() - 1)
+		return false;
+
+	if (found == std::string::npos)
+		str.clear();
+	else
+		str.resize(found + 1);
+
+	return true;
+}
+
+} // namespace anisthesia::detail::util