Tidy up some xml structures.
This commit is contained in:
parent
875cdc84ff
commit
8771b721d1
31 changed files with 885 additions and 563 deletions
68
src/core/file_utilities/File.cpp
Normal file
68
src/core/file_utilities/File.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "File.h"
|
||||
#include "FileLogger.h"
|
||||
|
||||
File::File(std::filesystem::path path)
|
||||
: mFullPath(path),
|
||||
mInHandle(),
|
||||
mOutHandle(),
|
||||
mAccessMode(AccessMode::Read)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string File::GetExtension() const
|
||||
{
|
||||
return mFullPath.extension();
|
||||
}
|
||||
|
||||
void File::SetAccessMode(AccessMode mode)
|
||||
{
|
||||
mAccessMode = mode;
|
||||
}
|
||||
|
||||
std::ifstream* File::GetInHandle() const
|
||||
{
|
||||
return mInHandle.get();
|
||||
}
|
||||
|
||||
std::ofstream* File::GetOutHandle() const
|
||||
{
|
||||
return mOutHandle.get();
|
||||
}
|
||||
|
||||
void File::Open()
|
||||
{
|
||||
if(mAccessMode == AccessMode::Read)
|
||||
{
|
||||
mInHandle = std::make_unique<std::ifstream>();
|
||||
mInHandle->open(mFullPath, std::ifstream::in);
|
||||
}
|
||||
else
|
||||
{
|
||||
mOutHandle = std::make_unique<std::ofstream>();
|
||||
mOutHandle->open(mFullPath, std::ofstream::out);
|
||||
}
|
||||
}
|
||||
|
||||
void File::Close()
|
||||
{
|
||||
if(mOutHandle)
|
||||
{
|
||||
mOutHandle->close();
|
||||
}
|
||||
if(mInHandle)
|
||||
{
|
||||
mInHandle->close();
|
||||
}
|
||||
}
|
||||
|
||||
FileFormat::Format File::InferFormat() const
|
||||
{
|
||||
const auto extension = GetExtension();
|
||||
return FileFormat::InferFormat(extension);
|
||||
}
|
||||
|
||||
bool File::PathExists() const
|
||||
{
|
||||
return std::filesystem::exists(mFullPath);
|
||||
}
|
43
src/core/file_utilities/File.h
Normal file
43
src/core/file_utilities/File.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include "FileFormats.h"
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
class File
|
||||
{
|
||||
public:
|
||||
enum class AccessMode{
|
||||
Read,
|
||||
Write
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
std::filesystem::path mFullPath;
|
||||
std::unique_ptr<std::ifstream> mInHandle;
|
||||
std::unique_ptr<std::ofstream> mOutHandle;
|
||||
AccessMode mAccessMode;
|
||||
|
||||
public:
|
||||
|
||||
File(std::filesystem::path fullPath);
|
||||
|
||||
std::string GetExtension() const;
|
||||
|
||||
FileFormat::Format InferFormat() const;
|
||||
|
||||
std::ifstream* GetInHandle() const;
|
||||
|
||||
std::ofstream* GetOutHandle() const;
|
||||
|
||||
bool PathExists() const;
|
||||
|
||||
void SetAccessMode(AccessMode mode);
|
||||
|
||||
void Open();
|
||||
|
||||
void Close();
|
||||
|
||||
};
|
9
src/core/file_utilities/FileFormats.cpp
Normal file
9
src/core/file_utilities/FileFormats.cpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include "FileFormats.h"
|
||||
|
||||
FileFormat::ExtensionMap FileFormat::mExtensions = []
|
||||
{
|
||||
ExtensionMap ret;
|
||||
ret[Format::Markdown] = ".md";
|
||||
ret[Format::Html] = ".html";
|
||||
return ret;
|
||||
}();
|
57
src/core/file_utilities/FileFormats.h
Normal file
57
src/core/file_utilities/FileFormats.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "StringUtils.h"
|
||||
|
||||
class FileFormat{
|
||||
|
||||
public:
|
||||
enum class Encoding{
|
||||
Ascii,
|
||||
UTF8,
|
||||
Binary
|
||||
};
|
||||
|
||||
enum class Format{
|
||||
Html,
|
||||
Xml,
|
||||
Markdown,
|
||||
Pdf,
|
||||
Json,
|
||||
Png,
|
||||
Wav,
|
||||
Midi,
|
||||
Unknown
|
||||
};
|
||||
|
||||
using ExtensionMap = std::map<Format, std::string>;
|
||||
|
||||
private:
|
||||
static ExtensionMap mExtensions;
|
||||
|
||||
public:
|
||||
|
||||
bool IsFormat(const std::string& extension, Format format)
|
||||
{
|
||||
return StringUtils::ToLower(extension) == mExtensions[format];
|
||||
}
|
||||
|
||||
static Format InferFormat(const std::string& query)
|
||||
{
|
||||
for(const auto& extension : mExtensions)
|
||||
{
|
||||
if(extension.second == query)
|
||||
{
|
||||
return extension.first;
|
||||
}
|
||||
}
|
||||
return Format::Unknown;
|
||||
}
|
||||
|
||||
std::string GetExtension(Format format)
|
||||
{
|
||||
return mExtensions[format];
|
||||
}
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue