From 876c829512301e3f20161f05d7c193540e6d1710 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 28 Sep 2024 17:39:03 -0500 Subject: Working through file picker. --- src/bookmouse_time.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/bookmouse_time.cpp (limited to 'src/bookmouse_time.cpp') 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 + +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 TimeFormat::format(const TimeInfo& timeInfo) +{ + using RetType = fud::Result; + + if (!m_utf8Valid || m_format.length() < 1) { + return RetType::error(FudStatus::ObjectInvalid); + } + + std::vector 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 -- cgit v1.2.3