comparison gen/configfile.cpp @ 1489:a048f31bf9f6

Move locating the configuration file into a separate function. Also look in PREFIX/etc/ldc. Fixes #322.
author Christian Kamm <kamm incasoftware de>
date Mon, 08 Jun 2009 19:48:20 +0200
parents 7d3b47852a7a
children cd4478b47b10
comparison
equal deleted inserted replaced
1488:ca31a8e9c42b 1489:a048f31bf9f6
38 return p; 38 return p;
39 } 39 }
40 #endif 40 #endif
41 41
42 42
43 bool ConfigFile::locate(sys::Path& p, const char* argv0, void* mainAddr, const char* filename)
44 {
45 // 1) try the current working dir
46 p = sys::Path::GetCurrentDirectory();
47 p.appendComponent(filename);
48 if (p.exists())
49 return true;
50
51 // 2) try the user home dir
52 p = sys::Path::GetUserHomeDirectory();
53 p.appendComponent(filename);
54 if (p.exists())
55 return true;
56
57 // 3) try the install-prefix/etc
58 p = sys::Path(LDC_INSTALL_PREFIX);
59 #if !_WIN32
60 // Does Windows need something similar?
61 p.appendComponent("etc");
62 #endif
63 p.appendComponent(filename);
64 if (p.exists())
65 return true;
66
67 // 4) try the install-prefix/etc/ldc
68 p = sys::Path(LDC_INSTALL_PREFIX);
69 #if !_WIN32
70 // Does Windows need something similar?
71 p.appendComponent("etc");
72 p.appendComponent("ldc");
73 #endif
74 p.appendComponent(filename);
75 if (p.exists())
76 return true;
77
78 // 5) try next to the executable
79 #if _WIN32
80 p = ConfigGetExePath(p);
81 #else
82 p = sys::Path::GetMainExecutable(argv0, mainAddr);
83 p.eraseComponent();
84 #endif
85 p.appendComponent(filename);
86 if (p.exists())
87 return true;
88
89 return false;
90 }
91
43 bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename) 92 bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename)
44 { 93 {
45 94 sys::Path p;
46 #if _WIN32 95 if (!locate(p, argv0, mainAddr, filename))
47 std::string exeDirectoryName; 96 {
48 #endif 97 // failed to find cfg, users still have the DFLAGS environment var
49 // try to find the config file 98 std::cerr << "Error failed to locate the configuration file: " << filename << std::endl;
50 99 return false;
51 // 1) try the current working dir
52 sys::Path p = sys::Path::GetCurrentDirectory();
53 p.appendComponent(filename);
54
55 if (!p.exists())
56 {
57 // 2) try the user home dir
58 p = sys::Path::GetUserHomeDirectory();
59 p.appendComponent(filename);
60
61 if (!p.exists())
62 {
63 // 3) try the install-prefix/etc
64 p = sys::Path(LDC_INSTALL_PREFIX);
65 #if !_WIN32
66 // Does Window need something similar?
67 p.appendComponent("etc");
68 #endif
69 p.appendComponent(filename);
70
71 if (!p.exists())
72 {
73 #if _WIN32
74 p = ConfigGetExePath(p);
75 exeDirectoryName = p.toString();
76 #else
77 // 4) try next to the executable
78 p = sys::Path::GetMainExecutable(argv0, mainAddr);
79 p.eraseComponent();
80 #endif
81 p.appendComponent(filename);
82 if (!p.exists())
83 {
84 // 5) fail load cfg, users still have the DFLAGS environment var
85 std::cerr << "Error failed to locate the configuration file: " << filename << std::endl;
86 return false;
87 }
88 }
89 }
90 } 100 }
91 101
92 // save config file path for -v output 102 // save config file path for -v output
93 pathstr = p.toString(); 103 pathstr = p.toString();
94 104
114 if (root.exists("switches")) 124 if (root.exists("switches"))
115 { 125 {
116 std::string binpathkey = "%%ldcbinarypath%%"; 126 std::string binpathkey = "%%ldcbinarypath%%";
117 127
118 #if _WIN32 128 #if _WIN32
119 std::string binpath; 129 sys::Path p;
120 //This will happen if ldc.conf is found somewhere other than 130 p = ConfigGetExePath(p);
121 //beside the ldc executable 131 std::string binpath = p.toString();
122 if (exeDirectoryName == "")
123 {
124 sys::Path p;
125 p = ConfigGetExePath(p);
126 exeDirectoryName = p.toString();
127 }
128 binpath = exeDirectoryName;
129 #else 132 #else
130 std::string binpath = sys::Path::GetMainExecutable(argv0, mainAddr).getDirname(); 133 std::string binpath = sys::Path::GetMainExecutable(argv0, mainAddr).getDirname();
131 #endif 134 #endif
132 135
133 136