replace fstream with fmt::print

This commit is contained in:
Henny022p 2021-11-27 08:32:17 +01:00
parent 9cc2fcf9ea
commit 25c9367d17
9 changed files with 33 additions and 37 deletions

View File

@ -1,8 +1,7 @@
#include "animation.h" #include "animation.h"
#include "reader.h" #include "reader.h"
#include <fstream>
#include <iostream>
#include <fmt/format.h> #include <fmt/format.h>
#include <util/file.h>
void AnimationAsset::convertToHumanReadable(const std::vector<char>& baserom) { void AnimationAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size); Reader reader(baserom, start, size);
@ -29,9 +28,8 @@ void AnimationAsset::convertToHumanReadable(const std::vector<char>& baserom) {
u8 keyframe_count = reader.read_u8(); u8 keyframe_count = reader.read_u8();
lines.push_back(fmt::format("\t.byte {} @ keyframe count\n", keyframe_count)); lines.push_back(fmt::format("\t.byte {} @ keyframe count\n", keyframe_count));
} }
std::ofstream out(assetPath); auto file = util::open_file(assetPath, "w");
for (const auto& line : lines) { for (const auto& line : lines) {
out << line; std::fputs(line.c_str(), file.get());
} }
out.close();
} }

View File

@ -2,6 +2,6 @@
#include <util/file.h> #include <util/file.h>
void BaseAsset::extractBinary(const std::vector<char>& baserom) { void BaseAsset::extractBinary(const std::vector<char>& baserom) {
auto file = util::open_file(path.string(), "w"); auto file = util::open_file(path, "w");
std::fwrite(baserom.data() + start, 1, static_cast<size_t>(size), file.get()); std::fwrite(baserom.data() + start, 1, static_cast<size_t>(size), file.get());
} }

View File

@ -1,8 +1,7 @@
#include "exitlist.h" #include "exitlist.h"
#include "reader.h" #include "reader.h"
#include <fstream>
#include <iostream>
#include <fmt/format.h> #include <fmt/format.h>
#include <util/file.h>
void ExitListAsset::convertToHumanReadable(const std::vector<char>& baserom) { void ExitListAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size); Reader reader(baserom, start, size);
@ -41,9 +40,8 @@ void ExitListAsset::convertToHumanReadable(const std::vector<char>& baserom) {
lines.emplace_back("\n"); lines.emplace_back("\n");
} }
std::ofstream out(assetPath); auto file = util::open_file(assetPath, "w");
for (const auto& line : lines) { for (const auto& line : lines) {
out << line; std::fputs(line.c_str(), file.get());
} }
out.close();
} }

View File

@ -1,9 +1,8 @@
#include "frameobjlists.h" #include "frameobjlists.h"
#include "reader.h" #include "reader.h"
#include <algorithm> #include <algorithm>
#include <fstream>
#include <iostream>
#include <fmt/format.h> #include <fmt/format.h>
#include <util/file.h>
void FrameObjListsAsset::convertToHumanReadable(const std::vector<char>& baserom) { void FrameObjListsAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size); Reader reader(baserom, start, size);
@ -73,9 +72,8 @@ void FrameObjListsAsset::convertToHumanReadable(const std::vector<char>& baserom
} }
} }
std::ofstream out(assetPath); auto file = util::open_file(assetPath, "w");
for (const auto& line : lines) { for (const auto& line : lines) {
out << line; std::fputs(line.c_str(), file.get());
} }
out.close();
} }

View File

@ -1,5 +1,6 @@
#include "macroasm.h" #include "macroasm.h"
#include <fstream> #include <fmt/format.h>
#include <util/file.h>
std::filesystem::path BaseMacroAsmAsset::generateAssetPath() { std::filesystem::path BaseMacroAsmAsset::generateAssetPath() {
std::filesystem::path asset_path = path; std::filesystem::path asset_path = path;
@ -14,7 +15,6 @@ std::filesystem::path BaseMacroAsmAsset::generateBuildPath() {
void BaseMacroAsmAsset::extractBinary(const std::vector<char>& baserom) { void BaseMacroAsmAsset::extractBinary(const std::vector<char>& baserom) {
BaseAsset::extractBinary(baserom); BaseAsset::extractBinary(baserom);
// Create dummy .s file that just incbins the .bin file. // Create dummy .s file that just incbins the .bin file.
std::ofstream out(assetPath); auto file = util::open_file(assetPath, "w");
out << "\t.incbin " << path << "\n"; fmt::print(file.get(), "\t.incbin \"{}\"\n", path.native());
out.close();
} }

View File

@ -2,11 +2,9 @@
#include "reader.h" #include "reader.h"
#include "util.h" #include "util.h"
#include <filesystem> #include <filesystem>
#include <iostream>
#include <fstream>
#include <util/file.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fmt/format.h> #include <fmt/format.h>
#include <util/file.h>
extern std::string gBaseromPath; extern std::string gBaseromPath;
@ -33,21 +31,20 @@ void MidiAsset::extractBinary(const std::vector<char>& baserom) {
// Extract tracks // Extract tracks
{ {
auto file = util::open_file(tracksPath.string(), "w"); auto file = util::open_file(tracksPath, "w");
std::fwrite(baserom.data() + start, 1, static_cast<size_t>(headerOffset), file.get()); std::fwrite(baserom.data() + start, 1, static_cast<size_t>(headerOffset), file.get());
} }
// Extract header // Extract header
{ {
auto file = util::open_file(headerPath.string(), "w"); auto file = util::open_file(headerPath, "w");
std::fwrite(baserom.data() + start + headerOffset, 1, static_cast<size_t>(size - headerOffset), file.get()); std::fwrite(baserom.data() + start + headerOffset, 1, static_cast<size_t>(size - headerOffset), file.get());
} }
// Create dummy .s file. // Create dummy .s file.
std::ofstream out(buildPath); auto file = util::open_file(buildPath, "w");
out << "\t.incbin " << tracksPath << "\n"; fmt::print(file.get(), "\t.incbin \"{}\"\n", tracksPath.native());
out << label << "::\n"; fmt::print(file.get(), "{}::\n", label);
out << "\t.incbin " << headerPath << "\n"; fmt::print(file.get(), "\t.incbin \"{}\"\n", headerPath.native());
out.close();
} }
void MidiAsset::parseOptions(std::vector<std::string>& commonParams, std::vector<std::string>& agb2midParams) { void MidiAsset::parseOptions(std::vector<std::string>& commonParams, std::vector<std::string>& agb2midParams) {

View File

@ -1,8 +1,7 @@
#include "spriteframe.h" #include "spriteframe.h"
#include "reader.h" #include "reader.h"
#include <fstream>
#include <iostream>
#include <fmt/format.h> #include <fmt/format.h>
#include <util/file.h>
void SpriteFrameAsset::convertToHumanReadable(const std::vector<char>& baserom) { void SpriteFrameAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size); Reader reader(baserom, start, size);
@ -18,9 +17,8 @@ void SpriteFrameAsset::convertToHumanReadable(const std::vector<char>& baserom)
lines.emplace_back("\n"); lines.emplace_back("\n");
} }
std::ofstream out(assetPath); auto file = util::open_file(assetPath, "w");
for (const auto& line : lines) { for (const auto& line : lines) {
out << line; std::fputs(line.c_str(), file.get());
} }
out.close();
} }

View File

@ -9,7 +9,7 @@ class OffsetCalculator {
public: public:
OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_); OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_);
void addAsset(int start, const std::string& symbol); void addAsset(int start, const std::string& symbol);
int getLastEnd() const { [[nodiscard]] int getLastEnd() const {
return lastEnd; return lastEnd;
} }
void setLastEnd(int lastEnd_) { void setLastEnd(int lastEnd_) {

View File

@ -3,7 +3,9 @@
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
#include <string>
#include <string_view> #include <string_view>
namespace util { namespace util {
namespace { namespace {
auto file_ptr_deleter = [](std::FILE* file) { std::fclose(file); }; auto file_ptr_deleter = [](std::FILE* file) { std::fclose(file); };
@ -11,8 +13,13 @@ auto file_ptr_deleter = [](std::FILE* file) { std::fclose(file); };
using file_ptr_t = std::unique_ptr<std::FILE, decltype(file_ptr_deleter)>; using file_ptr_t = std::unique_ptr<std::FILE, decltype(file_ptr_deleter)>;
file_ptr_t open_file(std::string_view filename, std::string_view mode) { inline file_ptr_t open_file(std::string_view filename, std::string_view mode) {
return { std::fopen(filename.data(), mode.data()), file_ptr_deleter }; return { std::fopen(filename.data(), mode.data()), file_ptr_deleter };
} }
inline file_ptr_t open_file(const std::string& filename, std::string_view mode) {
return { std::fopen(filename.c_str(), mode.data()), file_ptr_deleter };
}
} // namespace util } // namespace util
#endif // TOOLS_FILE_H #endif // TOOLS_FILE_H