Skip to content

Commit

Permalink
Switch from fmtlib to std::format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed May 21, 2024
1 parent d7ece5c commit cd200d3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 54 deletions.
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cmake_minimum_required(VERSION 3.16)

add_library(archive SHARED)
mo2_configure_library(archive
WARNINGS OFF PRIVATE_DEPENDS 7z fmt)
mo2_configure_library(archive WARNINGS OFF PRIVATE_DEPENDS 7z)
target_compile_definitions(archive PRIVATE -DMODORGANIZER_ARCHIVE_BUILDING)
mo2_install_target(archive)
14 changes: 7 additions & 7 deletions src/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ ArchiveImpl::ArchiveImpl()
m_Valid = true;
return;
} catch (std::exception const& e) {
m_LogCallback(LogLevel::Error, fmt::format(L"Caught exception {}.", e));
m_LogCallback(LogLevel::Error, std::format(L"Caught exception {}.", e));
m_LastError = Error::ERROR_LIBRARY_INVALID;
}
}
Expand Down Expand Up @@ -371,12 +371,12 @@ bool ArchiveImpl::open(std::wstring const& archiveName,

if (m_ArchivePtr->Open(file, 0, openCallbackPtr) != S_OK) {
m_LogCallback(LogLevel::Debug,
fmt::format(L"Failed to open {} using {} (from signature).",
std::format(L"Failed to open {} using {} (from signature).",
archiveName, signatureInfo.second.m_Name));
m_ArchivePtr.Release();
} else {
m_LogCallback(LogLevel::Debug,
fmt::format(L"Opened {} using {} (from signature).",
std::format(L"Opened {} using {} (from signature).",
archiveName, signatureInfo.second.m_Name));

// Retrieve the extension (warning: .extension() contains the dot):
Expand Down Expand Up @@ -433,12 +433,12 @@ bool ArchiveImpl::open(std::wstring const& archiveName,

if (m_ArchivePtr->Open(file, 0, openCallbackPtr) != S_OK) {
m_LogCallback(LogLevel::Debug,
fmt::format(L"Failed to open {} using {} (from signature).",
std::format(L"Failed to open {} using {} (from signature).",
archiveName, format.m_Name));
m_ArchivePtr.Release();
} else {
m_LogCallback(LogLevel::Debug,
fmt::format(L"Opened {} using {} (from signature).",
std::format(L"Opened {} using {} (from signature).",
archiveName, format.m_Name));
break;
}
Expand All @@ -457,7 +457,7 @@ bool ArchiveImpl::open(std::wstring const& archiveName,
}
m_LogCallback(
LogLevel::Warning,
fmt::format(L"The format(s) expected for this extension are: {}.",
std::format(L"The format(s) expected for this extension are: {}.",
ArchiveStrings::join(vformats, L", ")));
}
}
Expand All @@ -478,7 +478,7 @@ bool ArchiveImpl::open(std::wstring const& archiveName,
}
if (m_ArchivePtr->Open(file, 0, openCallbackPtr) == S_OK) {
m_LogCallback(LogLevel::Debug,
fmt::format(L"Opened {} using {} (from signature).", archiveName,
std::format(L"Opened {} using {} (from signature).", archiveName,
format.m_Name));
m_LogCallback(LogLevel::Warning,
L"This archive likely has an incorrect extension.");
Expand Down
20 changes: 9 additions & 11 deletions src/extractcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <Unknwn.h>

#include <fmt/format.h>
#include <fmt/xchar.h>
#include <filesystem>
#include <format>
#include <stdexcept>
#include <string>

#include "archive.h"
#include "extractcallback.h"
#include "propertyvariant.h"

#include <filesystem>
#include <stdexcept>
#include <string>

std::wstring operationResultToString(Int32 operationResult)
{
namespace R = NArchive::NExtract::NOperationResult;
Expand Down Expand Up @@ -67,7 +65,7 @@ std::wstring operationResultToString(Int32 operationResult)
return L"Wrong password";

default:
return fmt::format(L"Unknown error {}", operationResult);
return std::format(L"Unknown error {}", operationResult);
}
}

Expand Down Expand Up @@ -127,7 +125,7 @@ bool CArchiveExtractCallback::getOptionalProperty(UInt32 index, int property,
PropertyVariant prop;
if (m_ArchiveHandler->GetProperty(index, property, &prop) != S_OK) {
m_LogCallback(Archive::LogLevel::Error,
fmt::format(L"Error getting property {}.", property));
std::format(L"Error getting property {}.", property));
return false;
}
if (prop.is_empty()) {
Expand All @@ -143,7 +141,7 @@ bool CArchiveExtractCallback::getProperty(UInt32 index, int property, T* result)
PropertyVariant prop;
if (m_ArchiveHandler->GetProperty(index, property, &prop) != S_OK) {
m_LogCallback(Archive::LogLevel::Error,
fmt::format(L"Error getting property {}.", property));
std::format(L"Error getting property {}.", property));
return false;
}

Expand Down Expand Up @@ -243,7 +241,7 @@ STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index,
auto fileSizeFound = getOptionalProperty(index, kpidSize, &fileSize);
if (fileSizeFound && m_OutputFileStream->SetSize(fileSize) != S_OK) {
m_LogCallback(Archive::LogLevel::Error,
fmt::format(L"SetSize() failed on {}.", m_FullProcessedPaths[0]));
std::format(L"SetSize() failed on {}.", m_FullProcessedPaths[0]));
}

// This is messy but I can't find another way of doing it. A simple
Expand All @@ -260,7 +258,7 @@ STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index,
return S_OK;
} catch (std::exception const& e) {
m_LogCallback(Archive::LogLevel::Error,
fmt::format(L"Caught exception {} in GetStream.", e));
std::format(L"Caught exception {} in GetStream.", e));
}
return E_FAIL;
}
Expand Down
8 changes: 3 additions & 5 deletions src/extractcallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <atomic>
#include <chrono>
#include <filesystem>
#include <format>

#include "7zip/Archive/IArchive.h"
#include "7zip/IPassword.h"

#include <atlbase.h>

#include <fmt/format.h>
#include <fmt/xchar.h>

#include "archive.h"
#include "formatter.h"
#include "instrument.h"
Expand Down Expand Up @@ -73,9 +71,9 @@ class CArchiveExtractCallback : public IArchiveExtractCallback,
void reportError(const std::wstring& message);

template <class... Args>
void reportError(const wchar_t* format, Args&&... args)
void reportError(std::wformat_string<Args...> format, Args&&... args)
{
reportError(fmt::format(format, std::forward<Args>(args)...));
reportError(std::format(format, std::forward<Args>(args)...));
}

template <typename T>
Expand Down
37 changes: 15 additions & 22 deletions src/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,55 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// exposed outside of the library. It also contains some useful methods for
// string manipulation.

#include <fmt/format.h>
#include <format>

#include <cwctype>
#include <filesystem>
#include <stdexcept>
#include <string>

// Specializing fmt::formatter works, but gives warning, for whatever reason... So
// putting everything in the namespace.
namespace fmt
{

// I don't know why fmt does not provide this...
template <>
struct formatter<std::string, wchar_t> : formatter<std::wstring, wchar_t>
struct std::formatter<std::string, wchar_t> : std::formatter<std::wstring, wchar_t>
{
template <typename FormatContext>
auto format(std::string const& s, FormatContext& ctx)
auto format(std::string const& s, FormatContext& ctx) const
{
return formatter<std::wstring, wchar_t>::format(std::wstring(s.begin(), s.end()),
ctx);
return std::formatter<std::wstring, wchar_t>::format(
std::wstring(s.begin(), s.end()), ctx);
}
};

template <>
struct formatter<std::exception, wchar_t> : formatter<std::string, wchar_t>
struct std::formatter<std::exception, wchar_t> : std::formatter<std::string, wchar_t>
{
template <typename FormatContext>
auto format(std::exception const& ex, FormatContext& ctx)
auto format(std::exception const& ex, FormatContext& ctx) const
{
return formatter<std::string, wchar_t>::format(ex.what(), ctx);
return std::formatter<std::string, wchar_t>::format(ex.what(), ctx);
}
};

template <>
struct formatter<std::error_code, wchar_t> : formatter<std::string, wchar_t>
struct std::formatter<std::error_code, wchar_t> : std::formatter<std::string, wchar_t>
{
template <typename FormatContext>
auto format(std::error_code const& ec, FormatContext& ctx)
auto format(std::error_code const& ec, FormatContext& ctx) const
{
return formatter<std::string, wchar_t>::format(ec.message(), ctx);
return std::formatter<std::string, wchar_t>::format(ec.message(), ctx);
}
};

template <>
struct formatter<std::filesystem::path, wchar_t> : formatter<std::wstring, wchar_t>
struct std::formatter<std::filesystem::path, wchar_t>
: std::formatter<std::wstring, wchar_t>
{
template <typename FormatContext>
auto format(std::filesystem::path const& path, FormatContext& ctx)
auto format(std::filesystem::path const& path, FormatContext& ctx) const
{
return formatter<std::wstring, wchar_t>::format(path.native(), ctx);
return std::formatter<std::wstring, wchar_t>::format(path.native(), ctx);
}
};

} // namespace fmt

namespace ArchiveStrings
{

Expand Down
5 changes: 2 additions & 3 deletions src/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#define ARCHIVE_INSTRUMENT_H

#include <chrono>

#include <fmt/format.h>
#include <format>

namespace ArchiveTimers
{
Expand Down Expand Up @@ -87,7 +86,7 @@ class Timer
auto ms = [](auto&& t) {
return std::chrono::duration<double, std::milli>(t);
};
return fmt::format(
return std::format(
L"Instrument '{}': {} calls, total of {}ms, {:.3f}ms per call on average.",
name, ncalls, ms(time).count(), ms(time).count() / ncalls);
}
Expand Down
6 changes: 2 additions & 4 deletions src/opencallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <atlbase.h>

#include <format>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include "fileio.h"

#include <fmt/format.h>
#include <fmt/xchar.h>

#define UNUSED(x)

CArchiveOpenCallback::CArchiveOpenCallback(Archive::PasswordCallback passwordCallback,
Expand Down Expand Up @@ -130,7 +128,7 @@ STDMETHODIMP CArchiveOpenCallback::GetProperty(PROPID propID, PROPVARIANT* value

default:
m_LogCallback(Archive::LogLevel::Warning,
fmt::format(L"Unexpected property {}.", propID));
std::format(L"Unexpected property {}.", propID));
}
return S_OK;
}
Expand Down

0 comments on commit cd200d3

Please sign in to comment.