Skip to content

Commit

Permalink
Merge branch 'master' into pymem
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Dec 2, 2023
2 parents 3b58615 + 0a0430f commit 5db17af
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 161 deletions.
16 changes: 6 additions & 10 deletions g2o/apps/g2o_cli/dl_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <sys/types.h>

#include <algorithm>
#include <regex>

#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/logger.h"
Expand All @@ -42,16 +42,12 @@ namespace g2o {
int DlWrapper::openLibraries(const std::string& directory,
const std::string& pattern) {
G2O_TRACE("Loading libraries from {} pattern {}", directory, pattern);
const std::string searchPattern =
pattern.empty() ? directory + "/*" : directory + "/" + pattern;
const std::vector<std::string> matchingFiles =
getFilesByPattern(searchPattern.c_str());
std::vector<std::string> matchingFiles =
getFilesByPattern(directory, std::regex(pattern));

int numLibs = 0;
for (const auto& filename : matchingFiles) {
if (find(filenames_.begin(), filenames_.end(), filename) !=
filenames_.end())
continue;
for (const std::string& filename : matchingFiles) {
if (filenames_.count(filename) != 0) continue;

// open the lib
G2O_TRACE("Loading {}", filename);
Expand Down Expand Up @@ -93,7 +89,7 @@ bool DlWrapper::openLibrary(const std::string& filename) {

// cerr << "loaded " << filename << endl;

filenames_.push_back(filename);
filenames_.insert(filename);
handles_.push_back(handle);
return true;
}
Expand Down
10 changes: 6 additions & 4 deletions g2o/apps/g2o_cli/dl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#ifndef G2O_DL_WRAPPER_H
#define G2O_DL_WRAPPER_H

#include <string>
#include <vector>

#ifdef WINDOWS
#include <windows.h>
#endif
// clang-format on

#include <string>
#include <unordered_set>
#include <vector>

#include "g2o_cli_api.h"

Expand Down Expand Up @@ -75,7 +77,7 @@ class G2O_CLI_API DlWrapper {
#elif defined(WINDOWS)
std::vector<HMODULE> handles_;
#endif
std::vector<std::string> filenames_;
std::unordered_set<std::string> filenames_;
};

} // namespace g2o
Expand Down
6 changes: 4 additions & 2 deletions g2o/apps/g2o_cli/g2o_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ HMODULE getMyInstance() {
}
#endif

static constexpr std::string_view kTypesPattern = "*_types_*." SO_EXT;
static constexpr std::string_view kSolversPattern = "*_solver_*." SO_EXT;
static constexpr std::string_view kTypesPattern =
"^.*g2o_types_.*\\." SO_EXT "$";
static constexpr std::string_view kSolversPattern =
"^.*g2o_solver_.*\\." SO_EXT "$";

namespace g2o {

Expand Down
135 changes: 49 additions & 86 deletions g2o/stuff/filesys_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,112 +24,75 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/***************************************************************************
* filesysTools.cpp
*
* Fr 02 Mär 2007 23:14:08 CET
* Copyright 2007 Rainer Kümmerle
* Email [email protected]
****************************************************************************/
#include "filesys_tools.h"

// clang-format off
#ifdef WINDOWS
#include <windows.h>
#endif
// clang-format on

#include <sys/stat.h>

#if (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
#include <wordexp.h>
#endif
#include <filesystem>
#include <regex>
#include <string_view>

namespace g2o {

std::string getFileExtension(const std::string& filename) {
const std::string::size_type lastDot = filename.find_last_of('.');
if (lastDot != std::string::npos) return filename.substr(lastDot + 1);
return "";
std::string getFileExtension(std::string_view filename) {
const std::filesystem::path path(filename);
std::string result = path.extension().string();
if (!result.empty() && result.front() == '.') return result.substr(1);
return result;
}

std::string getPureFilename(const std::string& filename) {
const std::string::size_type lastDot = filename.find_last_of('.');
if (lastDot != std::string::npos) return filename.substr(0, lastDot);
return filename;
std::string getPureFilename(std::string_view filename) {
const std::filesystem::path path(filename);
if (path.has_parent_path()) {
return (path.parent_path() / path.stem()).string();
}
return path.stem().string();
}

std::string getBasename(const std::string& filename) {
#ifdef WINDOWS
const std::string::size_type lastSlash = filename.find_last_of('\\');
#else
const std::string::size_type lastSlash = filename.find_last_of('/');
#endif
if (lastSlash != std::string::npos) return filename.substr(lastSlash + 1);
return filename;
std::string getBasename(std::string_view filename) {
const std::filesystem::path path(filename);
return path.filename().string();
}

std::string getDirname(const std::string& filename) {
#ifdef WINDOWS
const std::string::size_type lastSlash = filename.find_last_of('\\');
#else
const std::string::size_type lastSlash = filename.find_last_of('/');
#endif
if (lastSlash != std::string::npos) return filename.substr(0, lastSlash);
return "";
std::string getDirname(std::string_view filename) {
const std::filesystem::path path(filename);
return path.parent_path().string();
}

std::string changeFileExtension(const std::string& filename,
const std::string& newExt, bool stripDot) {
std::string::size_type lastDot = filename.find_last_of('.');
if (lastDot != std::string::npos) {
if (!stripDot) ++lastDot;
return filename.substr(0, lastDot) + newExt;
std::string changeFileExtension(std::string_view filename,
std::string_view newExt) {
std::filesystem::path path(filename);
if (!path.has_extension()) {
return path.string();
}
return filename;
path.replace_extension(std::filesystem::path(newExt));
return path.string();
}

bool fileExists(const char* filename) {
struct stat statInfo;
return (stat(filename, &statInfo) == 0);
bool fileExists(std::string_view filename) {
const std::filesystem::path path(filename);
return std::filesystem::exists(path);
}

std::vector<std::string> getFilesByPattern(const char* pattern) {
std::vector<std::string> result;

#ifdef WINDOWS

HANDLE hFind;
WIN32_FIND_DATA FData;
if ((hFind = FindFirstFile(pattern, &FData)) != INVALID_HANDLE_VALUE) {
do {
result.push_back(FData.cFileName);
} while (FindNextFile(hFind, &FData));
FindClose(hFind);
}

#elif (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
std::vector<std::string> getFilesByPattern(std::string_view directory,
const std::regex& pattern) {
auto match = [&pattern](const std::filesystem::path& entry) {
return std::regex_search(entry.filename().string(), pattern);
};

wordexp_t p;
wordexp(pattern, &p, 0);

// For some reason, wordexp sometimes fails on an APPLE machine to
// return anything; therefore, run it several times until we do find
// something - or give up
#ifdef __APPLE__
for (int k = 0; (k < 100) && (p.we_wordc == 0); k++) {
// chrono::milliseconds duration(20);
// this_thread::sleep_for(duration);
wordexp(pattern, &p, WRDE_APPEND);
std::vector<std::string> result;
for (const auto& dir_entry :
std::filesystem::directory_iterator(std::filesystem::path(directory))) {
if (dir_entry.is_regular_file() && match(dir_entry)) {
result.emplace_back(dir_entry.path().string());
continue;
}
if (dir_entry.is_symlink() &&
std::filesystem::is_regular_file(
std::filesystem::read_symlink(dir_entry)) &&
match(dir_entry)) {
result.emplace_back(dir_entry.path().string());
continue;
}
}
#endif

result.reserve(p.we_wordc);
for (size_t i = 0; i < p.we_wordc; ++i) result.emplace_back(p.we_wordv[i]);

wordfree(&p);

#endif

return result;
}
Expand Down
30 changes: 12 additions & 18 deletions g2o/stuff/filesys_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/***************************************************************************
* filesysTools.h
*
* Fr 02 Mär 2007 23:14:21 CET
* Copyright 2007 Rainer Kümmerle
* Email [email protected]
****************************************************************************/

#ifndef G2O_FILESYS_TOOLS_H
#define G2O_FILESYS_TOOLS_H

Expand All @@ -44,7 +36,9 @@
* \brief utility functions for handling files, directory on Linux/Unix
*/

#include <regex>
#include <string>
#include <string_view>
#include <vector>

namespace g2o {
Expand All @@ -53,43 +47,43 @@ namespace g2o {
* get filename extension (the part after the last .), e.g.
* the extension of file.txt is txt
*/
G2O_STUFF_API std::string getFileExtension(const std::string& filename);
G2O_STUFF_API std::string getFileExtension(std::string_view filename);

/**
* get the filename without the extension.
* file.txt -> file
*/
G2O_STUFF_API std::string getPureFilename(const std::string& filename);
G2O_STUFF_API std::string getPureFilename(std::string_view filename);

/**
* change the fileextension of a given filename.
* Only if filename contains an extension, otherwise filename is returned.
*/
G2O_STUFF_API std::string changeFileExtension(const std::string& filename,
const std::string& newExt,
bool stripDot = false);
G2O_STUFF_API std::string changeFileExtension(std::string_view filename,
std::string_view newExt);

/**
* return the basename of the given filename
* /etc/fstab -> fstab
*/
G2O_STUFF_API std::string getBasename(const std::string& filename);
G2O_STUFF_API std::string getBasename(std::string_view filename);

/**
* return the directory of a given filename
* /etc/fstab -> /etc
*/
G2O_STUFF_API std::string getDirname(const std::string& filename);
G2O_STUFF_API std::string getDirname(std::string_view filename);

/**
* check if file exists (note a directory is also a file)
*/
G2O_STUFF_API bool fileExists(const char* filename);
G2O_STUFF_API bool fileExists(std::string_view filename);

/**
* return all files that match a given pattern, e.g., ~/blaa*.txt, /tmp/a*
* return all files that match a given regexp in the directory.
*/
G2O_STUFF_API std::vector<std::string> getFilesByPattern(const char* pattern);
G2O_STUFF_API std::vector<std::string> getFilesByPattern(
std::string_view directory, const std::regex& pattern);

} // namespace g2o
// @}
Expand Down
Loading

0 comments on commit 5db17af

Please sign in to comment.