--Code changes

-Make previous path related code work for all OSes.
This commit is contained in:
Person 2018-05-03 00:24:51 -07:00
parent ccf5a7e80f
commit dba1bdc6c8
5 changed files with 58 additions and 26 deletions

View File

@ -11,7 +11,7 @@ using namespace EmberCommon;
/// <param name="opt">A populated EmberOptions object which specifies all program options to be used</param>
/// <returns>True if success, else false.</returns>
template <typename T>
bool EmberAnimate(EmberOptions& opt)
bool EmberAnimate(int argc, _TCHAR* argv[], EmberOptions& opt)
{
auto info = OpenCLInfo::Instance();
std::cout.imbue(std::locale(""));
@ -45,6 +45,7 @@ bool EmberAnimate(EmberOptions& opt)
vector<unique_ptr<Renderer<T, float>>> renderers;
vector<string> errorReport;
std::recursive_mutex verboseCs;
auto fullpath = GetExePath(argv[0]);
if (opt.EmberCL())
{
@ -130,7 +131,7 @@ bool EmberAnimate(EmberOptions& opt)
renderers.push_back(std::move(tempRenderer));
}
if (!InitPaletteList<float>(opt.PalettePath()))//For any modern flames, the palette isn't used. This is for legacy purposes and should be removed.
if (!InitPaletteList<float>(fullpath, opt.PalettePath())) //For any modern flames, the palette isn't used. This is for legacy purposes and should be removed.
return false;
cout << "Parsing ember file " << opt.Input() << "\n";
@ -554,10 +555,10 @@ int _tmain(int argc, _TCHAR* argv[])
#ifdef DO_DOUBLE
if (!opt.Sp())
b = EmberAnimate<double>(opt);
b = EmberAnimate<double>(argc, argv, opt);
else
#endif
b = EmberAnimate<float>(opt);
b = EmberAnimate<float>(argc, argv, opt);
}
return b ? 0 : 1;

View File

@ -100,41 +100,63 @@ static bool ParseEmberFile(XmlToEmber<T>& parser, const string& filename, vector
{
if (!parser.Parse(filename.c_str(), embers, useDefaults))
{
cout << "Error parsing flame file " << filename << ", returning without executing.\n";
cerr << "Error parsing flame file " << filename << ", returning without executing.\n";
return false;
}
if (embers.empty())
{
cout << "Error: No data present in file " << filename << ". Aborting.\n";
cerr << "Error: No data present in file " << filename << ". Aborting.\n";
return false;
}
return true;
}
/// <summary>
/// Cross platform wrapper for getting the full path of the current executable.
/// </summary>
/// <param name="programPath">The value of argv[0] passed into main()</param>
/// <returns>The full path of the executable as a string</returns>
static string GetExePath(const char* argv0)
{
string fullpath;
#ifdef _WIN32
fullpath = argv0;
#else
vector<char> v;
v.resize(2048);
#if __APPLE__
uint32_t vs = uint32_t(v.size());
if (_NSGetExecutablePath(v.data(), &vs) == 0)
fullpath = string(v.data());
else
cerr << "Could not discern full path from executable.\n";
#else
auto fullsize = readlink("/proc/self/exe", v.data(), v.size());
fullpath = string(v.data());
#endif
#endif
return GetPath(fullpath);
}
/// <summary>
/// Wrapper for parsing palette Xml file and initializing it's private static members,
/// and printing any errors that occurred.
/// Template argument expected to be float or double.
/// </summary>
/// <param name="programPath">The full path of the folder the program is running in</param>
/// <param name="filename">The full path and name of the file</param>
/// <returns>True if success, else false.</returns>
template <typename T>
static bool InitPaletteList(const string& filename)
static bool InitPaletteList(const string& programPath, const string& filename)
{
auto paletteList = PaletteList<float>::Instance();
#ifdef _WIN32
vector<char> fullpath;
fullpath.resize(2048);
GetModuleFileName(nullptr, fullpath.data(), fullpath.size());
string s = GetPath(string(fullpath.data()));
#else
string s = "./";
#endif
static vector<string> paths =
{
s
programPath
#ifndef _WIN32
, "~/",
"~/.config/fractorium/",
@ -146,15 +168,21 @@ static bool InitPaletteList(const string& filename)
for (auto& p : paths)
{
auto fullpath = p + filename;
//cout << "Trying: " << fullpath << endl;
if (!added)
added |= paletteList->Add(p + filename);
{
if (std::ifstream(fullpath))
added |= paletteList->Add(fullpath);
}
else
break;
}
if (!added || !paletteList->Size())
{
cout << "Error parsing palette file " << filename << ". Reason: \n"
cerr << "Error parsing palette file " << filename << ". Reason: \n"
<< paletteList->ErrorReportString() << "\nReturning without executing.\n";
return false;
}

View File

@ -20,6 +20,7 @@
#include <tchar.h>
#else
#include <arpa/inet.h>
#include <unistd.h>
#define _TCHAR char
#define _tmain main
#define _T

View File

@ -43,7 +43,7 @@ void SetDefaultTestValues(Ember<T>& ember)
/// <param name="opt">A populated EmberOptions object which specifies all program options to be used</param>
/// <returns>True if success, else false.</returns>
template <typename T>
bool EmberGenome(EmberOptions& opt)
bool EmberGenome(int argc, _TCHAR* argv[], EmberOptions& opt)
{
auto info = OpenCLInfo::Instance();
std::cout.imbue(std::locale(""));
@ -193,6 +193,7 @@ bool EmberGenome(EmberOptions& opt)
unique_ptr<Renderer<T, float>> renderer(CreateRenderer<T>(opt.EmberCL() ? eRendererType::OPENCL_RENDERER : eRendererType::CPU_RENDERER, devices, false, 0, emberReport));
QTIsaac<ISAAC_SIZE, ISAAC_INT> rand(ISAAC_INT(t.Tic()), ISAAC_INT(t.Tic() * 2), ISAAC_INT(t.Tic() * 3));
vector<string> errorReport = emberReport.ErrorReport();
auto fullpath = GetExePath(argv[0]);
os.imbue(std::locale(""));
os2.imbue(std::locale(""));
@ -205,7 +206,7 @@ bool EmberGenome(EmberOptions& opt)
return false;
}
if (!InitPaletteList<float>(opt.PalettePath()))
if (!InitPaletteList<float>(fullpath, opt.PalettePath()))
return false;
if (!opt.EmberCL())
@ -870,10 +871,10 @@ int _tmain(int argc, _TCHAR* argv[])
#ifdef DO_DOUBLE
if (!opt.Sp())
b = EmberGenome<double>(opt);
b = EmberGenome<double>(argc, argv, opt);
else
#endif
b = EmberGenome<float>(opt);
b = EmberGenome<float>(argc, argv, opt);
}
return b ? 0 : 1;

View File

@ -11,7 +11,7 @@ using namespace EmberCommon;
/// <param name="opt">A populated EmberOptions object which specifies all program options to be used</param>
/// <returns>True if success, else false.</returns>
template <typename T>
bool EmberRender(EmberOptions& opt)
bool EmberRender(int argc, _TCHAR* argv[], EmberOptions& opt)
{
auto info = EmberCLns::OpenCLInfo::Instance();
std::cout.imbue(std::locale(""));
@ -47,6 +47,7 @@ bool EmberRender(EmberOptions& opt)
auto progress = make_unique<RenderProgress<T>>();
unique_ptr<Renderer<T, float>> renderer(CreateRenderer<T>(opt.EmberCL() ? eRendererType::OPENCL_RENDERER : eRendererType::CPU_RENDERER, devices, false, 0, emberReport));
vector<string> errorReport = emberReport.ErrorReport();
auto fullpath = GetExePath(argv[0]);
if (!errorReport.empty())
emberReport.DumpErrorReport();
@ -60,7 +61,7 @@ bool EmberRender(EmberOptions& opt)
if (opt.EmberCL() && renderer->RendererType() != eRendererType::OPENCL_RENDERER)//OpenCL init failed, so fall back to CPU.
opt.EmberCL(false);
if (!InitPaletteList<float>(opt.PalettePath()))//For any modern flames, the palette isn't used. This is for legacy purposes and should be removed.
if (!InitPaletteList<float>(fullpath, opt.PalettePath()))//For any modern flames, the palette isn't used. This is for legacy purposes and should be removed.
return false;
if (!ParseEmberFile(parser, opt.Input(), embers))
@ -432,10 +433,10 @@ int _tmain(int argc, _TCHAR* argv[])
#ifdef DO_DOUBLE
if (!opt.Sp())
b = EmberRender<double>(opt);
b = EmberRender<double>(argc, argv, opt);
else
#endif
b = EmberRender<float>(opt);
b = EmberRender<float>(argc, argv, opt);
}
return b ? 0 : 1;