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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkResourceFileLocator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkResourceFileLocator.h"
#include "vtkLogger.h"
#include "vtkObjectFactory.h"
#include <vtksys/Encoding.hxx>
#include <vtksys/SystemTools.hxx>
#if defined(_WIN32) && !defined(__CYGWIN__)
#define VTK_PATH_SEPARATOR "\\"
#else
#define VTK_PATH_SEPARATOR "/"
#endif
#define VTK_FILE_LOCATOR_DEBUG_MESSAGE(...) \
vtkVLogF(static_cast<vtkLogger::Verbosity>(this->LogVerbosity), __VA_ARGS__)
#if defined(_WIN32) && !defined(__CYGWIN__)
// Implementation for Windows win32 code but not cygwin
#include <windows.h>
#else
#include <dlfcn.h>
#endif
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkResourceFileLocator);
//------------------------------------------------------------------------------
vtkResourceFileLocator::vtkResourceFileLocator()
: LogVerbosity(vtkLogger::VERBOSITY_TRACE)
{
}
//------------------------------------------------------------------------------
vtkResourceFileLocator::~vtkResourceFileLocator() = default;
//------------------------------------------------------------------------------
std::string vtkResourceFileLocator::Locate(
const std::string& anchor, const std::string& landmark, const std::string& defaultDir)
{
return this->Locate(anchor, { std::string() }, landmark, defaultDir);
}
//------------------------------------------------------------------------------
std::string vtkResourceFileLocator::Locate(const std::string& anchor,
const std::vector<std::string>& landmark_prefixes, const std::string& landmark,
const std::string& defaultDir)
{
vtkVLogScopeF(
static_cast<vtkLogger::Verbosity>(this->LogVerbosity), "looking for '%s'", landmark.c_str());
std::vector<std::string> path_components;
vtksys::SystemTools::SplitPath(anchor, path_components);
while (!path_components.empty())
{
std::string curanchor = vtksys::SystemTools::JoinPath(path_components);
for (const std::string& curprefix : landmark_prefixes)
{
std::string landmarkdir =
curprefix.empty() ? curanchor : curanchor + VTK_PATH_SEPARATOR + curprefix;
const std::string landmarktocheck = landmarkdir + VTK_PATH_SEPARATOR + landmark;
if (vtksys::SystemTools::FileExists(landmarktocheck))
{
VTK_FILE_LOCATOR_DEBUG_MESSAGE("trying file %s -- found!", landmarktocheck.c_str());
return landmarkdir;
}
else
{
VTK_FILE_LOCATOR_DEBUG_MESSAGE("trying file %s -- not found!", landmarktocheck.c_str());
}
}
path_components.pop_back();
}
return defaultDir;
}
//------------------------------------------------------------------------------
std::string vtkResourceFileLocator::GetLibraryPathForSymbolUnix(const char* symbolname)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
(void)symbolname;
return std::string();
#else
void* handle = dlsym(RTLD_DEFAULT, symbolname);
if (!handle)
{
return std::string();
}
Dl_info di;
int ret = dladdr(handle, &di);
if (ret == 0 || !di.dli_saddr || !di.dli_fname)
{
return std::string();
}
return std::string(di.dli_fname);
#endif
}
//------------------------------------------------------------------------------
std::string vtkResourceFileLocator::GetLibraryPathForSymbolWin32(const void* fptr)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(fptr, &mbi, sizeof(mbi));
wchar_t pathBuf[16384];
if (!GetModuleFileNameW(static_cast<HMODULE>(mbi.AllocationBase), pathBuf, sizeof(pathBuf)))
{
return std::string();
}
return vtksys::Encoding::ToNarrow(pathBuf);
#else
(void)fptr;
return std::string();
#endif
}
//------------------------------------------------------------------------------
void vtkResourceFileLocator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "LogVerbosity: " << this->LogVerbosity << endl;
}
VTK_ABI_NAMESPACE_END
|