Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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
35namespace ROOT {
36namespace FoundationUtils {
37std::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) {
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 && 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
72std::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.
97void ConvertToUnixPath(std::string& Path) {
98 std::replace(Path.begin(), Path.end(), '\\', '/');
99}
100
101const 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
126static bool IgnorePrefix() {
127 static bool ignorePrefix = ::getenv("ROOTIGNOREPREFIX");
128 return ignorePrefix;
129}
130#endif
131
132const 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
154const 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
169const 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 =
179 return rootetcdir;
180}
181
182static 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
188bool 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
198bool ConvertEnvValueToBool(const std::string& 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
The file contains utilities which are foundational and could be used across the core component of ROO...
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
static int lowercase(const char *s)
Definition civetweb.c:2975
const std::string & GetPathSeparator()
const std::string & GetIncludeDir()
\ returns the include directory in the installation.
static std::string str_tolower(std::string s)
void ConvertToUnixPath(std::string &Path)
Transforms a file path by replacing its backslashes with slashes.
bool CanConvertEnvValueToBool(const std::string &value)
const std::string & GetRootSys()
std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT=false)
bool ConvertEnvValueToBool(const std::string &value)
const std::string & GetFallbackRootSys()
const std::string & GetEtcDir()
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static void output()