dmpapplication.cpp
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**************************************************************************
* file: dmpapplication.cpp
* Author: wanzhongping
* Date: 2021-02-24 11:23:15
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpapplication.h"
#include <iostream>
#include <fstream>
#include "dmpconfig.h"
#include "dmplogger.h"
bool DmpApplication::running_from_build_dir_ = false;
bool DmpApplication::initialized_ = false;
std::string DmpApplication::plugin_path_ = "";
std::string DmpApplication::prefix_path_ = "";
std::string DmpApplication::libexec_path_ = "";
std::string DmpApplication::library_path_ = "";
std::string DmpApplication::build_source_path_ = "";
std::string DmpApplication::build_output_path_ = "";
std::string DmpApplication::pkg_data_path_ = "";
DmpApplication::DmpApplication()
{
std::cout << "constructor called!" << std::endl;
LOGGER_DEBUG("constructor called!");
}
DmpApplication::~DmpApplication()
{
std::cout << "destructor called!" << std::endl;
LOGGER_DEBUG("destructor called!");
}
DmpApplication &DmpApplication::Instance()
{
static DmpApplication instance;
return instance;
}
void DmpApplication::Init()
{
LOGGER_INFO("DmpApplication init...");
ResolvePkgPath();
if (running_from_build_dir_)
{
// we run from source directory - not installed to destination (specified prefix)
prefix_path_ = "";
set_plugin_path(build_source_path_ + '/' + DMAP_PLUGIN_SUBDIR);
set_pkg_data_path(build_output_path_ + "/data"); // in buildDir/data - used for: doc, resources, svg
library_path_ = build_output_path_ + '/' + DMAP_LIB_SUBDIR + '/';
libexec_path_ = build_output_path_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}
else
{
char *prefix_path = getenv("DMAP_PREFIX_PATH");
if (!prefix_path)
{
set_prefix_path("/usr/local");
}else{
set_prefix_path(prefix_path);
}
}
initialized_ = true;
LOGGER_INFO("DmpApplication init done");
}
bool DmpApplication::IsRunningFromBuildDir()
{
return running_from_build_dir_;
}
std::string DmpApplication::ApplicationDirPath()
{
boost::filesystem::path libDir = boost::filesystem::current_path();
return libDir.c_str();
}
std::string DmpApplication::ResolvePkgPath()
{
std::string app_path = ApplicationDirPath();
if (!app_path.empty() || getenv("DMAP_PREFIX_PATH"))
{
std::string prefix_Path = getenv("DMAP_PREFIX_PATH") ? getenv("DMAP_PREFIX_PATH") : app_path;
// check if DMAP is run from build directory (not the install directory)
boost::filesystem::path dmapbuildpath;
boost::filesystem::path dir(prefix_Path);
if (SearchFile(dir, "dmapbuildpath.txt", dmapbuildpath))
{
std::cout << dmapbuildpath.c_str() << std::endl;
running_from_build_dir_ = true;
std::ifstream fin(dmapbuildpath.c_str());
std::getline(fin, build_source_path_);
boost::trim(build_source_path_);
std::getline(fin, build_output_path_);
boost::trim(build_output_path_);
}
if (IsRunningFromBuildDir)
{
return build_output_path_ + "/Data";
}
else
{
return prefix_Path + '/' + DMAP_DATA_SUBDIR;
}
}
return "";
}
std::string DmpApplication::pkg_data_path()
{
return pkg_data_path_;
}
void DmpApplication::set_pkg_data_path(const std::string &pkg_data_path)
{
pkg_data_path_ = pkg_data_path;
}
std::string DmpApplication::library_path()
{
return library_path_;
}
void DmpApplication::set_prefix_path(const std::string &prefix_path)
{
LOGGER_DEBUG("set_prefix_path: " + prefix_path);
prefix_path_ = prefix_path;
if(IsRunningFromBuildDir)
{
set_plugin_path(prefix_path_ + '/' + DMAP_PLUGIN_SUBDIR);
set_pkg_data_path(prefix_path_ + '/' + DMAP_DATA_SUBDIR);
}
library_path_ = prefix_path_ + '/' + DMAP_LIB_SUBDIR + '/';
libexec_path_ = prefix_path_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}
bool DmpApplication::SearchFile(const boost::filesystem::path &dir, const std::string file_name, boost::filesystem::path &path)
{
if (!boost::filesystem::exists(dir))
{
return false;
}
boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator iter(dir); iter != end_iter; ++iter)
{
if (boost::filesystem::is_regular_file(iter->status()) && iter->path().filename() == file_name)
{
path = iter->path();
return true;
}
if (boost::filesystem::is_directory(iter->status()))
{
if (SearchFile(iter->path(), file_name, path))
{
return true;
}
}
}
return false;
}
void DmpApplication::set_plugin_path(const std::string &plugin_path)
{
plugin_path_ = plugin_path;
}
std::string DmpApplication::prefix_path()
{
return prefix_path_;
}
std::string DmpApplication::libexec_path()
{
return libexec_path_;
}
std::string DmpApplication::plugin_path()
{
return plugin_path_;
}
bool DmpApplication::CreateDatabase()
{
return false;
}
std::string DmpApplication::build_output_path()
{
return build_output_path_;
}
std::string DmpApplication::DMapSettingsDirPath()
{
return "";
}