Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
FileInclude.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 08-Jan-18
2
4//
5// Podd::FileInclude
6//
7// Helper functions for supporting '#include' directives in
8// configuration files
9//
11
12#include "FileInclude.h"
13#include "TString.h"
14#include "TObjArray.h"
15#include "TObjString.h"
16#include "TSystem.h"
17
18#include <vector>
19#include <cassert>
20#include <memory> // for unique_ptr
21
22using namespace std;
23
24namespace Podd {
25
26const string kIncTag = "#include";
27const string kWhiteSpace = " \t";
28
29//_____________________________________________________________________________
30Int_t GetIncludeFileName( const string& line, string& incfile )
31{
32 // Extract file name from #include statement
33
34 if( line.substr(0,kIncTag.length()) != kIncTag ||
35 line.length() <= kIncTag.length() )
36 return -1; // Not an #include statement (should never happen)
37 string::size_type pos = line.find_first_not_of(kWhiteSpace,kIncTag.length()+1);
38 if( pos == string::npos || (line[pos] != '<' && line[pos] != '\"') )
39 return -2; // No proper file name delimiter
40 const char end = (line[pos] == '<') ? '>' : '\"';
41 string::size_type pos2 = line.find(end,pos+1);
42 if( pos2 == string::npos || pos2 == pos+1 )
43 return -3; // Unbalanced delimiters or zero length filename
44 if( line.length() > pos2 &&
45 line.find_first_not_of(kWhiteSpace,pos2+1) != string::npos )
46 return -4; // Trailing garbage after filename spec
47
48 incfile = line.substr(pos+1,pos2-pos-1);
49 return 0;
50}
51
52//_____________________________________________________________________________
53Int_t CheckIncludeFilePath( string& incfile )
54{
55 // Check if 'incfile' can be opened in the current directory or
56 // any of the directories in the include path
57
58 vector<TString> paths;
59 paths.emplace_back(incfile.c_str() );
60
61 TString incp = gSystem->Getenv("ANALYZER_CONFIGPATH");
62 if( !incp.IsNull() ) {
63 unique_ptr<TObjArray> incdirs( incp.Tokenize(":") );
64 if( !incdirs->IsEmpty() ) {
65 Int_t ndirs = incdirs->GetLast()+1;
66 assert( ndirs>0 );
67 for( Int_t i = 0; i < ndirs; ++i ) {
68 TString path = (static_cast<TObjString*>(incdirs->At(i)))->String();
69 if( path.IsNull() )
70 continue;
71 if( !path.EndsWith("/") )
72 path.Append("/");
73 path.Append( incfile.c_str() );
74 paths.emplace_back(path.Data() );
75 }
76 }
77 }
78
79 for( auto& path : paths ) {
80 if( !gSystem->ExpandPathName(path) &&
82 incfile = path.Data();
83 return 0;
84 }
85 }
86 return -1;
87}
88
89//_____________________________________________________________________________
90
91} // namespace Podd
int Int_t
kReadPermission
R__EXTERN TSystem * gSystem
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
TObjArray * Tokenize(const TString &delim) const
TString & Append(char c, Ssiz_t rep=1)
Bool_t IsNull() const
virtual const char * Getenv(const char *env)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
virtual char * ExpandPathName(const char *path)
TLine * line
const string kIncTag
const string kWhiteSpace
Int_t CheckIncludeFilePath(string &incfile)
Int_t GetIncludeFileName(const string &line, string &incfile)
end
STL namespace.