From c9522fb740200ccef6230cec452c48efb31e5394 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 11 May 2023 22:05:17 +0200 Subject: Removed all Printf-family functions from StringUtils. Replaced them with fmt::format calls, including changes to the format strings. Also changed the format strings to use FMT_STRING, so that the format is checked compile-time against the arguments. Also fixed code-style violations already present in the code. --- src/WorldStorage/FireworksSerializer.cpp | 12 ++++-------- src/WorldStorage/MapSerializer.cpp | 18 ++++++------------ src/WorldStorage/ScoreboardSerializer.cpp | 7 ++----- src/WorldStorage/WSSAnvil.cpp | 29 ++++++++++++++--------------- 4 files changed, 26 insertions(+), 40 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/FireworksSerializer.cpp b/src/WorldStorage/FireworksSerializer.cpp index fa411b4e9..f0fe7e48e 100644 --- a/src/WorldStorage/FireworksSerializer.cpp +++ b/src/WorldStorage/FireworksSerializer.cpp @@ -165,12 +165,10 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB AString cFireworkItem::ColoursToString(const cFireworkItem & a_FireworkItem) { AString Result; - - for (std::vector::const_iterator itr = a_FireworkItem.m_Colours.begin(); itr != a_FireworkItem.m_Colours.end(); ++itr) + for (const auto col: a_FireworkItem.m_Colours) { - AppendPrintf(Result, "%i;", *itr); + Result.append(fmt::format(FMT_STRING("{};"), col)); } - return Result; } @@ -200,12 +198,10 @@ void cFireworkItem::ColoursFromString(const AString & a_String, cFireworkItem & AString cFireworkItem::FadeColoursToString(const cFireworkItem & a_FireworkItem) { AString Result; - - for (std::vector::const_iterator itr = a_FireworkItem.m_FadeColours.begin(); itr != a_FireworkItem.m_FadeColours.end(); ++itr) + for (const auto col: a_FireworkItem.m_FadeColours) { - AppendPrintf(Result, "%i;", *itr); + Result.append(fmt::format(FMT_STRING("{};"), col)); } - return Result; } diff --git a/src/WorldStorage/MapSerializer.cpp b/src/WorldStorage/MapSerializer.cpp index 7e67d8059..e68ad00e6 100644 --- a/src/WorldStorage/MapSerializer.cpp +++ b/src/WorldStorage/MapSerializer.cpp @@ -17,11 +17,8 @@ cMapSerializer::cMapSerializer(const AString & a_WorldName, cMap * a_Map): m_Map(a_Map) { - AString DataPath; - Printf(DataPath, "%s%cdata", a_WorldName.c_str(), cFile::PathSeparator()); - - Printf(m_Path, "%s%cmap_%i.dat", DataPath.c_str(), cFile::PathSeparator(), a_Map->GetID()); - + auto DataPath = fmt::format(FMT_STRING("{}{}data"), a_WorldName, cFile::PathSeparator()); + m_Path = fmt::format(FMT_STRING("{}{}map_%i.dat"), DataPath, cFile::PathSeparator(), a_Map->GetID()); cFile::CreateFolder(DataPath); } @@ -29,7 +26,7 @@ cMapSerializer::cMapSerializer(const AString & a_WorldName, cMap * a_Map): -bool cMapSerializer::Load(void) +bool cMapSerializer::Load() { const auto Data = GZipFile::ReadRestOfFile(m_Path); const cParsedNBT NBT(Data.GetView()); @@ -171,11 +168,8 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT) cIDCountSerializer::cIDCountSerializer(const AString & a_WorldName) : m_MapCount(0) { - AString DataPath; - Printf(DataPath, "%s%cdata", a_WorldName.c_str(), cFile::PathSeparator()); - - Printf(m_Path, "%s%cidcounts.dat", DataPath.c_str(), cFile::PathSeparator()); - + auto DataPath = fmt::format(FMT_STRING("{}{}data"), a_WorldName, cFile::PathSeparator()); + m_Path = fmt::format(FMT_STRING("{}{}idcounts.dat"), DataPath, cFile::PathSeparator()); cFile::CreateFolder(DataPath); } @@ -183,7 +177,7 @@ cIDCountSerializer::cIDCountSerializer(const AString & a_WorldName) : m_MapCount -bool cIDCountSerializer::Load(void) +bool cIDCountSerializer::Load() { AString Data = cFile::ReadWholeFile(m_Path); if (Data.empty()) diff --git a/src/WorldStorage/ScoreboardSerializer.cpp b/src/WorldStorage/ScoreboardSerializer.cpp index 5e644aab9..e6e639bcb 100644 --- a/src/WorldStorage/ScoreboardSerializer.cpp +++ b/src/WorldStorage/ScoreboardSerializer.cpp @@ -16,11 +16,8 @@ cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScoreboard * a_ScoreBoard): m_ScoreBoard(a_ScoreBoard) { - AString DataPath; - Printf(DataPath, "%s%cdata", a_WorldName.c_str(), cFile::PathSeparator()); - + auto DataPath = fmt::format(FMT_STRING("{}{}data"), a_WorldName, cFile::PathSeparator()); m_Path = DataPath + cFile::PathSeparator() + "scoreboard.dat"; - cFile::CreateFolder(DataPath); } @@ -28,7 +25,7 @@ cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScore -bool cScoreboardSerializer::Load(void) +bool cScoreboardSerializer::Load() { try { diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index dfefb74d3..590647848 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -90,8 +90,7 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor) : m_Compressor(a_CompressionFactor) { // Create a level.dat file for mapping tools, if it doesn't already exist: - AString fnam; - Printf(fnam, "%s%clevel.dat", a_World->GetDataPath().c_str(), cFile::PathSeparator()); + auto fnam = fmt::format(FMT_STRING("{}{}level.dat"), a_World->GetDataPath(), cFile::PathSeparator()); if (!cFile::Exists(fnam)) { cFastNBTWriter Writer; @@ -182,8 +181,7 @@ bool cWSSAnvil::SaveChunk(const cChunkCoords & a_Chunk) void cWSSAnvil::ChunkLoadFailed(int a_ChunkX, int a_ChunkZ, const AString & a_Reason, const ContiguousByteBufferView a_ChunkDataToSave) { // Construct the filename for offloading: - AString OffloadFileName; - Printf(OffloadFileName, "%s%cregion%cbadchunks", m_World->GetDataPath().c_str(), cFile::PathSeparator(), cFile::PathSeparator()); + auto OffloadFileName = fmt::format(FMT_STRING("{0}{1}region{1}badchunks"), m_World->GetDataPath(), cFile::PathSeparator()); cFile::CreateFolder(OffloadFileName); auto t = time(nullptr); struct tm stm; @@ -192,24 +190,26 @@ void cWSSAnvil::ChunkLoadFailed(int a_ChunkX, int a_ChunkZ, const AString & a_Re #else localtime_r(&t, &stm); #endif - AppendPrintf(OffloadFileName, "%cch.%d.%d.%d-%02d-%02d-%02d-%02d-%02d.dat", + OffloadFileName.append(fmt::format( + FMT_STRING("{}ch.{}.{}.{}-{:02d}-{:02d}-{:02d}-{:02d}-{:02d}.dat"), cFile::PathSeparator(), a_ChunkX, a_ChunkZ, stm.tm_year + 1900, stm.tm_mon + 1, stm.tm_mday, stm.tm_hour, stm.tm_min, stm.tm_sec - ); + )); // Log the warning to console: const int RegionX = FAST_FLOOR_DIV(a_ChunkX, 32); const int RegionZ = FAST_FLOOR_DIV(a_ChunkZ, 32); - AString Info = Printf("Loading chunk [%d, %d] for world %s from file r.%d.%d.mca failed: %s Offloading old chunk data to file %s and regenerating chunk.", - a_ChunkX, a_ChunkZ, m_World->GetName().c_str(), RegionX, RegionZ, a_Reason.c_str(), OffloadFileName.c_str() + auto Info = fmt::format( + FMT_STRING("Loading chunk [{}, {}] for world {} from file r.{}.{}.mca failed: {} Offloading old chunk data to file {} and regenerating chunk."), + a_ChunkX, a_ChunkZ, m_World->GetName(), RegionX, RegionZ, a_Reason, OffloadFileName ); - LOGWARNING("%s", Info.c_str()); + LOGWARNING("%s", Info); // Write the data: cFile f; if (!f.Open(OffloadFileName, cFile::fmWrite)) { - LOGWARNING("Cannot open file %s for writing! Old chunk data is lost.", OffloadFileName.c_str()); + LOGWARNING("Cannot open file %s for writing! Old chunk data is lost.", OffloadFileName); return; } f.Write(a_ChunkDataToSave.data(), a_ChunkDataToSave.size()); @@ -218,7 +218,7 @@ void cWSSAnvil::ChunkLoadFailed(int a_ChunkX, int a_ChunkZ, const AString & a_Re // Write a description file: if (!f.Open(OffloadFileName + ".info", cFile::fmWrite)) { - LOGWARNING("Cannot open file %s.info for writing! The information about the failed chunk will not be written.", OffloadFileName.c_str()); + LOGWARNING("Cannot open file %s.info for writing! The information about the failed chunk will not be written.", OffloadFileName); return; } f.Write(Info.c_str(), Info.size()); @@ -288,10 +288,9 @@ cWSSAnvil::cMCAFile * cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) } // Load it anew: - AString FileName; - Printf(FileName, "%s%cregion", m_World->GetDataPath().c_str(), cFile::PathSeparator()); + auto FileName = fmt::format(FMT_STRING("{}{}region"), m_World->GetDataPath(), cFile::PathSeparator()); cFile::CreateFolder(FileName); - AppendPrintf(FileName, "/r.%d.%d.mca", RegionX, RegionZ); + FileName.append(fmt::format(FMT_STRING("/r.{}.{}.mca"), RegionX, RegionZ)); cMCAFile * f = new cMCAFile(*this, FileName, RegionX, RegionZ); if (f == nullptr) { @@ -4011,7 +4010,7 @@ bool cWSSAnvil::cMCAFile::GetChunkData(const cChunkCoords & a_Chunk, ContiguousB if (CompressionType != 2) { // Chunk is in an unknown compression - m_ParentSchema.ChunkLoadFailed(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ, Printf("Unknown chunk compression: %d", CompressionType), a_Data); + m_ParentSchema.ChunkLoadFailed(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ, fmt::format(FMT_STRING("Unknown chunk compression: {}"), CompressionType), a_Data); return false; } return true; -- cgit v1.2.3