22 #include "usUtils_p.h"
34 #ifdef US_PLATFORM_POSIX
40 #ifndef WIN32_LEAN_AND_MEAN
41 #define WIN32_LEAN_AND_MEAN
54 #if !defined(US_PLATFORM_LINUX)
55 std::string library_suffix()
57 #ifdef US_PLATFORM_WINDOWS
59 #elif defined(US_PLATFORM_APPLE)
67 #ifdef US_PLATFORM_POSIX
69 const char DIR_SEP =
'/';
71 bool load_impl(
const std::string& modulePath)
73 void* handle = dlopen(modulePath.c_str(), RTLD_NOW | RTLD_LOCAL);
78 return (handle != NULL);
81 #elif defined(US_PLATFORM_WINDOWS)
83 const char DIR_SEP =
'\\';
85 bool load_impl(
const std::string& modulePath)
87 void* handle = LoadLibrary(modulePath.c_str());
92 return (handle != NULL);
97 #ifdef US_ENABLE_AUTOLOADING_SUPPORT
98 #error "Missing load_impl implementation for this platform."
100 bool load_impl(
const std::string&) {
return false; }
111 std::vector<std::string> loadedModules;
113 std::string loadPath = absoluteBasePath + DIR_SEP + subDir;
120 std::size_t indexOfLastSeparator = absoluteBasePath.find_last_of(DIR_SEP);
121 if (indexOfLastSeparator != std::string::npos)
123 std::string intermediateDir = absoluteBasePath.substr(indexOfLastSeparator+1);
124 bool equalSubDir = intermediateDir.size() == std::strlen(CMAKE_INTDIR);
125 for (std::size_t i = 0; equalSubDir && i < intermediateDir.size(); ++i)
127 if (std::tolower(intermediateDir[i]) != std::tolower(CMAKE_INTDIR[i]))
134 loadPath = absoluteBasePath.substr(0, indexOfLastSeparator+1) + subDir + DIR_SEP + CMAKE_INTDIR;
135 dir =
opendir(loadPath.c_str());
143 struct dirent *ent = NULL;
144 while ((ent =
readdir(dir)) != NULL)
146 bool loadFile =
true;
147 #ifdef _DIRENT_HAVE_D_TYPE
154 std::string entryFileName(ent->
d_name);
158 #if !defined(US_PLATFORM_LINUX)
159 if (entryFileName.rfind(library_suffix()) != (entryFileName.size() - library_suffix().size()))
164 if (!loadFile)
continue;
166 std::string libPath = loadPath;
167 if (!libPath.empty() && libPath.find_last_of(DIR_SEP) != libPath.size() -1)
171 libPath += entryFileName;
172 US_DEBUG <<
"Auto-loading module " << libPath;
174 if (!load_impl(libPath))
176 US_WARN <<
"Auto-loading of module " << libPath <<
" failed.";
180 loadedModules.push_back(libPath);
185 return loadedModules;
190 std::vector<std::string> loadedModules;
194 return loadedModules;
199 std::size_t indexOfLastSeparator = moduleInfo.
location.find_last_of(DIR_SEP);
200 std::string moduleBasePath = moduleInfo.
location.substr(0, indexOfLastSeparator);
202 for (ModuleSettings::PathList::iterator i = autoLoadPaths.begin();
203 i != autoLoadPaths.end(); ++i)
205 if (*i == ModuleSettings::CURRENT_MODULE_PATH())
214 std::sort(autoLoadPaths.begin(), autoLoadPaths.end());
215 autoLoadPaths.erase(std::unique(autoLoadPaths.begin(), autoLoadPaths.end()), autoLoadPaths.end());
216 for (ModuleSettings::PathList::iterator i = autoLoadPaths.begin();
217 i != autoLoadPaths.end(); ++i)
219 if (i->empty())
continue;
221 loadedModules.insert(loadedModules.end(), paths.begin(), paths.end());
223 return loadedModules;
236 #ifdef US_PLATFORM_POSIX
237 return std::string(strerror(errno));
241 DWORD dw = GetLastError();
244 FORMAT_MESSAGE_ALLOCATE_BUFFER |
245 FORMAT_MESSAGE_FROM_SYSTEM |
246 FORMAT_MESSAGE_IGNORE_INSERTS,
249 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
253 std::string errMsg((LPCTSTR)lpMsgBuf);
274 (*handler)(msgType, buf);
278 fprintf(stderr,
"%s\n", buf);
282 if (msgType == ErrorMsg)
284 #if defined(_MSC_VER) && !defined(NDEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
286 int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
287 _CrtSetReportMode(_CRT_ERROR, reportMode);
288 int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, CppMicroServices_VERSION_STR, buf);
289 if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW)
295 #ifdef US_PLATFORM_POSIX
303 #ifdef US_HAVE_CXXABI_H
309 ::std::string result;
310 #ifdef US_HAVE_CXXABI_H
312 char* demangled = abi::__cxa_demangle(typeInfo.name(), 0, 0, &status);
313 if (demangled && status == 0)
318 #elif defined(US_PLATFORM_WINDOWS)
319 const char* demangled = typeInfo.name();
320 if (demangled != NULL)
325 while (pos != std::string::npos)
327 if ((pos = result.find(
"struct ", pos)) != std::string::npos)
329 result = result.substr(0, pos) + result.substr(pos + 7);
335 while (pos != std::string::npos)
337 if ((pos = result.find(
"class ", pos)) != std::string::npos)
339 result = result.substr(0, pos) + result.substr(pos + 6);
static MsgHandler handler
US_Core_EXPORT::std::string GetDemangledName(const ::std::type_info &typeInfo)
static int closedir(DIR *dirp)
static DIR * opendir(const char *dirname)
static std::string GetLastErrorStr()
void message_output(MsgType msgType, const char *buf)
static struct dirent * readdir(DIR *dirp)
std::vector< std::string > AutoLoadModulesFromPath(const std::string &absoluteBasePath, const std::string &subDir)
std::vector< std::string > AutoLoadModules(const ModuleInfo &moduleInfo)
MsgHandler installMsgHandler(MsgHandler h)
std::string GetLastErrorStr()
std::vector< std::string > PathList