Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common command line parser #1100

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/celcompat/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <celutil/winutil.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#if defined(_MSC_VER) && !defined(__clang__)
// M$VC++ build without C++ exceptions are not supported yet
Expand Down Expand Up @@ -594,5 +595,66 @@ bool create_directory(const path& p)
return r;
}

path current_path()
{
std::error_code ec;
path p = current_path(ec);

if (ec)
#if __cpp_exceptions
throw filesystem_error(ec, "celfs::current_path error");
#else
std::abort();
#endif
return p;
}
path current_path(std::error_code& ec)
{
#ifdef _WIN32
std::wstring buffer(MAX_PATH + 1, 0);
DWORD r = GetModuleFileNameW(nullptr, &buffer[0], MAX_PATH);
if (r == 0)
{
ec = std::error_code(errno, std::system_category());
return path();
}
auto pos = buffer.find_last_of(L"\\/");
return buffer.substr(0, pos);
#else
std::string buffer(256, 0);
char *r = getcwd(&buffer[0], buffer.size());
if (r == nullptr)
{
ec = std::error_code(errno, std::system_category());
return path();
}
return buffer;
#endif
}

void current_path(const path& p)
{
std::error_code ec;
current_path(p, ec);
if (ec)
#if __cpp_exceptions
throw filesystem_error(ec, "celfs::current_path error");
#else
std::abort();
#endif
}

void current_path(const path& p, std::error_code& ec) noexcept
{
#ifdef _WIN32
BOOL ret = SetCurrentDirectoryW(p.c_str());
if (!ret)
ec = std::error_code(errno, std::system_category());
#else
int ret = chdir(p.c_str());
if (ret != 0)
ec = std::error_code(errno, std::system_category());
#endif
}
}
}
5 changes: 5 additions & 0 deletions src/celcompat/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,10 @@ bool is_directory(const path& p, std::error_code& ec) noexcept;

bool create_directory(const path& p);
bool create_directory(const path& p, std::error_code& ec) noexcept;

path current_path();
path current_path(std::error_code& ec);
void current_path(const path& p);
void current_path(const path& p, std::error_code& ec) noexcept;
}
}
2 changes: 2 additions & 0 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ std::ofstream hdrlog;
#endif
#ifdef _MSC_VER
#include <malloc.h>
#ifndef alloca
#define alloca(s) _alloca(s)
#endif
#endif

using namespace cmod;
using namespace Eigen;
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/vertexobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void VertexObject::enableAttribArrays() noexcept
auto n = t.first;
auto& p = t.second;
glEnableVertexAttribArray(n);
glVertexAttribPointer(n, p.count, p.type, p.normalized, p.stride, (GLvoid*) p.offset);
glVertexAttribPointer(n, p.count, p.type, p.normalized, p.stride, (GLvoid*) (size_t) p.offset);
}
}

Expand Down
88 changes: 77 additions & 11 deletions src/celestia/celestiacore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,10 @@ void CelestiaCore::setStartURL(const string &url)
}
}

std::string_view CelestiaCore::getStartURL() const
{
return startURL;
}

void CelestiaCore::tick()
{
Expand Down Expand Up @@ -3680,13 +3684,15 @@ using StarLoader = CatalogLoader<StarDatabase>;
using DeepSkyLoader = CatalogLoader<DSODatabase>;


bool CelestiaCore::initSimulation(const fs::path& configFileName,
const vector<fs::path>& extrasDirs,
ProgressNotifier* progressNotifier)
bool CelestiaCore::initSimulation(ProgressNotifier* progressNotifier)
{
if (!configFileName.empty())
initDataPath();
std::error_code ec;
fs::current_path(m_dataPath, ec);

if (!m_configFileName.empty())
{
config = ReadCelestiaConfig(configFileName);
config = ReadCelestiaConfig(m_configFileName);
}
else
{
Expand Down Expand Up @@ -3723,12 +3729,12 @@ bool CelestiaCore::initSimulation(const fs::path& configFileName,
// after the ones from the config file and the order in which they were
// specified is preserved. This process in O(N*M), but the number of
// additional extras directories should be small.
for (const auto& dir : extrasDirs)
for (const auto& dir : m_extrasDirs)
{
if (find(config->extrasDirs.begin(), config->extrasDirs.end(), dir.string()) ==
if (find(config->extrasDirs.begin(), config->extrasDirs.end(), dir) ==
config->extrasDirs.end())
{
config->extrasDirs.push_back(dir.string());
config->extrasDirs.push_back(dir);
}
}

Expand Down Expand Up @@ -4764,7 +4770,7 @@ bool CelestiaCore::saveScreenShot(const fs::path& filename, ContentType type) co
return false;
}

void CelestiaCore::setLogFile(fs::path &fn)
void CelestiaCore::setLogFile(const fs::path &fn)
{
m_logfile = std::ofstream(fn.string());
if (m_logfile.good())
Expand All @@ -4779,6 +4785,39 @@ void CelestiaCore::setLogFile(fs::path &fn)
}
}

bool CelestiaCore::initDataPath()
{
// was set using command line
if (!m_dataPath.empty())
return true;

#ifdef _WIN32
const wchar_t *d = _wgetenv(L"CELESTIA_DATA_DIR");
#else
const char *d = getenv("CELESTIA_DATA_DIR");
#endif
if (d != nullptr)
{
m_dataPath = fs::path(d);
return true;
}

#ifdef NATIVE_OSX_APP
// On macOS data directory is in a fixed position relative to the
// application bundle
error_code ec;
auto curDir = fs::current_path(ec);
if (ec != error_code())
return false;
assert(curDir.is_absolute());
m_dataPath = curDir.parent_path() / "Resources";
#else
m_dataPath = fs::path(CONFIG_DATA_DIR);
#endif

return true;
}

#ifdef USE_FFMPEG
auto CelestiaCore::getSupportedMovieSizes() const
-> celestia::util::array_view<MovieSize>
Expand All @@ -4800,9 +4839,9 @@ auto CelestiaCore::getSupportedMovieSizes() const
auto CelestiaCore::getSupportedMovieFramerates() const
-> celestia::util::array_view<float>
{
static std::array<float, 5> MovieFramerates =
static std::array<float, 6> MovieFramerates =
{
15.0f, 24.0f, 25.0f, 29.97f, 30.0f
15.0f, 24.0f, 25.0f, 29.97f, 30.0f, 60.0f
};
return MovieFramerates;
}
Expand All @@ -4818,3 +4857,30 @@ auto CelestiaCore::getSupportedMovieCodecs() const
return MovieCodecs;
}
#endif

static void CommandLineError(const char *message)
{
cout << message << '\n';
}

util::CmdLineParser CelestiaCore::getCommandLineParser()
{
util::CmdLineParser parser;
parser.on("conf", 'c', true,
_("Configuration file name expected after --conf/-c"),
[this](const char *v) { m_configFileName = v; return true; });
parser.on("dir", 'd', true,
_("Directory expected after --dir/-d"),
[this](const char *v) { m_dataPath = v; return true; });
parser.on("extrasdir", 'e', true,
_("Directory expected after --extrasdir/-e"),
[this](const char *v) { m_extrasDirs.push_back(v); return true; });
parser.on("url", 'u', true,
_("URL expected after --url/-u"),
[this](const char *v) { setStartURL(v); return true; });
parser.on("log", 'l', true,
_("A filename expected after --log/-l"),
[this](const char *v) { setLogFile(v); return true; });

return parser;
}
26 changes: 19 additions & 7 deletions src/celestia/celestiacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
#include <fstream>
#include <string>
#include <memory>
#include <celutil/array_view.h>
#include <celutil/cmdline.h>
#include <celutil/filetype.h>
#include <celutil/tee.h>
#include <celutil/timer.h>
#include <celutil/watcher.h>
// #include <celutil/watchable.h>
#include <celcompat/string_view.h>
#include <celengine/solarsys.h>
#include <celengine/overlay.h>
#include <celengine/texture.h>
Expand All @@ -25,7 +29,6 @@
#include <celengine/simulation.h>
#include <celengine/overlayimage.h>
#include <celengine/viewporteffect.h>
#include <celutil/tee.h>
#include "configfile.h"
#include "favorites.h"
#include "destination.h"
Expand Down Expand Up @@ -195,9 +198,7 @@ class CelestiaCore // : public Watchable<CelestiaCore>
CelestiaCore();
~CelestiaCore();

bool initSimulation(const fs::path& configFileName = fs::path(),
const std::vector<fs::path>& extrasDirs = {},
ProgressNotifier* progressNotifier = nullptr);
bool initSimulation(ProgressNotifier* progressNotifier = nullptr);
bool initRenderer();
void start(double t);
void start();
Expand All @@ -206,6 +207,7 @@ class CelestiaCore // : public Watchable<CelestiaCore>

// URLs and history navigation
void setStartURL(const std::string& url);
std::string_view getStartURL() const;
bool goToUrl(const std::string& urlStr);
void addToHistory();
void back();
Expand Down Expand Up @@ -322,12 +324,12 @@ class CelestiaCore // : public Watchable<CelestiaCore>

void notifyWatchers(int);

void setLogFile(fs::path&);
void setLogFile(const fs::path&);

class Alerter
{
public:
virtual ~Alerter() {};
virtual ~Alerter() = default;
virtual void fatalError(const std::string&) = 0;
};

Expand All @@ -337,7 +339,7 @@ class CelestiaCore // : public Watchable<CelestiaCore>
class CursorHandler
{
public:
virtual ~CursorHandler() {};
virtual ~CursorHandler() = default;
virtual void setCursorShape(CursorShape) = 0;
virtual CursorShape getCursorShape() const = 0;
};
Expand Down Expand Up @@ -389,6 +391,8 @@ class CelestiaCore // : public Watchable<CelestiaCore>
Image captureImage() const;
bool saveScreenShot(const fs::path&, ContentType = Content_Unknown) const;

celestia::util::CmdLineParser getCommandLineParser();

protected:
bool readStars(const CelestiaConfig&, ProgressNotifier*);
void renderOverlay();
Expand All @@ -397,6 +401,8 @@ class CelestiaCore // : public Watchable<CelestiaCore>
#endif // CELX

private:
bool initDataPath();

CelestiaConfig* config{ nullptr };

Universe* universe{ nullptr };
Expand Down Expand Up @@ -538,6 +544,12 @@ class CelestiaCore // : public Watchable<CelestiaCore>
std::ofstream m_logfile;
teestream m_tee;

// options passed through command line
fs::path m_dataPath;
fs::path m_configFileName;
fs::path m_logFilename;
std::vector<fs::path> m_extrasDirs;

#ifdef CELX
friend View* getViewByObserver(CelestiaCore*, Observer*);
friend void getObservers(CelestiaCore*, std::vector<Observer*>&);
Expand Down
Loading