Skip to content

Commit

Permalink
Reformat c++ and header files
Browse files Browse the repository at this point in the history
  • Loading branch information
wgergely committed Sep 15, 2024
1 parent d1c7df1 commit 5e9506f
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 215 deletions.
108 changes: 71 additions & 37 deletions src/include/commandlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,90 +10,118 @@
#include <string>
#include <vector>

class CommandLineParser {
public:
struct ArgumentSpec {
std::vector<std::wstring> names; // Include both short and long names here
class CommandLineParser
{
public:
struct ArgumentSpec
{
std::vector<std::wstring> names; // Include both short and long names here
std::wstring description;
std::optional<std::wstring> defaultValue;
bool requiresValue = false;
bool required = false;
};

private:
private:
std::map<std::wstring, ArgumentSpec> specs;
std::map<std::wstring, std::wstring> parsedArgs;
std::map<std::wstring, std::wstring> aliasMap; // Map aliases to primary names

void buildAliasMap() {
for (const auto &spec : specs) {
for (const auto &name : spec.second.names) {
std::map<std::wstring, std::wstring> aliasMap; // Map aliases to primary names

void buildAliasMap()
{
for (const auto &spec : specs)
{
for (const auto &name : spec.second.names)
{
aliasMap[name] = spec.first;
}
}
}

void initializeDefaults() {
for (const auto &spec : specs) {
if (spec.second.defaultValue.has_value()) {
void initializeDefaults()
{
for (const auto &spec : specs)
{
if (spec.second.defaultValue.has_value())
{
parsedArgs[spec.first] = spec.second.defaultValue.value();
} else if (!spec.second.requiresValue) {
}
else if (!spec.second.requiresValue)
{
// If it doesn't require a value and no default is specified, initialize
// with an empty string
parsedArgs[spec.first] = L"";
}
}
}

public:
CommandLineParser(std::initializer_list<std::pair<const std::wstring, ArgumentSpec>> list) {
for (const auto &item : list) {
public:
CommandLineParser(std::initializer_list<std::pair<const std::wstring, ArgumentSpec>> list)
{
for (const auto &item : list)
{
specs[item.first] = item.second;
}
buildAliasMap();
initializeDefaults();
}

void parse(int argc, wchar_t *argv[]) {
for (int i = 1; i < argc; ++i) {
void parse(int argc, wchar_t *argv[])
{
for (int i = 1; i < argc; ++i)
{
std::wstring arg = argv[i];
std::wstring primaryName;

// Find the primary argument name for the given alias
auto aliasIt = aliasMap.find(arg);
if (aliasIt != aliasMap.end()) {
if (aliasIt != aliasMap.end())
{
primaryName = aliasIt->second;
} else {
}
else
{
throw std::runtime_error("Unknown argument: " + std::string(arg.begin(), arg.end()));
}

const auto &spec = specs[primaryName];
if (spec.requiresValue) {
if ((i + 1) < argc && argv[i + 1][0] != L'-') {
if (spec.requiresValue)
{
if ((i + 1) < argc && argv[i + 1][0] != L'-')
{
parsedArgs[primaryName] = argv[++i];
} else {
}
else
{
throw std::runtime_error("Argument requires a value but none was provided.");
}
} else {
}
else
{
parsedArgs[primaryName] = spec.defaultValue.value_or(L"");
}
}

for (const auto &spec : specs) {
if (spec.second.required && parsedArgs.find(spec.first) == parsedArgs.end()) {
for (const auto &spec : specs)
{
if (spec.second.required && parsedArgs.find(spec.first) == parsedArgs.end())
{
throw std::runtime_error("Missing required argument: " +
std::string(spec.first.begin(), spec.first.end()));
}
}
}

template <typename T>
T get(const std::wstring &name) const {
T get(const std::wstring &name) const
{
auto it = parsedArgs.find(name);
if (it != parsedArgs.end()) {
if (it != parsedArgs.end())
{
T value;
std::wistringstream wiss(it->second);
if (!(wiss >> value)) {
if (!(wiss >> value))
{
throw std::runtime_error("Invalid argument value for: " + std::string(name.begin(), name.end()));
}
return value;
Expand All @@ -104,24 +132,30 @@ class CommandLineParser {

bool has(const std::wstring &name) const { return parsedArgs.find(name) != parsedArgs.end(); }

void showHelp() const {
void showHelp() const
{
std::wcout << L"Usage instructions:\n";
for (const auto &specItem : specs) {
for (const auto &specItem : specs)
{
const auto &spec = specItem.second;
std::wstring names = join(spec.names, L", ");
std::wstring defaultValue = spec.defaultValue.has_value() ? spec.defaultValue.value() : L"";
std::wcout << L" " << names << L"\t" << spec.description;
if (!defaultValue.empty()) {
if (!defaultValue.empty())
{
std::wcout << L" (default: " << defaultValue << L")";
}
std::wcout << std::endl;
}
}

static std::wstring join(const std::vector<std::wstring> &vec, const std::wstring &delimiter) {
static std::wstring join(const std::vector<std::wstring> &vec, const std::wstring &delimiter)
{
std::wstring result;
for (auto it = vec.begin(); it != vec.end(); ++it) {
if (it != vec.begin()) {
for (auto it = vec.begin(); it != vec.end(); ++it)
{
if (it != vec.begin())
{
result += delimiter;
}
result += *it;
Expand All @@ -130,4 +164,4 @@ class CommandLineParser {
}
};

#endif // COMMAND_LINE_PARSER_H
#endif // COMMAND_LINE_PARSER_H
4 changes: 2 additions & 2 deletions src/include/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // _WIN32
#endif // _WIN32
#include <filesystem>
#include <iostream>
#include <sstream>
Expand All @@ -18,4 +18,4 @@ Dist::Paths InitializeEnvironment(bool use_grandparent = false);

int LaunchProcess(int argc, wchar_t *argv[], std::filesystem::path exe_path);

#endif // ENV_H
#endif // ENV_H
7 changes: 3 additions & 4 deletions src/include/imageutil.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef IMAGEUTIL_H
#define IMAGEUTIL_H

#pragma once // Prevents multiple inclusions
#pragma once // Prevents multiple inclusions

#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/imagecache.h>
Expand All @@ -21,12 +21,11 @@
#include <variant>
#include <vector>


#ifdef _PYBIND_MODULE
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
#endif // _PYBIND_MODULE
#endif // _PYBIND_MODULE

std::wstring empty_string = L"";
std::string empty_string_ = "";
Expand Down Expand Up @@ -64,4 +63,4 @@ int ConvertImage(const std::wstring &input, const std::wstring &output, int size
*/
int ConvertSequence(const std::wstring &input, const std::wstring &output, int size = 9, int threads = 0,
bool verbose = false);
#endif // IMAGEUTIL_H
#endif // IMAGEUTIL_H
21 changes: 12 additions & 9 deletions src/include/stringconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,39 @@
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // For MultiByteToWideChar and WideCharToMultiByte
#include <windows.h> // For MultiByteToWideChar and WideCharToMultiByte
#else
#include <codecvt> // For std::codecvt_utf8 (deprecated in C++17)
#include <codecvt> // For std::codecvt_utf8 (deprecated in C++17)
#endif

class StringConverter {
public:
static std::wstring to_wstring(const std::string &utf8Str) {
class StringConverter
{
public:
static std::wstring to_wstring(const std::string &utf8Str)
{
#ifdef _WIN32
int count = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0);
std::wstring wideStr(count, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], count);
return wideStr;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(utf8Str);
#endif
}

static std::string to_string(const std::wstring &wideStr) {
static std::string to_string(const std::wstring &wideStr)
{
#ifdef _WIN32
int count = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string utf8Str(count, 0);
WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], count, nullptr, nullptr);
return utf8Str;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(wideStr);
#endif
}
};

#endif // STRINGCONVERTER_H
#endif // STRINGCONVERTER_H
10 changes: 7 additions & 3 deletions src/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
#endif

int wmain(int argc, wchar_t *argv[]) {
int wmain(int argc, wchar_t *argv[])
{
Dist::Paths paths = InitializeEnvironment(false);

try {
try
{
return LaunchProcess(argc, argv, paths.py_launcher_exe);
} catch (std::exception &e) {
}
catch (std::exception &e)
{
MessageBoxA(NULL, e.what(), "Error", MB_ICONERROR);
std::wcerr << L"Error: " << e.what() << L"\n";
return 1;
Expand Down
Loading

0 comments on commit 5e9506f

Please sign in to comment.