diff options
Diffstat (limited to 'core/utils')
| -rw-r--r-- | core/utils/epoch.cc | 78 | ||||
| -rw-r--r-- | core/utils/epoch.hh | 30 | ||||
| -rw-r--r-- | core/utils/physfs.cc | 152 | ||||
| -rw-r--r-- | core/utils/physfs.hh | 28 | ||||
| -rw-r--r-- | core/utils/string.cc | 114 | ||||
| -rw-r--r-- | core/utils/string.hh | 34 |
6 files changed, 218 insertions, 218 deletions
diff --git a/core/utils/epoch.cc b/core/utils/epoch.cc index 36bdb79..be5d4bd 100644 --- a/core/utils/epoch.cc +++ b/core/utils/epoch.cc @@ -1,39 +1,39 @@ -#include "core/pch.hh" - -#include "core/utils/epoch.hh" - -std::uint64_t utils::unix_seconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count()); -} - -std::uint64_t utils::unix_milliseconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()); -} - -std::uint64_t utils::unix_microseconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count()); -} - -std::int64_t utils::signed_unix_seconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count()); -} - -std::int64_t utils::signed_unix_milliseconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()); -} - -std::int64_t utils::signed_unix_microseconds(void) -{ - const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); - return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count()); -} +#include "core/pch.hh"
+
+#include "core/utils/epoch.hh"
+
+std::uint64_t utils::unix_seconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count());
+}
+
+std::uint64_t utils::unix_milliseconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
+}
+
+std::uint64_t utils::unix_microseconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count());
+}
+
+std::int64_t utils::signed_unix_seconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count());
+}
+
+std::int64_t utils::signed_unix_milliseconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
+}
+
+std::int64_t utils::signed_unix_microseconds(void)
+{
+ const auto elapsed = std::chrono::system_clock::now().time_since_epoch();
+ return static_cast<std::int64_t>(std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count());
+}
diff --git a/core/utils/epoch.hh b/core/utils/epoch.hh index 4bf5460..1df564c 100644 --- a/core/utils/epoch.hh +++ b/core/utils/epoch.hh @@ -1,15 +1,15 @@ -#pragma once - -namespace utils -{ -std::uint64_t unix_seconds(void); -std::uint64_t unix_milliseconds(void); -std::uint64_t unix_microseconds(void); -} // namespace utils - -namespace utils -{ -std::int64_t signed_unix_seconds(void); -std::int64_t signed_unix_milliseconds(void); -std::int64_t signed_unix_microseconds(void); -} // namespace utils +#pragma once
+
+namespace utils
+{
+std::uint64_t unix_seconds(void);
+std::uint64_t unix_milliseconds(void);
+std::uint64_t unix_microseconds(void);
+} // namespace utils
+
+namespace utils
+{
+std::int64_t signed_unix_seconds(void);
+std::int64_t signed_unix_milliseconds(void);
+std::int64_t signed_unix_microseconds(void);
+} // namespace utils
diff --git a/core/utils/physfs.cc b/core/utils/physfs.cc index cb310df..b801963 100644 --- a/core/utils/physfs.cc +++ b/core/utils/physfs.cc @@ -1,76 +1,76 @@ -#include "core/pch.hh" - -#include "core/utils/physfs.hh" - -bool utils::read_file(std::string_view path, std::vector<std::byte>& buffer) -{ - auto file = PHYSFS_openRead(std::string(path).c_str()); - - if(file == nullptr) { - spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - return false; - } - - PHYSFS_sint64 file_size = PHYSFS_fileLength(file); - buffer.resize(static_cast<std::size_t>(file_size)); - - PHYSFS_readBytes(file, buffer.data(), file_size); - PHYSFS_close(file); - - return true; -} - -bool utils::read_file(std::string_view path, std::string& buffer) -{ - auto file = PHYSFS_openRead(std::string(path).c_str()); - - if(file == nullptr) { - spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - return false; - } - - PHYSFS_sint64 file_size = PHYSFS_fileLength(file); - buffer.resize(static_cast<std::size_t>(file_size)); - - PHYSFS_readBytes(file, buffer.data(), file_size); - PHYSFS_close(file); - - return true; -} - -bool utils::write_file(std::string_view path, const std::vector<std::byte>& buffer) -{ - auto file = PHYSFS_openWrite(std::string(path).c_str()); - - if(file == nullptr) { - spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - return false; - } - - PHYSFS_writeBytes(file, buffer.data(), static_cast<PHYSFS_uint64>(buffer.size())); - PHYSFS_close(file); - - return true; -} - -bool utils::write_file(std::string_view path, const std::string& buffer) -{ - auto file = PHYSFS_openWrite(std::string(path).c_str()); - - if(file == nullptr) { - spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - return false; - } - - PHYSFS_writeBytes(file, buffer.data(), static_cast<PHYSFS_uint64>(buffer.size())); - PHYSFS_close(file); - - return true; -} - -std::string_view utils::physfs_error(void) -{ - auto error_code = PHYSFS_getLastErrorCode(); - auto error_string = PHYSFS_getErrorByCode(error_code); - return std::string_view(error_string, std::strlen(error_string)); -} +#include "core/pch.hh"
+
+#include "core/utils/physfs.hh"
+
+bool utils::read_file(std::string_view path, std::vector<std::byte>& buffer)
+{
+ auto file = PHYSFS_openRead(std::string(path).c_str());
+
+ if(file == nullptr) {
+ spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return false;
+ }
+
+ PHYSFS_sint64 file_size = PHYSFS_fileLength(file);
+ buffer.resize(static_cast<std::size_t>(file_size));
+
+ PHYSFS_readBytes(file, buffer.data(), file_size);
+ PHYSFS_close(file);
+
+ return true;
+}
+
+bool utils::read_file(std::string_view path, std::string& buffer)
+{
+ auto file = PHYSFS_openRead(std::string(path).c_str());
+
+ if(file == nullptr) {
+ spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return false;
+ }
+
+ PHYSFS_sint64 file_size = PHYSFS_fileLength(file);
+ buffer.resize(static_cast<std::size_t>(file_size));
+
+ PHYSFS_readBytes(file, buffer.data(), file_size);
+ PHYSFS_close(file);
+
+ return true;
+}
+
+bool utils::write_file(std::string_view path, const std::vector<std::byte>& buffer)
+{
+ auto file = PHYSFS_openWrite(std::string(path).c_str());
+
+ if(file == nullptr) {
+ spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return false;
+ }
+
+ PHYSFS_writeBytes(file, buffer.data(), static_cast<PHYSFS_uint64>(buffer.size()));
+ PHYSFS_close(file);
+
+ return true;
+}
+
+bool utils::write_file(std::string_view path, const std::string& buffer)
+{
+ auto file = PHYSFS_openWrite(std::string(path).c_str());
+
+ if(file == nullptr) {
+ spdlog::error("physfs: {}: {}", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
+ return false;
+ }
+
+ PHYSFS_writeBytes(file, buffer.data(), static_cast<PHYSFS_uint64>(buffer.size()));
+ PHYSFS_close(file);
+
+ return true;
+}
+
+std::string_view utils::physfs_error(void)
+{
+ auto error_code = PHYSFS_getLastErrorCode();
+ auto error_string = PHYSFS_getErrorByCode(error_code);
+ return std::string_view(error_string, std::strlen(error_string));
+}
diff --git a/core/utils/physfs.hh b/core/utils/physfs.hh index 4af94dc..220954e 100644 --- a/core/utils/physfs.hh +++ b/core/utils/physfs.hh @@ -1,14 +1,14 @@ -#pragma once - -namespace utils -{ -bool read_file(std::string_view path, std::vector<std::byte>& buffer); -bool read_file(std::string_view path, std::string& buffer); -bool write_file(std::string_view path, const std::vector<std::byte>& buffer); -bool write_file(std::string_view path, const std::string& buffer); -} // namespace utils - -namespace utils -{ -std::string_view physfs_error(void); -} // namespace utils +#pragma once
+
+namespace utils
+{
+bool read_file(std::string_view path, std::vector<std::byte>& buffer);
+bool read_file(std::string_view path, std::string& buffer);
+bool write_file(std::string_view path, const std::vector<std::byte>& buffer);
+bool write_file(std::string_view path, const std::string& buffer);
+} // namespace utils
+
+namespace utils
+{
+std::string_view physfs_error(void);
+} // namespace utils
diff --git a/core/utils/string.cc b/core/utils/string.cc index dd3d567..4bdc073 100644 --- a/core/utils/string.cc +++ b/core/utils/string.cc @@ -1,57 +1,57 @@ -#include "core/pch.hh" - -#include "core/utils/string.hh" - -constexpr static const char* WHITESPACE_CHARS = " \t\r\n"; - -bool utils::is_whitespace(const std::string& string) -{ - if(string.find_first_not_of(WHITESPACE_CHARS) == std::string::npos) { - return true; - } - else if((string.size() == 1) && string[0] == 0x00) { - return true; - } - else { - return string.empty(); - } -} - -std::string utils::join(const std::vector<std::string>& strings, const std::string& separator) -{ - std::ostringstream stream; - for(const std::string& str : strings) - stream << str << separator; - return stream.str(); -} - -std::vector<std::string> utils::split(const std::string& string, const std::string& separator) -{ - std::size_t pos = 0; - std::size_t prev = 0; - std::vector<std::string> result; - - while((pos = string.find(separator, prev)) != std::string::npos) { - result.push_back(string.substr(prev, pos - prev)); - prev = pos + separator.length(); - } - - if(prev <= string.length()) { - result.push_back(string.substr(prev, string.length() - prev)); - } - - return result; -} - -std::string utils::trim_whitespace(const std::string& string) -{ - auto su = string.find_first_not_of(WHITESPACE_CHARS); - auto sv = string.find_last_not_of(WHITESPACE_CHARS); - - if(su == std::string::npos) { - return std::string(); - } - else { - return string.substr(su, sv - su + 1); - } -} +#include "core/pch.hh"
+
+#include "core/utils/string.hh"
+
+constexpr static const char* WHITESPACE_CHARS = " \t\r\n";
+
+bool utils::is_whitespace(const std::string& string)
+{
+ if(string.find_first_not_of(WHITESPACE_CHARS) == std::string::npos) {
+ return true;
+ }
+ else if((string.size() == 1) && string[0] == 0x00) {
+ return true;
+ }
+ else {
+ return string.empty();
+ }
+}
+
+std::string utils::join(const std::vector<std::string>& strings, const std::string& separator)
+{
+ std::ostringstream stream;
+ for(const std::string& str : strings)
+ stream << str << separator;
+ return stream.str();
+}
+
+std::vector<std::string> utils::split(const std::string& string, const std::string& separator)
+{
+ std::size_t pos = 0;
+ std::size_t prev = 0;
+ std::vector<std::string> result;
+
+ while((pos = string.find(separator, prev)) != std::string::npos) {
+ result.push_back(string.substr(prev, pos - prev));
+ prev = pos + separator.length();
+ }
+
+ if(prev <= string.length()) {
+ result.push_back(string.substr(prev, string.length() - prev));
+ }
+
+ return result;
+}
+
+std::string utils::trim_whitespace(const std::string& string)
+{
+ auto su = string.find_first_not_of(WHITESPACE_CHARS);
+ auto sv = string.find_last_not_of(WHITESPACE_CHARS);
+
+ if(su == std::string::npos) {
+ return std::string();
+ }
+ else {
+ return string.substr(su, sv - su + 1);
+ }
+}
diff --git a/core/utils/string.hh b/core/utils/string.hh index 827c3ca..0e896fd 100644 --- a/core/utils/string.hh +++ b/core/utils/string.hh @@ -1,17 +1,17 @@ -#pragma once - -namespace utils -{ -bool is_whitespace(const std::string& string); -} // namespace utils - -namespace utils -{ -std::string join(const std::vector<std::string>& strings, const std::string& separator); -std::vector<std::string> split(const std::string& string, const std::string& separator); -} // namespace utils - -namespace utils -{ -std::string trim_whitespace(const std::string& string); -} // namespace utils +#pragma once
+
+namespace utils
+{
+bool is_whitespace(const std::string& string);
+} // namespace utils
+
+namespace utils
+{
+std::string join(const std::vector<std::string>& strings, const std::string& separator);
+std::vector<std::string> split(const std::string& string, const std::string& separator);
+} // namespace utils
+
+namespace utils
+{
+std::string trim_whitespace(const std::string& string);
+} // namespace utils
|
