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#include <cstdlib>
26
27#include <cerrno>
28#include <cstring>
29
30#ifdef _WIN32
31#include <direct.h>
32#include <Windows4Root.h>
33#else
34#include <unistd.h>
35#endif // _WIN32
36
37#ifdef __FreeBSD__
38#include <sys/syslimits.h>
39#include <sys/param.h>
40#include <sys/user.h>
41#include <sys/types.h>
42#include <libutil.h>
43#include <libprocstat.h>
44#endif // __FreeBSD__
45
46namespace ROOT {
47namespace FoundationUtils {
48std::string GetCurrentDir()
49{
50 char fixedLength[1024];
52 size_t len = 1024;
53 char *result = currWorkDir;
54
55 do {
56 if (!result) {
57 len = 2 * len;
58 if (fixedLength != currWorkDir) {
59 delete[] currWorkDir;
60 }
61 currWorkDir = new char[len];
62 }
63#ifdef WIN32
65#else
67#endif
68 } while (!result && errno == ERANGE);
69
70 std::string output = currWorkDir;
71 output += '/';
72#ifdef WIN32
73 // convert backslashes into forward slashes
74 std::replace(output.begin(), output.end(), '\\', '/');
75#endif
76
77 if (fixedLength != currWorkDir) {
78 delete[] currWorkDir;
79 }
80 return output;
81}
82
83std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT /* = false*/)
84{
85 std::string result(path);
86
87 const char *currWorkDir = base.c_str();
89 if (result.substr(0, lenCurrWorkDir) == currWorkDir) {
90 // Convert to path relative to $PWD.
91 // If that's not what the caller wants, she should pass -I to rootcling and a
92 // different relative path to the header files.
93 result.erase(0, lenCurrWorkDir);
94 }
95 // FIXME: This is not a generic approach for an interface. We should rework
96 // this part.
97 if (isBuildingROOT) {
98 // For ROOT, convert module directories like core/base/inc/ to include/
99 int posInc = result.find("/inc/");
100 if (posInc != -1) {
101 result = /*std::string("include") +*/ result.substr(posInc + 5);
102 }
103 }
104 return result;
105}
106
107/// Transforms a file path by replacing its backslashes with slashes.
108void ConvertToUnixPath(std::string& Path) {
109 std::replace(Path.begin(), Path.end(), '\\', '/');
110}
111
112const std::string& GetFallbackRootSys() {
113 static std::string fallback;
114 if (!fallback.empty())
115 return fallback;
116
117#if defined(WIN32) || defined(__FreeBSD__)
118 auto parent_path = [](std::string path) {
119 return path.substr(0, path.find_last_of("/\\"));
120 };
121#endif
122
123#ifdef WIN32
124 static char lpFilename[_MAX_PATH];
126 NULL, // handle to module to find filename for
127 lpFilename, // pointer to buffer to receive module path
128 sizeof(lpFilename))) { // size of buffer, in characters
130 }
131#elif defined __FreeBSD__
134
135 char lpFilename[PATH_MAX] = "";
136 if (kp!=NULL) {
139 }
140
141 free(kp);
142 procstat_close(ps);
143#else
144 // FIXME: We should not hardcode this path. We can use a similar to the
145 // windows technique to get the path to the executable. The easiest way
146 // to do this is to depend on LLVMSupport and use getMainExecutable.
147 fallback = "/usr/local/root";
148#endif
149 return fallback;
150}
151
152#ifdef ROOTPREFIX
153static bool IgnorePrefix() {
154 static bool ignorePrefix = std::getenv("ROOTIGNOREPREFIX");
155 return ignorePrefix;
156}
157#endif
158
159const std::string& GetRootSys() {
160#ifdef ROOTPREFIX
161 if (!IgnorePrefix()) {
162 const static std::string rootsys = ROOTPREFIX;
163 return rootsys;
164 }
165#endif
166 static std::string rootsys;
167 if (rootsys.empty()) {
168 if (const char* envValue = std::getenv("ROOTSYS")) {
170 // We cannot use gSystem->UnixPathName.
172 }
173 }
174 // FIXME: Should this also call UnixPathName for consistency?
175 if (rootsys.empty())
177 return rootsys;
178}
179
180
181const std::string& GetIncludeDir() {
182#ifdef ROOTINCDIR
183 if (!IgnorePrefix()) {
184 const static std::string rootincdir = ROOTINCDIR;
185 return rootincdir;
186 }
187#endif
188 static std::string rootincdir;
189 if (rootincdir.empty()) {
190 const std::string& sep = GetPathSeparator();
191 rootincdir = GetRootSys() + sep + "include" + sep;
192 }
193 return rootincdir;
194}
195
196const std::string& GetEtcDir() {
197#ifdef ROOTETCDIR
198 if (!IgnorePrefix()) {
199 const static std::string rootetcdir = ROOTETCDIR;
200 return rootetcdir;
201 }
202#endif
203
204 const static std::string rootetcdir =
206 return rootetcdir;
207}
208
209static std::string str_tolower(std::string s) {
210 std::transform(s.begin(), s.end(), s.begin(),
211 [](unsigned char c){ return std::tolower(c); });
212 return s;
213}
214
215bool CanConvertEnvValueToBool(const std::string& value) {
216 std::string lowercase = str_tolower(value);
217 if (lowercase == "1" || lowercase == "on" || lowercase == "true")
218 return true;
219 if (lowercase == "0" || lowercase == "off" || lowercase == "false")
220 return true;
221
222 return false;
223}
224
225bool ConvertEnvValueToBool(const std::string& value) {
227 std::string lowercase = str_tolower(value);
228 if (lowercase == "1" || lowercase == "on" || lowercase == "true")
229 return true;
230 if (lowercase == "0" || lowercase == "off" || lowercase == "false")
231 return false;
232 // FIXME: Implement a wrapper around __builtin_unreachable() and use it here
233 return false;
234}
235
236} // namespace FoundationUtils
237} // 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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
#define free
Definition civetweb.c:1578
static int lowercase(const char *s)
Definition civetweb.c:3084
const_iterator begin() const
const_iterator end() const
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()
static void output()