Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usModuleSettings.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "usModuleSettings.h"
23 #include "usThreads_p.h"
24 #include "usStaticInit_p.h"
25 
26 #include <string>
27 #include <sstream>
28 #include <set>
29 #include <algorithm>
30 #include <cctype>
31 
32 US_BEGIN_NAMESPACE
33 
34 namespace {
35 
36  std::string RemoveTrailingPathSeparator(const std::string& in)
37  {
38 #ifdef US_PLATFORM_WINDOWS
39  const char separator = '\\';
40 #else
41  const char separator = '/';
42 #endif
43  if (in.empty()) return in;
44  std::string::const_iterator lastChar = --in.end();
45  while (lastChar != in.begin() && std::isspace(*lastChar)) lastChar--;
46  if (*lastChar != separator) lastChar++;
47  std::string::const_iterator firstChar = in.begin();
48  while (firstChar < lastChar && std::isspace(*firstChar)) firstChar++;
49  return std::string(firstChar, lastChar);
50  }
51 
52 }
53 
54 std::string ModuleSettings::CURRENT_MODULE_PATH()
55 {
56  static const std::string var = "us_current_module_path";
57  return var;
58 }
59 
60 struct ModuleSettingsPrivate : public MultiThreaded<>
61 {
62  ModuleSettingsPrivate()
63  : autoLoadPaths()
64  #ifdef US_ENABLE_AUTOLOADING_SUPPORT
65  , autoLoadingEnabled(true)
66  #else
67  , autoLoadingEnabled(false)
68  #endif
69  , autoLoadingDisabled(false)
70  , logLevel(DebugMsg)
71  {
72  autoLoadPaths.insert(ModuleSettings::CURRENT_MODULE_PATH());
73 
74  char* envPaths = getenv("US_AUTOLOAD_PATHS");
75  if (envPaths != NULL)
76  {
77  std::stringstream ss(envPaths);
78  std::string envPath;
79 #ifdef US_PLATFORM_WINDOWS
80  const char separator = ';';
81 #else
82  const char separator = ':';
83 #endif
84  while (std::getline(ss, envPath, separator))
85  {
86  std::string normalizedEnvPath = RemoveTrailingPathSeparator(envPath);
87  if (!normalizedEnvPath.empty())
88  {
89  extraPaths.insert(normalizedEnvPath);
90  }
91  }
92  }
93 
94  if (getenv("US_DISABLE_AUTOLOADING"))
95  {
96  autoLoadingDisabled = true;
97  }
98  }
99 
100  std::set<std::string> autoLoadPaths;
101  std::set<std::string> extraPaths;
102  bool autoLoadingEnabled;
103  bool autoLoadingDisabled;
104  std::string storagePath;
105  MsgType logLevel;
106 };
107 
108 US_GLOBAL_STATIC(ModuleSettingsPrivate, moduleSettingsPrivate)
109 
110 bool ModuleSettings::IsThreadingSupportEnabled()
111 {
112 #ifdef US_ENABLE_THREADING_SUPPORT
113  return true;
114 #else
115  return false;
116 #endif
117 }
118 
119 bool ModuleSettings::IsAutoLoadingEnabled()
120 {
121  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
122 #ifdef US_ENABLE_AUTOLOADING_SUPPORT
123  return !moduleSettingsPrivate()->autoLoadingDisabled &&
124  moduleSettingsPrivate()->autoLoadingEnabled;
125 #else
126  return false;
127 #endif
128 }
129 
130 void ModuleSettings::SetAutoLoadingEnabled(bool enable)
131 {
132  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
133  moduleSettingsPrivate()->autoLoadingEnabled = enable;
134 }
135 
136 ModuleSettings::PathList ModuleSettings::GetAutoLoadPaths()
137 {
138  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
139  ModuleSettings::PathList paths(moduleSettingsPrivate()->autoLoadPaths.begin(),
140  moduleSettingsPrivate()->autoLoadPaths.end());
141  paths.insert(paths.end(), moduleSettingsPrivate()->extraPaths.begin(),
142  moduleSettingsPrivate()->extraPaths.end());
143  std::sort(paths.begin(), paths.end());
144  paths.erase(std::unique(paths.begin(), paths.end()), paths.end());
145  return paths;
146 }
147 
148 void ModuleSettings::SetAutoLoadPaths(const PathList& paths)
149 {
150  PathList normalizedPaths;
151  normalizedPaths.resize(paths.size());
152  std::transform(paths.begin(), paths.end(), normalizedPaths.begin(), RemoveTrailingPathSeparator);
153 
154  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
155  moduleSettingsPrivate()->autoLoadPaths.clear();
156  moduleSettingsPrivate()->autoLoadPaths.insert(normalizedPaths.begin(), normalizedPaths.end());
157 }
158 
159 void ModuleSettings::AddAutoLoadPath(const std::string& path)
160 {
161  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
162  moduleSettingsPrivate()->autoLoadPaths.insert(RemoveTrailingPathSeparator(path));
163 }
164 
165 void ModuleSettings::SetStoragePath(const std::string &path)
166 {
167  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
168  moduleSettingsPrivate()->storagePath = RemoveTrailingPathSeparator(path);
169 }
170 
171 std::string ModuleSettings::GetStoragePath()
172 {
173  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
174  return moduleSettingsPrivate()->storagePath;
175 }
176 
177 void ModuleSettings::SetLogLevel(MsgType level)
178 {
179  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
180  moduleSettingsPrivate()->logLevel = level;
181 }
182 
183 MsgType ModuleSettings::GetLogLevel()
184 {
185  US_UNUSED(ModuleSettingsPrivate::Lock(moduleSettingsPrivate()));
186  return moduleSettingsPrivate()->logLevel;
187 }
188 
189 US_END_NAMESPACE
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
std::vector< std::string > PathList