Logo ROOT  
Reference Guide
FoundationUtils.cxx
Go to the documentation of this file.
1 /// \file FoundationUtils.cxx
2 ///
3 /// \brief The file contains utilities which are foundational and could be used
4 /// across the core component of ROOT.
5 ///
6 ///
7 /// \author Vassil Vassilev <vvasilev@cern.ch>
8 ///
9 /// \date June, 2019
10 ///
11 /*************************************************************************
12  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
13  * All rights reserved. *
14  * *
15  * For the licensing terms see $ROOTSYS/LICENSE. *
16  * For the list of contributors see $ROOTSYS/README/CREDITS. *
17  *************************************************************************/
18 
19 #include <ROOT/FoundationUtils.hxx>
20 
21 #include <RConfigure.h>
22 
23 #include <algorithm>
24 #include <cassert>
25 
26 #include <errno.h>
27 #include <string.h>
28 #ifdef _WIN32
29 #include <direct.h>
30 #include <Windows4Root.h>
31 #else
32 #include <unistd.h>
33 #endif // _WIN32
34 
35 namespace ROOT {
36 namespace FoundationUtils {
37 std::string GetCurrentDir()
38 {
39  char fixedLength[1024];
40  char *currWorkDir = fixedLength;
41  size_t len = 1024;
42  char *result = currWorkDir;
43 
44  do {
45  if (result == 0) {
46  len = 2 * len;
47  if (fixedLength != currWorkDir) {
48  delete[] currWorkDir;
49  }
50  currWorkDir = new char[len];
51  }
52 #ifdef WIN32
53  result = ::_getcwd(currWorkDir, len);
54 #else
55  result = getcwd(currWorkDir, len);
56 #endif
57  } while (result == 0 && errno == ERANGE);
58 
59  std::string output = currWorkDir;
60  output += '/';
61 #ifdef WIN32
62  // convert backslashes into forward slashes
63  std::replace(output.begin(), output.end(), '\\', '/');
64 #endif
65 
66  if (fixedLength != currWorkDir) {
67  delete[] currWorkDir;
68  }
69  return output;
70 }
71 
72 std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT /* = false*/)
73 {
74  std::string result(path);
75 
76  const char *currWorkDir = base.c_str();
77  size_t lenCurrWorkDir = strlen(currWorkDir);
78  if (result.substr(0, lenCurrWorkDir) == currWorkDir) {
79  // Convert to path relative to $PWD.
80  // If that's not what the caller wants, she should pass -I to rootcling and a
81  // different relative path to the header files.
82  result.erase(0, lenCurrWorkDir);
83  }
84  // FIXME: This is not a generic approach for an interface. We should rework
85  // this part.
86  if (isBuildingROOT) {
87  // For ROOT, convert module directories like core/base/inc/ to include/
88  int posInc = result.find("/inc/");
89  if (posInc != -1) {
90  result = /*std::string("include") +*/ result.substr(posInc + 5, -1);
91  }
92  }
93  return result;
94 }
95 
96 /// Transforms a file path by replacing its backslashes with slashes.
97 void ConvertToUnixPath(std::string& Path) {
98  std::replace(Path.begin(), Path.end(), '\\', '/');
99 }
100 
101 const std::string& GetFallbackRootSys() {
102  static std::string fallback;
103  if (!fallback.empty())
104  return fallback;
105 #ifdef WIN32
106  static char lpFilename[_MAX_PATH];
107  if (::GetModuleFileNameA(
108  NULL, // handle to module to find filename for
109  lpFilename, // pointer to buffer to receive module path
110  sizeof(lpFilename))) { // size of buffer, in characters
111  auto parent_path = [](std::string path) {
112  return path.substr(0, path.find_last_of("/\\"));
113  };
114  fallback = parent_path(parent_path(lpFilename));
115  }
116 #else
117  // FIXME: We should not hardcode this path. We can use a similar to the
118  // windows technique to get the path to the executable. The easiest way
119  // to do this is to depend on LLVMSupport and use getMainExecutable.
120  fallback = "/usr/local/root";
121 #endif
122  return fallback;
123 }
124 
125 #ifdef ROOTPREFIX
126 static bool IgnorePrefix() {
127  static bool ignorePrefix = ::getenv("ROOTIGNOREPREFIX");
128  return ignorePrefix;
129 }
130 #endif
131 
132 const std::string& GetRootSys() {
133 #ifdef ROOTPREFIX
134  if (!IgnorePrefix()) {
135  const static std::string rootsys = ROOTPREFIX;
136  return rootsys;
137  }
138 #endif
139  static std::string rootsys;
140  if (rootsys.empty()) {
141  if (const char* envValue = ::getenv("ROOTSYS")) {
142  rootsys = envValue;
143  // We cannot use gSystem->UnixPathName.
144  ConvertToUnixPath(rootsys);
145  }
146  }
147  // FIXME: Should this also call UnixPathName for consistency?
148  if (rootsys.empty())
149  rootsys = GetFallbackRootSys();
150  return rootsys;
151 }
152 
153 
154 const std::string& GetIncludeDir() {
155 #ifdef ROOTINCDIR
156  if (!IgnorePrefix()) {
157  const static std::string rootincdir = ROOTINCDIR;
158  return rootincdir;
159  }
160 #endif
161  static std::string rootincdir;
162  if (rootincdir.empty()) {
163  const std::string& sep = GetPathSeparator();
164  rootincdir = GetRootSys() + sep + "include" + sep;
165  }
166  return rootincdir;
167 }
168 
169 const std::string& GetEtcDir() {
170 #ifdef ROOTETCDIR
171  if (!IgnorePrefix()) {
172  const static std::string rootetcdir = ROOTETCDIR;
173  return rootetcdir;
174  }
175 #endif
176 
177  const static std::string rootetcdir =
178  GetRootSys() + GetPathSeparator() + "etc" + GetPathSeparator();;
179  return rootetcdir;
180 }
181 
182 static std::string str_tolower(std::string s) {
183  std::transform(s.begin(), s.end(), s.begin(),
184  [](unsigned char c){ return std::tolower(c); });
185  return s;
186 }
187 
188 bool CanConvertEnvValueToBool(const std::string& value) {
189  std::string lowercase = str_tolower(value);
190  if (lowercase == "1" || lowercase == "on" || lowercase == "true")
191  return true;
192  if (lowercase == "0" || lowercase == "off" || lowercase == "false")
193  return true;
194 
195  return false;
196 }
197 
198 bool ConvertEnvValueToBool(const std::string& value) {
199  assert(CanConvertEnvValueToBool(value));
200  std::string lowercase = str_tolower(value);
201  if (lowercase == "1" || lowercase == "on" || lowercase == "true")
202  return true;
203  if (lowercase == "0" || lowercase == "off" || lowercase == "false")
204  return false;
205  // FIXME: Implement a wrapper around __builtin_unreachable() and use it here
206  return false;
207 }
208 
209 } // namespace FoundationUtils
210 } // namespace ROOT
c
#define c(i)
Definition: RSha256.hxx:101
Windows4Root.h
ROOT::FoundationUtils::GetCurrentDir
std::string GetCurrentDir()
Definition: FoundationUtils.cxx:37
output
static void output(int code)
Definition: gifencode.c:226
ROOT::FoundationUtils::GetRootSys
const std::string & GetRootSys()
Definition: FoundationUtils.cxx:132
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
ROOT::FoundationUtils::GetPathSeparator
const std::string & GetPathSeparator()
Definition: FoundationUtils.hxx:43
ROOT::FoundationUtils::MakePathRelative
std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT=false)
Definition: FoundationUtils.cxx:72
ROOT::FoundationUtils::CanConvertEnvValueToBool
bool CanConvertEnvValueToBool(const std::string &value)
Definition: FoundationUtils.cxx:188
ROOT::FoundationUtils::ConvertToUnixPath
void ConvertToUnixPath(std::string &Path)
Transforms a file path by replacing its backslashes with slashes.
Definition: FoundationUtils.cxx:97
lowercase
static int lowercase(const char *s)
Definition: civetweb.c:3260
ROOT::FoundationUtils::GetEtcDir
const std::string & GetEtcDir()
Definition: FoundationUtils.cxx:169
ROOT::FoundationUtils::GetFallbackRootSys
const std::string & GetFallbackRootSys()
Definition: FoundationUtils.cxx:101
FoundationUtils.hxx
The file contains utilities which are foundational and could be used across the core component of ROO...
ROOT::FoundationUtils::ConvertEnvValueToBool
bool ConvertEnvValueToBool(const std::string &value)
Definition: FoundationUtils.cxx:198
ROOT::Math::detail::sep
@ sep
Definition: GenVectorIO.h:35
ROOT::FoundationUtils::str_tolower
static std::string str_tolower(std::string s)
Definition: FoundationUtils.cxx:182
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::FoundationUtils::GetIncludeDir
const std::string & GetIncludeDir()
\ returns the include directory in the installation.
Definition: FoundationUtils.cxx:154