mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-02-28 09:33:27 +01:00
- Updated to DMD frontend 1.041.
- Removed dmd/inifile.c , it's not under a free license, replaced with libconfig based config file.
This commit is contained in:
112
gen/configfile.cpp
Normal file
112
gen/configfile.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
#include "llvm/System/Path.h"
|
||||
|
||||
#include "libconfig.h++"
|
||||
|
||||
#include "gen/configfile.h"
|
||||
|
||||
#include "mars.h"
|
||||
|
||||
namespace sys = llvm::sys;
|
||||
|
||||
ConfigFile::ConfigFile()
|
||||
{
|
||||
cfg = new libconfig::Config;
|
||||
}
|
||||
|
||||
ConfigFile::~ConfigFile()
|
||||
{
|
||||
delete cfg;
|
||||
}
|
||||
|
||||
bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename)
|
||||
{
|
||||
|
||||
// try to find the config file
|
||||
|
||||
// 1) try the current working dir
|
||||
sys::Path p = sys::Path::GetCurrentDirectory();
|
||||
p.appendComponent(filename);
|
||||
|
||||
if (!p.exists())
|
||||
{
|
||||
// 2) try the user home dir
|
||||
p = sys::Path::GetUserHomeDirectory();
|
||||
p.appendComponent(filename);
|
||||
|
||||
if (!p.exists())
|
||||
{
|
||||
// 3) try the install-prefix/etc
|
||||
p = sys::Path(LDC_INSTALL_PREFIX);
|
||||
p.appendComponent(filename);
|
||||
|
||||
if (!p.exists())
|
||||
{
|
||||
// 4) try next to the executable
|
||||
p = sys::Path::GetMainExecutable(argv0, mainAddr);
|
||||
p.eraseComponent();
|
||||
p.appendComponent(filename);
|
||||
if (!p.exists())
|
||||
{
|
||||
// 5) fail load cfg, users still have the DFLAGS environment var
|
||||
std::cerr << "Error failed to locate the configuration file: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// read the cfg
|
||||
cfg->readFile(p.c_str());
|
||||
|
||||
// make sure there's a default group
|
||||
if (!cfg->exists("default"))
|
||||
{
|
||||
std::cerr << "no default settings in configuration file" << std::endl;
|
||||
return false;
|
||||
}
|
||||
libconfig::Setting& root = cfg->lookup("default");
|
||||
if (!root.isGroup())
|
||||
{
|
||||
std::cerr << "default is not a group" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// handle switches
|
||||
if (root.exists("switches"))
|
||||
{
|
||||
libconfig::Setting& arr = cfg->lookup("default.switches");
|
||||
int len = arr.getLength();
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
const char* v = arr[i];
|
||||
switches.push_back(v);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch(libconfig::FileIOException& fioe)
|
||||
{
|
||||
std::cerr << "Error reading configuration file: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
catch(libconfig::ParseException& pe)
|
||||
{
|
||||
std::cerr << "Error parsing configuration file: " << filename
|
||||
<< "(" << pe.getLine() << "): " << pe.getError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
std::cerr << "Unknown exception caught!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user