Debian Codegraph @

src/yvalve/config/os/win32/config_root.cpp

raw
1 /*
2 * The contents of this file are subject to the Initial
3 * Developer's Public License Version 1.0 (the "License");
4 * you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
6 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
7 *
8 * Software distributed under the License is distributed AS IS,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
10 * See the License for the specific language governing rights
11 * and limitations under the License.
12 *
13 * The Original Code was created by Dmitry Yemanov
14 * for the Firebird Open Source RDBMS project.
15 *
16 * Copyright (c) 2002 Dmitry Yemanov <[email protected]>
17 * and all contributors signed below.
18 *
19 * All Rights Reserved.
20 * Contributor(s): ______________________________________.
21 * Adriano dos Santos Fernandes
22 */
23
24 #include "firebird.h"
25
26 #include <windows.h>
27
28 #include "fb_types.h"
29 #include "../../../../common/classes/fb_string.h"
30 #include "../../../../common/dllinst.h"
31 #include "../../../../yvalve/config/os/config_root.h"
32
33 using Firebird::PathName;
34
35
36 /******************************************************************************
37 *
38 * Platform-specific root locator
39 */
40 namespace {
41
42 bool getPathFromHInstance(PathName& root)
43 {
44 const HINSTANCE hDllInst = Firebird::hDllInst;
45 if (!hDllInst)
46 {
47 return false;
48 }
49
50 char* filename = root.getBuffer(MAX_PATH);
51 GetModuleFileName(hDllInst, filename, MAX_PATH);
52
53 root.recalculate_length();
54
55 return root.hasData();
56 }
57
58 } // namespace
59
60
61 void ConfigRoot::osConfigRoot()
62 {
63 root_dir = install_dir;
64 }
65
66 void ConfigRoot::osConfigInstallDir()
67 {
68 // get the pathname of the running dll / executable
69 PathName module_path;
70 if (!getPathFromHInstance(module_path))
71 {
72 module_path = fb_utils::get_process_name();
73 }
74
75 if (module_path.hasData())
76 {
77 // get rid of the filename
78 PathName bin_dir, file_name;
79 PathUtils::splitLastComponent(bin_dir, file_name, module_path);
80
81 // search for the configuration file in the bin directory
82 PathUtils::concatPath(file_name, bin_dir, Firebird::CONFIG_FILE);
83 DWORD attributes = GetFileAttributes(file_name.c_str());
84 if (attributes == INVALID_FILE_ATTRIBUTES || attributes == FILE_ATTRIBUTE_DIRECTORY)
85 {
86 // search for the configuration file in the parent directory
87 PathName parent_dir;
88 PathUtils::splitLastComponent(parent_dir, file_name, bin_dir);
89
90 if (parent_dir.hasData())
91 {
92 PathUtils::concatPath(file_name, parent_dir, Firebird::CONFIG_FILE);
93 attributes = GetFileAttributes(file_name.c_str());
94 if (attributes != INVALID_FILE_ATTRIBUTES && attributes != FILE_ATTRIBUTE_DIRECTORY)
95 {
96 install_dir = parent_dir;
97 }
98 }
99 }
100
101 if (install_dir.isEmpty())
102 {
103 install_dir = bin_dir;
104 }
105 }
106
107 if (install_dir.isEmpty())
108 {
109 // As a last resort get it from the default install directory
110 install_dir = FB_PREFIX;
111 }
112
113 PathUtils::ensureSeparator(install_dir);
114 }