Tidy up some xml structures.
This commit is contained in:
parent
875cdc84ff
commit
8771b721d1
31 changed files with 885 additions and 563 deletions
|
@ -4,6 +4,8 @@ list(APPEND core_LIB_INCLUDES
|
|||
CommandLineArgs.cpp
|
||||
loggers/FileLogger.cpp
|
||||
file_utilities/BinaryFile.cpp
|
||||
file_utilities/File.cpp
|
||||
file_utilities/FileFormats.cpp
|
||||
StringUtils.cpp
|
||||
http/HttpResponse.cpp)
|
||||
|
||||
|
@ -12,4 +14,6 @@ add_library(core SHARED ${core_LIB_INCLUDES})
|
|||
|
||||
target_include_directories(core PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/file_utilities"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/loggers"
|
||||
)
|
|
@ -1,29 +1,45 @@
|
|||
#include "CommandLineArgs.h"
|
||||
|
||||
CommandLineArgs::CommandLineArgs()
|
||||
: mArugments()
|
||||
: mArugments(),
|
||||
mLaunchPath()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<CommandLineArgs> CommandLineArgs::CreateUnique()
|
||||
{
|
||||
return std::make_unique<CommandLineArgs>();
|
||||
}
|
||||
|
||||
std::filesystem::path CommandLineArgs::GetLaunchPath()
|
||||
{
|
||||
return mLaunchPath;
|
||||
}
|
||||
|
||||
void CommandLineArgs::RecordLaunchPath()
|
||||
{
|
||||
mLaunchPath = std::filesystem::current_path();
|
||||
}
|
||||
|
||||
void CommandLineArgs::Process(int argc, char *argv[])
|
||||
{
|
||||
for(int idx=0; idx<argc; idx++)
|
||||
{
|
||||
mArugments.push_back(std::string(argv[idx]));
|
||||
}
|
||||
for(int idx=0; idx<argc; idx++)
|
||||
{
|
||||
mArugments.push_back(std::string(argv[idx]));
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t CommandLineArgs::GetNumberOfArgs() const
|
||||
{
|
||||
return mArugments.size();
|
||||
return mArugments.size();
|
||||
}
|
||||
|
||||
std::string CommandLineArgs::GetArg(std::size_t index) const
|
||||
{
|
||||
if(index<mArugments.size())
|
||||
{
|
||||
return mArugments[index];
|
||||
}
|
||||
return "";
|
||||
if(index<mArugments.size())
|
||||
{
|
||||
return mArugments[index];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
|
|
@ -2,18 +2,28 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
class CommandLineArgs
|
||||
{
|
||||
std::vector<std::string> mArugments;
|
||||
std::vector<std::string> mArugments;
|
||||
std::filesystem::path mLaunchPath;
|
||||
|
||||
public:
|
||||
|
||||
CommandLineArgs();
|
||||
CommandLineArgs();
|
||||
|
||||
void Process(int argc, char *argv[]);
|
||||
static std::unique_ptr<CommandLineArgs> CreateUnique();
|
||||
|
||||
std::size_t GetNumberOfArgs() const;
|
||||
void RecordLaunchPath();
|
||||
|
||||
std::string GetArg(std::size_t index) const;
|
||||
std::filesystem::path GetLaunchPath();
|
||||
|
||||
void Process(int argc, char *argv[]);
|
||||
|
||||
std::size_t GetNumberOfArgs() const;
|
||||
|
||||
std::string GetArg(std::size_t index) const;
|
||||
};
|
||||
|
||||
using CommandLineArgsUPtr = std::unique_ptr<CommandLineArgs>;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "StringUtils.h"
|
||||
#include <locale>
|
||||
#include <algorithm>
|
||||
|
||||
bool StringUtils::IsAlphaNumeric(char c)
|
||||
{
|
||||
|
@ -12,3 +13,11 @@ bool StringUtils::IsSpace(char c)
|
|||
std::locale loc;
|
||||
return std::isspace(c, loc);
|
||||
}
|
||||
|
||||
std::string StringUtils::ToLower(const std::string& s)
|
||||
{
|
||||
std::string ret;
|
||||
std::transform(s.begin(), s.end(), ret.begin(),
|
||||
[](unsigned char c){ return std::tolower(c); });
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -16,4 +16,5 @@ public:
|
|||
|
||||
static bool IsAlphaNumeric(char c);
|
||||
static bool IsSpace(char c);
|
||||
static std::string ToLower(const std::string& s);
|
||||
};
|
||||
|
|
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