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