MolSim
Loading...
Searching...
No Matches
PathUtils.h
Go to the documentation of this file.
1
9#pragma once
10#include <algorithm>
11#include <cstdlib>
12#include <limits.h>
13#include <string>
14#include <unistd.h>
15
17namespace PathUtils {
25static inline bool isXmlFile(const std::string &filename) {
26 size_t dotPos = filename.find_last_of('.');
27 if (dotPos == std::string::npos || dotPos == filename.length() - 1) {
28 return false;
29 }
30 std::string extension = filename.substr(dotPos);
31 std::transform(extension.begin(), extension.end(), extension.begin(),
32 [](unsigned char c) { return std::tolower(c); });
33 return extension == ".xml";
34}
35
46static inline std::string getTargetPath(const std::string &fullPath, const std::string &targetDir) {
47 size_t pos = fullPath.rfind(targetDir);
48 if (pos != std::string::npos) {
49 return fullPath.substr(0, pos + targetDir.length());
50 }
51 return "";
52}
53
63static inline bool setupFileTests(std::string &targetPath, std::string &skipReason) {
64 auto getCurrentPath = []() -> std::string {
65 char buffer[PATH_MAX];
66 if (getcwd(buffer, sizeof(buffer)) != nullptr) {
67 return std::string(buffer);
68 }
69 return "";
70 };
71 auto pathExists = [](const std::string &path) -> bool { return access(path.c_str(), F_OK) == 0; };
72 std::string cwd = getCurrentPath();
73 if (cwd.empty()) {
74 skipReason = "Unable to determine current working directory, skipping tests...";
75 return false;
76 }
77 targetPath = PathUtils::getTargetPath(cwd, "MolSim");
78 if (targetPath.empty()) {
79 skipReason = "Project root directory not found, skipping tests...";
80 return false;
81 }
82 targetPath += "/tests/files";
83 if (!pathExists(targetPath)) {
84 skipReason = "Test files not found, skipping tests...";
85 return false;
86 }
87 return true;
88}
89} // namespace PathUtils
Namespace defining utility functions for working with file paths.
Definition PathUtils.h:17
static bool setupFileTests(std::string &targetPath, std::string &skipReason)
Checks if the test file directory can be found.
Definition PathUtils.h:63
static bool isXmlFile(const std::string &filename)
Checks if a file is an XML file by verifying its extension in a case-insensitive way.
Definition PathUtils.h:25
static std::string getTargetPath(const std::string &fullPath, const std::string &targetDir)
Helper method to get the absolute path of a file or directory contained within a given path.
Definition PathUtils.h:46