dmpapplication.cpp
4.9 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
/**************************************************************************
* 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 <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
DmpApplication::DmpApplication()
{
runningFromBuildDir_ = false;
initialized_ = false;
}
DmpApplication::~DmpApplication()
{
}
DmpApplication* DmpApplication::Instance()
{
static DmpApplication instance;
return &instance;
}
void DmpApplication::initialize()
{
resolvePkgPath();
if (runningFromBuildDir_)
{
prefixPath_ = "";
setPluginPath(buildSourcePath_ + '/' + DMAP_PLUGIN_SUBDIR);
setPkgDataPath(buildOutputPath_ + "/data"); // in buildDir/data - used for: doc, resources, svg
libraryPath_ = buildOutputPath_ + '/' + DMAP_LIB_SUBDIR + '/';
libexecPath_ = buildOutputPath_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}
else
{
char *prefixPath = getenv("DMAP_PREFIX_PATH");
if (!prefixPath)
{
setPrefixPath("/usr/local");
}else{
setPrefixPath(prefixPath);
}
}
initialized_ = true;
}
bool DmpApplication::isRunningFromBuildDir()
{
return runningFromBuildDir_;
}
std::string DmpApplication::applicationDirPath()
{
boost::filesystem::path libDir = boost::filesystem::current_path();
return libDir.c_str();
}
std::string DmpApplication::resolvePkgPath()
{
std::string appPath = applicationDirPath();
if (!appPath.empty() || getenv("DMAP_PREFIX_PATH"))
{
std::string prefixPath = getenv("DMAP_PREFIX_PATH") ? getenv("DMAP_PREFIX_PATH") : appPath;
// check if DMAP is run from build directory (not the install directory)
boost::filesystem::path dmapbuildpath;
boost::filesystem::path dir(prefixPath);
if (searchFile(dir, "dmapbuildpath.txt", dmapbuildpath))
{
std::cout << dmapbuildpath.c_str() << std::endl;
runningFromBuildDir_ = true;
std::ifstream fin(dmapbuildpath.c_str());
std::getline(fin, buildSourcePath_);
boost::trim(buildSourcePath_);
std::getline(fin, buildOutputPath_);
boost::trim(buildOutputPath_);
}
if (isRunningFromBuildDir())
{
return buildOutputPath_ + "/Data";
}
else
{
return prefixPath + '/' + DMAP_DATA_SUBDIR;
}
}
return "";
}
std::string DmpApplication::pkgDataPath()
{
if(pkgDataPath_.empty()) {
return resolvePkgPath();
} else {
return pkgDataPath_;
}
}
void DmpApplication::setPkgDataPath(const std::string &pkgDataPath)
{
pkgDataPath_ = pkgDataPath;
}
std::string DmpApplication::libraryPath()
{
return libraryPath_;
}
void DmpApplication::setPrefixPath(const std::string &prefixPath)
{
prefixPath_ = prefixPath;
if(isRunningFromBuildDir())
{
setPluginPath(prefixPath_ + '/' + DMAP_PLUGIN_SUBDIR);
setPkgDataPath(prefixPath_ + '/' + DMAP_DATA_SUBDIR);
}
libraryPath_ = prefixPath_ + '/' + DMAP_LIB_SUBDIR + '/';
libexecPath_ = prefixPath_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}
bool DmpApplication::searchFile(const boost::filesystem::path &dir, const std::string fileName, 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() == fileName)
{
path = iter->path();
return true;
}
if (boost::filesystem::is_directory(iter->status()))
{
if (searchFile(iter->path(), fileName, path))
{
return true;
}
}
}
return false;
}
void DmpApplication::setPluginPath(const std::string &pluginPath)
{
pluginPath_ = pluginPath;
}
std::string DmpApplication::prefixPath()
{
return prefixPath_;
}
std::string DmpApplication::libexecPath()
{
return libexecPath_;
}
std::string DmpApplication::pluginPath()
{
return pluginPath_;
}
// bool DmpApplication::CreateDatabase()
// {
// return false;
// }
std::string DmpApplication::buildOutputPath()
{
return buildOutputPath_;
}
std::string DmpApplication::dmapSettingsDirPath()
{
return "";
}