Tabs -> Spaces

This commit is contained in:
Pasha Bibko
2025-07-21 17:39:43 +01:00
parent 24fde1b770
commit 5bfeb75536
10 changed files with 514 additions and 515 deletions

View File

@@ -5,98 +5,98 @@
namespace LXC::Util
{
// Error returned when Util::ReadFile runs into errors //
struct FileReadError final
{
// Different reasons why the error can occur //
enum Reason
{
FileNotFound,
PermissionDenied,
NotAFile
};
// Error returned when Util::ReadFile runs into errors //
struct FileReadError final
{
// Different reasons why the error can occur //
enum Reason
{
FileNotFound,
PermissionDenied,
NotAFile
};
// Constructor to pass arguments to the struct //
FileReadError(const std::filesystem::path& _path, Reason _reason)
: path(_path), reason(_reason)
{}
// Constructor to pass arguments to the struct //
FileReadError(const std::filesystem::path& _path, Reason _reason)
: path(_path), reason(_reason)
{}
// Error information //
const std::filesystem::path path;
const Reason reason;
// Error information //
const std::filesystem::path path;
const Reason reason;
// Turns the error into a c-string //
inline static const char* const ReasonStr(Reason reason)
{
static const char* reasons[] =
{
"File cannot be found",
"File reading permissions are denied",
"Not a file"
};
// Turns the error into a c-string //
inline static const char* const ReasonStr(Reason reason)
{
static const char* reasons[] =
{
"File cannot be found",
"File reading permissions are denied",
"Not a file"
};
return reasons[reason];
}
};
return reasons[reason];
}
};
// Util function to read a file as quick as possible with error handling //
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
{
// Checks the file exists //
if (!std::filesystem::exists(filepath))
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::FileNotFound);
// Util function to read a file as quick as possible with error handling //
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
{
// Checks the file exists //
if (!std::filesystem::exists(filepath))
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::FileNotFound);
// Checks it is a regular file //
if (!std::filesystem::is_regular_file(filepath))
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::NotAFile);
// Checks it is a regular file //
if (!std::filesystem::is_regular_file(filepath))
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::NotAFile);
// Checks it can open the file //
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file)
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::PermissionDenied);
// Checks it can open the file //
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file)
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::PermissionDenied);
// Copies the file to the output string //
const std::streamsize len = file.tellg();
file.seekg(0, std::ios::beg);
// Copies the file to the output string //
const std::streamsize len = file.tellg();
file.seekg(0, std::ios::beg);
std::string contents(len, '\0');
file.read(&contents[0], len);
std::string contents(len, '\0');
file.read(&contents[0], len);
return contents;
}
return contents;
}
// Struct to hold a position within a file //
struct FileLocation
{
unsigned short col;
unsigned short line;
};
// Struct to hold a position within a file //
struct FileLocation
{
unsigned short col;
unsigned short line;
};
// Finds the location of a given index within a file //
inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index)
{
// Resets location //
location.line = 1;
location.col = 1;
// Finds the location of a given index within a file //
inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index)
{
// Resets location //
location.line = 1;
location.col = 1;
// Returns false if outside the bounds //
if (index < 0 || index > file.length())
return false;
// Returns false if outside the bounds //
if (index < 0 || index > file.length())
return false;
// Finds the location //
__int32 localIndex = 0;
while (localIndex != index)
{
if (file[localIndex] == '\n')
{
location.line += 1;
location.col = 0;
}
// Finds the location //
__int32 localIndex = 0;
while (localIndex != index)
{
if (file[localIndex] == '\n')
{
location.line += 1;
location.col = 0;
}
location.col++;
localIndex++;
}
location.col++;
localIndex++;
}
return true;
}
return true;
}
}

View File

@@ -8,137 +8,137 @@
namespace LXC::Util
{
// Enum to translate to the Win32 code for the colors //
enum Color : WORD
{
DEFAULT = 0x07,
// Enum to translate to the Win32 code for the colors //
enum Color : WORD
{
DEFAULT = 0x07,
BLACK = 0x00,
BLUE = 0x01,
GREEN = 0x02,
AQUA = 0x03,
RED = 0x04,
PURPLE = 0x05,
YELLOW = 0x06,
LIGHT_GRAY = 0x07,
LIGHT_BLUE = 0x09,
LIGHT_GREEN = 0x0a,
LIGHT_AQUA = 0x0b,
LIGHT_RED = 0x0c,
LIGHT_PURPLE = 0x0d,
LIGHT_YELLOW = 0x0e,
WHITE = 0x0f
};
BLACK = 0x00,
BLUE = 0x01,
GREEN = 0x02,
AQUA = 0x03,
RED = 0x04,
PURPLE = 0x05,
YELLOW = 0x06,
LIGHT_GRAY = 0x07,
LIGHT_BLUE = 0x09,
LIGHT_GREEN = 0x0a,
LIGHT_AQUA = 0x0b,
LIGHT_RED = 0x0c,
LIGHT_PURPLE = 0x0d,
LIGHT_YELLOW = 0x0e,
WHITE = 0x0f
};
}
namespace LXC::Internal
{
// Checks if a type can be outputted to std::ostream //
template<typename T> concept Logable = requires(std::ostream& os, T t)
{
// I have no idea what this part does at all //
{ os << t } -> std::same_as<std::ostream&>;
};
// Checks if a type can be outputted to std::ostream //
template<typename T> concept Logable = requires(std::ostream& os, T t)
{
// I have no idea what this part does at all //
{ os << t } -> std::same_as<std::ostream&>;
};
// Checks if a list of types can be outputted to std::ostream //
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
// Checks if a list of types can be outputted to std::ostream //
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
// Checks if the type has a custom log method //
template <typename T> concept HasLogStrFunc = requires(T obj)
{
// Checks the type has a function LogStr that returns a string //
{ obj.LogStr() } -> std::same_as<std::string>;
};
// Checks if the type has a custom log method //
template <typename T> concept HasLogStrFunc = requires(T obj)
{
// Checks the type has a function LogStr that returns a string //
{ obj.LogStr() } -> std::same_as<std::string>;
};
// Returns a reference to the log file //
inline std::ofstream& Log()
{
static std::ofstream sLog;
return sLog;
}
// Returns a reference to the log file //
inline std::ofstream& Log()
{
static std::ofstream sLog;
return sLog;
}
// Base function for all derived Print() functions //
template<Util::Color col, bool newLine, typename... Args>
inline void WriteImpl(std::ostream& os, Args&&... args)
{
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if constexpr(col != Util::Color::DEFAULT)
SetConsoleTextAttribute(hConsole, static_cast<WORD>(col));
// Base function for all derived Print() functions //
template<Util::Color col, bool newLine, typename... Args>
inline void WriteImpl(std::ostream& os, Args&&... args)
{
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if constexpr(col != Util::Color::DEFAULT)
SetConsoleTextAttribute(hConsole, static_cast<WORD>(col));
(os << ... << std::forward<Args>(args));
(os << ... << std::forward<Args>(args));
if constexpr (newLine)
os << std::endl;
if constexpr (newLine)
os << std::endl;
if constexpr (col != Util::Color::DEFAULT)
SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT));
}
if constexpr (col != Util::Color::DEFAULT)
SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT));
}
}
namespace LXC::Util
{
// Prints arguments to the console with the given color //
template<Color col, typename... Args>
requires Internal::AllLogable<Args...>
inline void PrintAs(Args&&... args)
{
Internal::WriteImpl<col, false>(std::cout, std::forward<Args>(args)...);
}
// Prints arguments to the console with the given color //
template<Color col, typename... Args>
requires Internal::AllLogable<Args...>
inline void PrintAs(Args&&... args)
{
Internal::WriteImpl<col, false>(std::cout, std::forward<Args>(args)...);
}
// Prints arguments to the console //
template<typename... Args>
requires Internal::AllLogable<Args...>
inline void Print(Args&&... args)
{
Internal::WriteImpl<Util::Color::DEFAULT, false>(std::cout, std::forward<Args>(args)...);
}
// Prints arguments to the console //
template<typename... Args>
requires Internal::AllLogable<Args...>
inline void Print(Args&&... args)
{
Internal::WriteImpl<Util::Color::DEFAULT, false>(std::cout, std::forward<Args>(args)...);
}
// Prints arguments to the console with a new-line character at the end //
template<typename... Args>
requires Internal::AllLogable<Args...>
inline void PrintLn(Args&&... args)
{
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, std::forward<Args>(args)...);
}
// Prints arguments to the console with a new-line character at the end //
template<typename... Args>
requires Internal::AllLogable<Args...>
inline void PrintLn(Args&&... args)
{
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, std::forward<Args>(args)...);
}
// Logs all the arguments to the file (automatically flushes) //
template<typename... Args>
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
inline void Log(Args&&... args)
{
std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, R"([LXC] ")", std::forward<Args>(args)..., '"');
}
// Logs all the arguments to the file (automatically flushes) //
template<typename... Args>
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
inline void Log(Args&&... args)
{
std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, R"([LXC] ")", std::forward<Args>(args)..., '"');
}
// Logs a singular argument to the log, calls Log() if it can on the object //
template<typename T>
requires Internal::HasLogStrFunc<T> || Internal::Logable<T>
inline void Log(T arg)
{
std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY
{
if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
// Logs a singular argument to the log, calls Log() if it can on the object //
template<typename T>
requires Internal::HasLogStrFunc<T> || Internal::Logable<T>
inline void Log(T arg)
{
std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY
{
if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '"', arg, '"');
}
}
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '"', arg, '"');
}
}
// Intitalises the log with the given file name //
inline void CreateLog(const std::filesystem::path& path)
{
// Opens the log file with the given path //
std::ofstream& log = Internal::Log();
log.open(path);
// Intitalises the log with the given file name //
inline void CreateLog(const std::filesystem::path& path)
{
// Opens the log file with the given path //
std::ofstream& log = Internal::Log();
log.open(path);
// Assigns a function to close the log file on program exit //
std::atexit([]()
{
std::ofstream& log = Internal::Log();
log.close();
});
}
// Assigns a function to close the log file on program exit //
std::atexit([]()
{
std::ofstream& log = Internal::Log();
log.close();
});
}
}

View File

@@ -2,9 +2,9 @@
// Platform specific includes //
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#error "Code is currently only supported on Win32"
#error "Code is currently only supported on Win32"
#endif // _WIN32

View File

@@ -7,110 +7,110 @@
namespace LXC::Util
{
// Util function to stop/ the program //
inline void Stop()
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Util function to stop/ the program //
inline void Stop()
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Triggers a breakpoint so the debugger can work out where the program exits //
if (IsDebuggerPresent())
DebugBreak();
// Triggers a breakpoint so the debugger can work out where the program exits //
if (IsDebuggerPresent())
DebugBreak();
#endif // _DEBUG
#endif // _DEBUG
// Force exits the program //
std::exit(EXIT_FAILURE);
}
// Force exits the program //
std::exit(EXIT_FAILURE);
}
// Custom version of std::unexpected //
template<typename ErrorType> struct FunctionFail final
{
// Basic constructor to copy the error across //
explicit FunctionFail(ErrorType _err) : error(_err)
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Custom version of std::unexpected //
template<typename ErrorType> struct FunctionFail final
{
// Basic constructor to copy the error across //
explicit FunctionFail(ErrorType _err) : error(_err)
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Triggers a breakpoint when a debugger is attached as a function has failed //
if (IsDebuggerPresent())
DebugBreak();
// Triggers a breakpoint when a debugger is attached as a function has failed //
if (IsDebuggerPresent())
DebugBreak();
#endif // _DEBUG
}
#endif // _DEBUG
}
// Constructs the FunctionFail with the error itself //
template<typename... Args> requires std::constructible_from<ErrorType, Args...>
explicit FunctionFail(Args&&... args)
: error(std::forward<Args>(args)...)
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Constructs the FunctionFail with the error itself //
template<typename... Args> requires std::constructible_from<ErrorType, Args...>
explicit FunctionFail(Args&&... args)
: error(std::forward<Args>(args)...)
{
// Only checks for a debugger when compiled in Debug mode //
#ifdef _DEBUG
// Triggers a breakpoint when a debugger is attached as a function has failed //
if (IsDebuggerPresent())
DebugBreak();
// Triggers a breakpoint when a debugger is attached as a function has failed //
if (IsDebuggerPresent())
DebugBreak();
#endif // _DEBUG
}
#endif // _DEBUG
}
const ErrorType error;
};
const ErrorType error;
};
// Custom version of std::expected //
template<typename ResultType, typename ErrorType>
requires (!std::same_as<ResultType, bool>) // ResultType being bool causes issues with operator overloads
class ReturnVal final
{
public:
// Constructor for function sucess //
ReturnVal(ResultType result)
: m_Result(result), m_FunctionFailed(false)
{}
// Custom version of std::expected //
template<typename ResultType, typename ErrorType>
requires (!std::same_as<ResultType, bool>) // ResultType being bool causes issues with operator overloads
class ReturnVal final
{
public:
// Constructor for function sucess //
ReturnVal(ResultType result)
: m_Result(result), m_FunctionFailed(false)
{}
// Constructor for function fail //
ReturnVal(FunctionFail<ErrorType> error)
: m_Error(error.error), m_FunctionFailed(true)
{}
// Constructor for function fail //
ReturnVal(FunctionFail<ErrorType> error)
: m_Error(error.error), m_FunctionFailed(true)
{}
// Destructor //
~ReturnVal() {}
// Destructor //
~ReturnVal() {}
// Different getters of the class //
// Different getters of the class //
inline bool Failed() const { return m_FunctionFailed; }
inline bool Suceeded() const { return !m_FunctionFailed; }
inline bool Failed() const { return m_FunctionFailed; }
inline bool Suceeded() const { return !m_FunctionFailed; }
inline ResultType& Result()
{
if (Suceeded()) _LIKELY
return m_Result;
inline ResultType& Result()
{
if (Suceeded()) _LIKELY
return m_Result;
std::exit(EXIT_FAILURE);
}
std::exit(EXIT_FAILURE);
}
inline ErrorType& Error()
{
if (Failed()) _LIKELY
return m_Error;
inline ErrorType& Error()
{
if (Failed()) _LIKELY
return m_Error;
std::exit(EXIT_FAILURE);
}
std::exit(EXIT_FAILURE);
}
// Operator overloads //
// Operator overloads //
operator bool() const { return !m_FunctionFailed; }
operator ResultType() { return Result(); }
operator bool() const { return !m_FunctionFailed; }
operator ResultType() { return Result(); }
private:
// Union to hold either the result or the error //
union
{
ResultType m_Result;
ErrorType m_Error;
};
private:
// Union to hold either the result or the error //
union
{
ResultType m_Result;
ErrorType m_Error;
};
// Tracks what item is currently in the union //
bool m_FunctionFailed;
};
// Tracks what item is currently in the union //
bool m_FunctionFailed;
};
}