ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
parserUtilities.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "parserUtilities.h"
5
6#include <unistd.h>
7
8#include <cstdlib>
9#include <filesystem>
10#include <stdexcept>
11
13
14 /*
15 * Adds a '/' to the end of the input string path, only if path does not end in
16 * a '/' character
17 */
18 static std::string appendForwardSlash(const std::string& path);
19
21 return appendForwardSlash(std::filesystem::current_path());
22 }
23
24 std::string convertToAbsolutePath(const std::string& relativePath) {
25 return concatenatePaths(getCurrentWorkingDirectory(), relativePath);
26 }
27
28 std::string concatenatePaths(const std::string& path1, const std::string& path2) {
29 std::string returnValue = path2;
30 if(path2[0] != '/') { // path2 not absolute path
31 returnValue = appendForwardSlash(path1) + path2;
32 }
33 return returnValue;
34 }
35
36 std::string extractDirectory(const std::string& path) {
37 size_t pos = path.find_last_of('/');
38 bool isPathJustFileName = (pos == std::string::npos);
39
40 if(isPathJustFileName) { // No forward slashes in path; just the file name,
41 // meaning working directory has the file in it.
42 return "./";
43 }
44 return path.substr(0, pos + 1); // substring till the last '/'. The '/'
45 // character is included in the return
46 // string
47 }
48
49 std::string extractFileName(const std::string& path) {
50 std::string extractedName = path;
51
52 size_t pos = path.find_last_of('/');
53 bool isPathJustFileName = (pos == std::string::npos); // no '/' in the string
54
55 if(!isPathJustFileName) {
56 extractedName = path.substr(pos + 1, std::string::npos); // get the substring after the
57 // last '/' in the path
58 }
59 return extractedName;
60 }
61
62 std::string appendForwardSlash(const std::string& path) {
63 if(path.empty()) {
64 return "/";
65 }
66 if(path.back() == '/') { // path ends with '/'
67 return path;
68 }
69 // add '/' to path
70 return path + "/";
71 }
72
73} // namespace ChimeraTK::parserUtilities
std::string extractFileName(std::string const &path)
Extract the string after the last '/' in a path. Returned substring does not include the '/' characte...
std::string getCurrentWorkingDirectory()
Returns absolute path to current working directory. The returned path ends with a forward slash.
std::string extractDirectory(std::string const &path)
Returns the path to the directory containing the file provided as the input parameter.
std::string concatenatePaths(const std::string &path1, const std::string &path2)
Concatenates two given paths using custom rules.
std::string convertToAbsolutePath(std::string const &relativePath)
Converts a relative path to its absolute path.