Mercurial > projects > ldc
comparison gen/configfile.cpp @ 1103:b30fe7e1dbb9
- Updated to DMD frontend 1.041.
- Removed dmd/inifile.c , it's not under a free license, replaced with libconfig based config file.
author | Tomas Lindquist Olsen <tomas.l.olsen gmail.com> |
---|---|
date | Thu, 12 Mar 2009 20:37:27 +0100 |
parents | |
children | 454f0c8acc4b |
comparison
equal
deleted
inserted
replaced
1102:ae950bd712d3 | 1103:b30fe7e1dbb9 |
---|---|
1 #include <iostream> | |
2 #include <string> | |
3 #include <cassert> | |
4 | |
5 #include "llvm/System/Path.h" | |
6 | |
7 #include "libconfig.h++" | |
8 | |
9 #include "gen/configfile.h" | |
10 | |
11 #include "mars.h" | |
12 | |
13 namespace sys = llvm::sys; | |
14 | |
15 ConfigFile::ConfigFile() | |
16 { | |
17 cfg = new libconfig::Config; | |
18 } | |
19 | |
20 ConfigFile::~ConfigFile() | |
21 { | |
22 delete cfg; | |
23 } | |
24 | |
25 bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename) | |
26 { | |
27 | |
28 // try to find the config file | |
29 | |
30 // 1) try the current working dir | |
31 sys::Path p = sys::Path::GetCurrentDirectory(); | |
32 p.appendComponent(filename); | |
33 | |
34 if (!p.exists()) | |
35 { | |
36 // 2) try the user home dir | |
37 p = sys::Path::GetUserHomeDirectory(); | |
38 p.appendComponent(filename); | |
39 | |
40 if (!p.exists()) | |
41 { | |
42 // 3) try the install-prefix/etc | |
43 p = sys::Path(LDC_INSTALL_PREFIX); | |
44 p.appendComponent(filename); | |
45 | |
46 if (!p.exists()) | |
47 { | |
48 // 4) try next to the executable | |
49 p = sys::Path::GetMainExecutable(argv0, mainAddr); | |
50 p.eraseComponent(); | |
51 p.appendComponent(filename); | |
52 if (!p.exists()) | |
53 { | |
54 // 5) fail load cfg, users still have the DFLAGS environment var | |
55 std::cerr << "Error failed to locate the configuration file: " << filename << std::endl; | |
56 return false; | |
57 } | |
58 } | |
59 } | |
60 } | |
61 | |
62 try | |
63 { | |
64 // read the cfg | |
65 cfg->readFile(p.c_str()); | |
66 | |
67 // make sure there's a default group | |
68 if (!cfg->exists("default")) | |
69 { | |
70 std::cerr << "no default settings in configuration file" << std::endl; | |
71 return false; | |
72 } | |
73 libconfig::Setting& root = cfg->lookup("default"); | |
74 if (!root.isGroup()) | |
75 { | |
76 std::cerr << "default is not a group" << std::endl; | |
77 return false; | |
78 } | |
79 | |
80 // handle switches | |
81 if (root.exists("switches")) | |
82 { | |
83 libconfig::Setting& arr = cfg->lookup("default.switches"); | |
84 int len = arr.getLength(); | |
85 for (int i=0; i<len; i++) | |
86 { | |
87 const char* v = arr[i]; | |
88 switches.push_back(v); | |
89 } | |
90 } | |
91 | |
92 } | |
93 catch(libconfig::FileIOException& fioe) | |
94 { | |
95 std::cerr << "Error reading configuration file: " << filename << std::endl; | |
96 return false; | |
97 } | |
98 catch(libconfig::ParseException& pe) | |
99 { | |
100 std::cerr << "Error parsing configuration file: " << filename | |
101 << "(" << pe.getLine() << "): " << pe.getError() << std::endl; | |
102 return false; | |
103 } | |
104 catch(...) | |
105 { | |
106 std::cerr << "Unknown exception caught!" << std::endl; | |
107 return false; | |
108 } | |
109 | |
110 return true; | |
111 } | |
112 |