Replace FileAPI with c++ streams and std::filesystem

This commit is contained in:
Jan
2021-03-03 14:04:35 +01:00
parent b6b0a57232
commit 1cd06668e0
96 changed files with 1355 additions and 1061 deletions

View File

@ -1,10 +1,10 @@
#pragma once
#include "IAssetDumper.h"
#include "Utils/FileAPI.h"
#include "Utils/PathUtils.h"
#include <cstdio>
#include <fstream>
#include <filesystem>
#include <iostream>
template<class T>
class AbstractAssetDumper : public IAssetDumper<T>
@ -12,7 +12,7 @@ class AbstractAssetDumper : public IAssetDumper<T>
protected:
virtual bool ShouldDump(XAssetInfo<T>* asset) = 0;
virtual std::string GetFileNameForAsset(Zone* zone, XAssetInfo<T>* asset) = 0;
virtual void DumpAsset(Zone* zone, XAssetInfo<T>* asset, FileAPI::File* out) = 0;
virtual void DumpAsset(Zone* zone, XAssetInfo<T>* asset, std::ostream& stream) = 0;
public:
void DumpPool(Zone* zone, AssetPool<T>* pool, const std::string& basePath) override
@ -25,21 +25,23 @@ public:
continue;
}
std::string assetFilePath = utils::Path::Combine(basePath, GetFileNameForAsset(zone, assetInfo));
std::filesystem::path assetFilePath(basePath);
assetFilePath.append(GetFileNameForAsset(zone, assetInfo));
FileAPI::DirectoryCreate(utils::Path::GetDirectory(assetFilePath));
auto assetFileFolder(assetFilePath);
assetFileFolder.replace_filename("");
create_directories(assetFileFolder);
auto file = FileAPI::Open(assetFilePath, FileAPI::Mode::MODE_WRITE);
if(file.IsOpen())
std::ofstream file(assetFilePath, std::fstream::out | std::fstream::binary);
if(file.is_open())
{
DumpAsset(zone, assetInfo, &file);
DumpAsset(zone, assetInfo, file);
file.Close();
file.close();
}
else
{
printf("Failed to open file '%s' to dump asset '%s'\n", assetFilePath.c_str(), assetInfo->m_name.c_str());
std::cout << "Failed to open file '" << assetFilePath.string() << "' to dump asset '" << assetInfo->m_name.c_str() << "'\n";
}
}
}

View File

@ -2,23 +2,21 @@
#include <sstream>
const std::string CsvWriter::LINE_BREAK = "\n";
CsvWriter::CsvWriter(FileAPI::IFile* file)
CsvWriter::CsvWriter(std::ostream& stream)
: m_stream(stream),
m_column_count(0),
m_current_column(0),
m_first_row(true)
{
m_file = file;
m_first_row = true;
m_current_column = 0;
m_column_count = 0;
}
void CsvWriter::WriteColumn(const std::string& value)
{
if (m_current_column++ > 0)
m_file->Printf(",");
m_stream << ",";
bool containsSeparator = false;
bool containsQuote = false;
auto containsSeparator = false;
auto containsQuote = false;
for (const auto& c : value)
{
if (c == '"')
@ -35,7 +33,7 @@ void CsvWriter::WriteColumn(const std::string& value)
{
std::ostringstream str;
for(const auto& c : value)
for (const auto& c : value)
{
if (c == '"')
str << "\"\"";
@ -43,15 +41,15 @@ void CsvWriter::WriteColumn(const std::string& value)
str << c;
}
m_file->Printf("\"%s\"", str.str().c_str());
m_stream << "\"" << str.str() << "\"";
}
else if (containsSeparator)
{
m_file->Printf("\"%s\"", value.c_str());
m_stream << "\"" << value << "\"";
}
else
{
m_file->Printf("%s", value.c_str());
m_stream << value;
}
}
@ -64,13 +62,13 @@ void CsvWriter::NextRow()
}
else
{
while(m_current_column < m_column_count)
while (m_current_column < m_column_count)
{
m_file->Printf(",");
m_stream << ",";
m_current_column++;
}
}
m_file->Printf("\n");
m_stream << "\n";
m_current_column = 0;
}

View File

@ -1,18 +1,19 @@
#pragma once
#include "Utils/FileAPI.h"
#include <string>
#include <ostream>
class CsvWriter
{
static constexpr char SEPARATOR = ',';
static const std::string LINE_BREAK;
FileAPI::IFile* m_file;
std::ostream& m_stream;
unsigned m_column_count;
unsigned m_current_column;
bool m_first_row;
public:
explicit CsvWriter(FileAPI::IFile* file);
explicit CsvWriter(std::ostream& stream);
void WriteColumn(const std::string& value);
void NextRow();

View File

@ -1,16 +1,12 @@
#include "StringFileDumper.h"
#include <regex>
StringFileDumper::StringFileDumper(Zone* zone, FileAPI::File* file)
StringFileDumper::StringFileDumper(Zone* zone, std::ostream& stream)
: m_zone(zone),
m_stream(stream),
m_language_caps("ENGLISH"),
m_wrote_header(false)
{
m_zone = zone;
m_file = file;
m_config_file = "";
m_notes = "";
m_language_caps = "ENGLISH";
m_wrote_header = false;
}
void StringFileDumper::SetLanguageName(std::string language)
@ -31,11 +27,11 @@ void StringFileDumper::SetNotes(std::string notes)
void StringFileDumper::WriteHeader()
{
m_file->Printf("// Dumped from fastfile \"%s\".\n", m_zone->m_name.c_str());
m_file->Printf("// In their original format the strings might have been separated in multiple files.\n");
m_file->Printf("VERSION \"1\"\n");
m_file->Printf("CONFIG \"%s\"\n", m_config_file.c_str());
m_file->Printf("FILENOTES \"%s\"\n", m_notes.c_str());
m_stream << "// Dumped from fastfile \"" << m_zone->m_name << "\".\n";
m_stream << "// In their original format the strings might have been separated in multiple files.\n";
m_stream << "VERSION \"1\"\n";
m_stream << "CONFIG \"" << m_config_file << "\"\n";
m_stream << "FILENOTES \"" << m_notes << "\"\n";
m_wrote_header = true;
}
@ -45,13 +41,13 @@ void StringFileDumper::WriteLocalizeEntry(const std::string& reference, const st
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\n");
m_file->Printf("REFERENCE %s\n", reference.c_str());
m_stream << "\n";
m_stream << "REFERENCE " << reference <<"\n";
const std::string escapedValue = std::regex_replace(value, std::regex("\n"), "\\n");
const auto escapedValue = std::regex_replace(value, std::regex("\n"), "\\n");
const std::string valueSpacing = std::string(15 - m_language_caps.length(), ' ');
m_file->Printf("LANG_%s%s\"%s\"\n", m_language_caps.c_str(), valueSpacing.c_str(), escapedValue.c_str());
const auto valueSpacing = std::string(15 - m_language_caps.length(), ' ');
m_stream << "LANG_" << m_language_caps << valueSpacing << "\"" << escapedValue << "\"\n";
}
void StringFileDumper::Finalize()
@ -59,5 +55,5 @@ void StringFileDumper::Finalize()
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\nENDMARKER");
}
m_stream << "\nENDMARKER";
}

View File

@ -1,12 +1,13 @@
#pragma once
#include <ostream>
#include "Zone/Zone.h"
#include "Utils/FileAPI.h"
class StringFileDumper
{
Zone* m_zone;
FileAPI::File* m_file;
std::ostream& m_stream;
std::string m_config_file;
std::string m_notes;
@ -17,7 +18,7 @@ class StringFileDumper
void WriteHeader();
public:
StringFileDumper(Zone* zone, FileAPI::File* file);
StringFileDumper(Zone* zone, std::ostream& stream);
void SetConfigFile(std::string configFile);
void SetNotes(std::string notes);