[pkg-d-commits] [ldc] 16/95: adapt config file to use adhoc parser
Matthias Klumpp
mak at moszumanska.debian.org
Thu Jul 13 20:53:56 UTC 2017
This is an automated email from the git hooks/post-receive script.
mak pushed a commit to annotated tag v1.3.0-beta1
in repository ldc.
commit 2c501301d96834b9de9cd67f5729b9264f885c9b
Author: Remi THEBAULT <remi.thebault at gmail.com>
Date: Fri Feb 24 23:18:35 2017 +0100
adapt config file to use adhoc parser
---
driver/configfile.cpp | 59 ++------------------
driver/configfile.d | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++
driver/configfile.h | 24 ++++----
3 files changed, 166 insertions(+), 66 deletions(-)
diff --git a/driver/configfile.cpp b/driver/configfile.cpp
index 9e33884..4959d7d 100644
--- a/driver/configfile.cpp
+++ b/driver/configfile.cpp
@@ -10,7 +10,6 @@
#include "driver/configfile.h"
#include "driver/exe_path.h"
#include "mars.h"
-#include "libconfig.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
@@ -85,12 +84,8 @@ static bool ReadPathFromRegistry(llvm::SmallString<128> &p) {
}
#endif
-ConfigFile::ConfigFile() {
- cfg = new config_t;
- config_init(cfg);
-}
-bool ConfigFile::locate() {
+bool ConfigFile::locate(std::string& pathstr) {
// temporary configuration
llvm::SmallString<128> p;
@@ -175,6 +170,7 @@ bool ConfigFile::locate() {
}
bool ConfigFile::read(const char *explicitConfFile, const char* section) {
+ std::string pathstr;
// explicitly provided by user in command line?
if (explicitConfFile) {
const std::string clPath = explicitConfFile;
@@ -193,56 +189,13 @@ bool ConfigFile::read(const char *explicitConfFile, const char* section) {
// locate file automatically if path is not set yet
if (pathstr.empty()) {
- if (!locate()) {
+ if (!locate(pathstr)) {
return false;
}
}
- // read the cfg
- if (!config_read_file(cfg, pathstr.c_str())) {
- std::cerr << "error reading configuration file" << std::endl;
- return false;
- }
-
- config_setting_t *root = nullptr;
- if (section && *section)
- root = config_lookup(cfg, section);
-
- // make sure there's a default group
- if (!root) {
- section = "default";
- root = config_lookup(cfg, section);
- }
- if (!root) {
- std::cerr << "no default settings in configuration file" << std::endl;
- return false;
- }
- if (!config_setting_is_group(root)) {
- std::cerr << section << " is not a group" << std::endl;
- return false;
- }
-
- // handle switches
- if (config_setting_t *sw = config_setting_get_member(root, "switches")) {
- // replace all %%ldcbinarypath%% occurrences by the path to the
- // LDC bin directory (using forward slashes)
- std::string binpathkey = "%%ldcbinarypath%%";
-
- std::string binpath = exe_path::getBinDir();
- std::replace(binpath.begin(), binpath.end(), '\\', '/');
-
- int len = config_setting_length(sw);
- for (int i = 0; i < len; i++) {
- std::string v(config_setting_get_string(config_setting_get_elem(sw, i)));
-
- size_t p;
- while (std::string::npos != (p = v.find(binpathkey))) {
- v.replace(p, binpathkey.size(), binpath);
- }
-
- switches.push_back(strdup(v.c_str()));
- }
- }
+ pathcstr = strdup(pathstr.c_str());
+ auto binpath = exe_path::getBinDir();
- return true;
+ return readConfig(pathcstr, section, binpath.c_str());
}
diff --git a/driver/configfile.d b/driver/configfile.d
new file mode 100644
index 0000000..7edde4b
--- /dev/null
+++ b/driver/configfile.d
@@ -0,0 +1,149 @@
+//===-- driver/configfile.d - LDC config file handling ------------*- D -*-===//
+//
+// LDC – the LLVM D compiler
+//
+// This file is distributed under the BSD-style LDC license. See the LICENSE
+// file for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Handles reading and parsing of an LDC config file (ldc.conf/ldc2.conf).
+//
+//===----------------------------------------------------------------------===//
+module driver.configfile;
+
+import driver.config;
+import core.stdc.stdio;
+import core.stdc.string;
+
+
+string fromStringz(const(char)* cstr)
+{
+ return cstr[0 .. strlen(cstr)].idup;
+}
+
+immutable(char)* toStringz(in string s)
+{
+ auto nullTerm = s ~ '\0';
+ return nullTerm.ptr;
+}
+
+
+string prepareBinDir(const(char)* binDir)
+{
+ immutable len = strlen(binDir);
+ auto res = binDir[0 .. len].dup;
+ foreach (ref c; res)
+ {
+ if (c == '\\') c = '/';
+ }
+ return cast(string)res; // assumeUnique
+}
+
+
+ArraySetting findSwitches(Setting s)
+{
+ auto grp = cast(GroupSetting)s;
+ if (!grp) return null;
+ foreach (c; grp.children)
+ {
+ if (c.name == "switches")
+ {
+ return cast(ArraySetting)c;
+ }
+ }
+ return null;
+}
+
+
+string replace(string str, string pattern, string replacement)
+{
+ string res;
+ size_t cap = str.length;
+ if (replacement.length > pattern.length)
+ cap += replacement.length - pattern.length;
+ reserve(res, cap);
+
+ while(str.length)
+ {
+ if (str.length < pattern.length)
+ {
+ res ~= str;
+ str = null;
+ }
+ else if (str[0 .. pattern.length] == pattern)
+ {
+ res ~= replacement;
+ str = str[pattern.length .. $];
+ }
+ else
+ {
+ res ~= str[0];
+ str = str[1 .. $];
+ }
+ }
+ return res;
+}
+
+struct ConfigFile
+{
+public:
+
+ alias s_iterator = const(char)**;
+
+private:
+
+ // representation
+
+ const(char)* pathcstr;
+ s_iterator switches_b;
+ s_iterator switches_e;
+
+ extern(C++)
+ bool readConfig(const(char)* cfPath, const(char)* section, const(char)* binDir)
+ {
+ immutable dBinDir = prepareBinDir(binDir);
+ immutable dSec = fromStringz(section);
+
+ try
+ {
+ auto settings = parseConfigFile(cfPath);
+
+ ArraySetting secSwitches;
+ ArraySetting defSwitches;
+
+ foreach (s; settings)
+ {
+ if (s.name == dSec) {
+ secSwitches = findSwitches(s);
+ }
+ else if (s.name == "default")
+ {
+ defSwitches = findSwitches(s);
+ }
+ }
+
+ auto switches = secSwitches ? secSwitches : defSwitches;
+ if (!switches)
+ {
+ throw new Exception("could not look up switches in "~cfPath[0 .. strlen(cfPath)].idup);
+ }
+
+ auto slice = new const(char)*[switches.vals.length];
+ foreach (i, sw; switches.vals)
+ {
+ slice[i] = toStringz(sw.replace("%%ldcbinarypath%%", dBinDir));
+ }
+
+ switches_b = slice.ptr;
+ switches_e = slice.ptr+slice.length;
+
+ return true;
+ }
+ catch (Exception ex)
+ {
+ fprintf(stderr, "%s\n", toStringz(ex.msg));
+ return false;
+ }
+ }
+}
diff --git a/driver/configfile.h b/driver/configfile.h
index 4b1164c..15eea1d 100644
--- a/driver/configfile.h
+++ b/driver/configfile.h
@@ -16,31 +16,29 @@
#include <string>
#include <vector>
-#include "llvm/ADT/SmallString.h"
-#include "libconfig.h"
class ConfigFile {
public:
- typedef std::vector<const char *> s_vector;
- typedef s_vector::iterator s_iterator;
+ using s_iterator = const char **;
public:
- ConfigFile();
- bool read(const char *explicitConfFile, const char* section);
+ bool read(const char *explicitConfFile, const char *section);
- s_iterator switches_begin() { return switches.begin(); }
- s_iterator switches_end() { return switches.end(); }
+ s_iterator switches_begin() { return switches_b; }
+ s_iterator switches_end() { return switches_e; }
- const std::string &path() { return pathstr; }
+ std::string path() { return std::string(pathcstr); }
private:
- bool locate();
+ bool locate(std::string& pathstr);
- config_t *cfg;
- std::string pathstr;
+ // implemented in D
+ bool readConfig(const char* cfPath, const char* section, const char* binDir);
- s_vector switches;
+ const char *pathcstr =nullptr;
+ s_iterator switches_b =nullptr;
+ s_iterator switches_e =nullptr;
};
#endif // LDC_DRIVER_CONFIGFILE_H
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git
More information about the pkg-d-commits
mailing list