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 "reader.h"
#include <fstream>
#include <iostream>
#include <fmt/format.h>
#include <util/file.h>
void AnimationAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size);
@ -29,9 +28,8 @@ void AnimationAsset::convertToHumanReadable(const std::vector<char>& baserom) {
u8 keyframe_count = reader.read_u8();
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) {
out << line;
std::fputs(line.c_str(), file.get());
}
out.close();
}

View File

@ -2,6 +2,6 @@
#include <util/file.h>
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());
}

View File

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

View File

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

View File

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

View File

@ -1,8 +1,7 @@
#include "spriteframe.h"
#include "reader.h"
#include <fstream>
#include <iostream>
#include <fmt/format.h>
#include <util/file.h>
void SpriteFrameAsset::convertToHumanReadable(const std::vector<char>& baserom) {
Reader reader(baserom, start, size);
@ -18,9 +17,8 @@ void SpriteFrameAsset::convertToHumanReadable(const std::vector<char>& baserom)
lines.emplace_back("\n");
}
std::ofstream out(assetPath);
auto file = util::open_file(assetPath, "w");
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:
OffsetCalculator(const std::filesystem::path& offsetsFile, int baseOffset_);
void addAsset(int start, const std::string& symbol);
int getLastEnd() const {
[[nodiscard]] int getLastEnd() const {
return lastEnd;
}
void setLastEnd(int lastEnd_) {

View File

@ -3,7 +3,9 @@
#include <cstdio>
#include <memory>
#include <string>
#include <string_view>
namespace util {
namespace {
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)>;
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 };
}
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
#endif // TOOLS_FILE_H