diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-09-28 17:39:03 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-09-28 17:39:03 -0500 |
commit | 876c829512301e3f20161f05d7c193540e6d1710 (patch) | |
tree | 17cb3f1956d88ce87e1bcd980ea67f0592b0bed4 /src/bookmouse_time.cpp | |
parent | dac2e7507d0172e2a87ed5b2df9c320bc9717da6 (diff) |
Working through file picker.
Diffstat (limited to 'src/bookmouse_time.cpp')
-rw-r--r-- | src/bookmouse_time.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/bookmouse_time.cpp b/src/bookmouse_time.cpp new file mode 100644 index 0000000..c779168 --- /dev/null +++ b/src/bookmouse_time.cpp @@ -0,0 +1,70 @@ +#include "bookmouse_time.hpp" + +#include <vector> + +namespace bookmouse { + +using fud::FudStatus; +using fud::Result; + +TimeFormat::TimeFormat(const char* format) : m_format{format}, m_sizeNeeded{m_format.length()} +{ + if (m_format.utf8Valid()) { + m_utf8Valid = true; + } +} + +TimeFormat::TimeFormat(const fud::String& format) : m_format{format}, m_sizeNeeded{m_format.length()} +{ + if (m_format.utf8Valid()) { + m_utf8Valid = true; + } +} + +fud::Result<fud::String, fud::FudStatus> TimeFormat::format(const TimeInfo& timeInfo) +{ + using RetType = fud::Result<fud::String, fud::FudStatus>; + + if (!m_utf8Valid || m_format.length() < 1) { + return RetType::error(FudStatus::ObjectInvalid); + } + + std::vector<char> output; + size_t resultSize = 0; + constexpr size_t maxSize = 1024; + while (resultSize == 0 && m_sizeNeeded <= maxSize) { + output.resize(m_sizeNeeded); + +#if defined __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" +#elif defined __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif + + resultSize = std::strftime(output.data(), output.size(), m_format.c_str(), &timeInfo); + +#if defined __clang__ +#pragma clang diagnostic pop +#elif defined __GNUC__ +#pragma GCC diagnostic pop +#endif + + + if (resultSize == 0) { + if (m_sizeNeeded <= 512) { + m_sizeNeeded *= 2; + } else { + m_sizeNeeded = maxSize; + } + } + } + if (resultSize == 0) { + return RetType::error(FudStatus::Failure); + } + + return RetType::okay(fud::String(output.data())); +} + +} // namespace bookmouse |