| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

PathResolver.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/PathResolver.h"
2 #include "GaudiKernel/System.h"
3
4 #ifdef WIN32
5 // Disable warning
6 // C4996: 'std::copy': Function call with parameters that may be unsafe
7 // Probably coming from Boost classification.
8 #pragma warning(disable:4996)
9 #endif
10
11 #include <iostream>
12 #include <string>
13 #include <vector>
14 #include <stdlib.h>
15
16 #include <boost/algorithm/string/split.hpp>
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/filesystem.hpp>
19
20 namespace bf = boost::filesystem;
21 using namespace std;
22
23 #ifdef _WIN32
24 static const char* path_separator = ",;";
25 #else
26 static const char* path_separator = ",:";
27 #endif
28
29 //
31 //
32
33 namespace System {
34
35 typedef enum {
36 PR_regular_file,
37 PR_directory
38 } PR_file_type;
39
40 typedef enum {
41 PR_local,
42 PR_recursive
43 } PR_search_type;
44
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
46
47 static bool
48 PR_find( const bf::path& file, const string& search_list,
49 PR_file_type file_type, PathResolver::SearchType search_type,
50 string& result ) {
51
52 bool found(false);
53
54 // look for file as specified first
55
56 try {
57 if ( ( file_type == PR_regular_file && is_regular_file( file ) ) ||
58 ( file_type == PR_directory && is_directory( file ) ) ) {
59 result = bf::system_complete(file).string();
60 return true;
61 }
62 } catch (bf::filesystem_error /*err*/) {
63 }
64
65 // assume that "." is always part of the search path, so check locally first
66
67 try {
68 bf::path local = bf::initial_path() / file;
69 if ( ( file_type == PR_regular_file && is_regular_file( local ) ) ||
70 ( file_type == PR_directory && is_directory( local ) ) ) {
71 result = bf::system_complete(file).string();
72 return true;
73 }
74 } catch (bf::filesystem_error /*err*/) {
75 }
76
77
78 // iterate through search list
79 vector<string> spv;
80 split(spv, search_list, boost::is_any_of( path_separator), boost::token_compress_on);
81 for (const auto& itr : spv ) {
82
83 bf::path fp = itr / file;
84
85 try {
86 if ( ( file_type == PR_regular_file && is_regular_file( fp ) ) ||
87 ( file_type == PR_directory && is_directory( fp ) ) ) {
88 result = bf::system_complete(fp).string();
89 return true;
90 }
91 } catch (bf::filesystem_error /*err*/) {
92 }
93
94
95 // if recursive searching requested, drill down
96 if (search_type == PathResolver::RecursiveSearch &&
97 is_directory( bf::path(itr) ) ) {
98
99 bf::recursive_directory_iterator end_itr;
100 try {
101 for ( bf::recursive_directory_iterator ritr( itr );
102 ritr != end_itr; ++ritr) {
103
104 // skip if not a directory
105 if (! is_directory( bf::path(*ritr) ) ) { continue; }
106
107 bf::path fp2 = bf::path(*ritr) / file;
108 if ( ( file_type == PR_regular_file && is_regular_file( fp2 ) ) ||
109 ( file_type == PR_directory && is_directory( fp2 ) ) ) {
110 result = bf::system_complete( fp2 ).string();
111 return true;
112 }
113 }
114 } catch (bf::filesystem_error /*err*/) {
115 }
116 }
117
118 }
119
120 return found;
121 }
122
123 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
124
125 string
126 PathResolver::find_file(const std::string& logical_file_name,
127 const std::string& search_path,
128 SearchType search_type) {
129
130 std::string path_list;
131 System::getEnv(search_path, path_list);
132
133 return (find_file_from_list (logical_file_name, path_list, search_type));
134 }
135
136 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
137
138 std::string
139 PathResolver::find_file_from_list (const std::string& logical_file_name,
140 const std::string& search_list,
141 SearchType search_type)
142 {
143 std::string result("");
144
145 bf::path lfn( logical_file_name );
146
147 /* bool found = */
148 PR_find (lfn, search_list, PR_regular_file, search_type, result);
149
150 // The following functionality was in the original PathResolver, but I believe
151 // that it's WRONG. It extracts the filename of the requested item, and searches
152 // for that if the preceding search fails. i.e., if you're looking for "B/a.txt",
153 // and that fails, it will look for just "a.txt" in the search list.
154
155 // if (! found && lfn.filename() != lfn ) {
156 // result = "";
157 // PR_find (lfn.filename(), search_list, PR_regular_file, search_type, result);
158 // }
159
160 return (result);
161 }
162
163 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
164
165 string PathResolver::find_directory (const std::string& logical_file_name,
166 const std::string& search_path,
167 SearchType search_type)
168 {
169 std::string path_list;
170 System::getEnv(search_path, path_list);
171
172 return (find_directory_from_list (logical_file_name, path_list, search_type));
173 }
174
175 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
176
177 string
178 PathResolver::find_directory_from_list (const std::string& logical_file_name,
179 const std::string& search_list,
180 SearchType search_type)
181 {
182 std::string result;
183
184 if (!PR_find (logical_file_name, search_list, PR_directory, search_type, result))
185 {
186 result = "";
187 }
188
189 return (result);
190 }
191
192
193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
194
195 PathResolver::SearchPathStatus
196 PathResolver::check_search_path (const std::string& search_path)
197 {
198 std::string path_list;
199 if ( ! System::getEnv(search_path, path_list) )
200 return (EnvironmentVariableUndefined);
201
202 vector<string> spv;
203 boost::split( spv, path_list, boost::is_any_of( path_separator ), boost::token_compress_on);
204
205 try {
206 for (const auto& itr : spv ) {
207 bf::path pp(itr);
208 if (!is_directory(pp)) {
209 return (UnknownDirectory);
210 }
211 }
212 } catch(bf::filesystem_error /*err*/) {
213 return (UnknownDirectory);
214 }
215
216 return ( Ok );
217 }
218
219 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
220
221 std::string PathResolverFindXMLFile (const std::string& logical_file_name)
222 {
223 return PathResolver::find_file (logical_file_name, "XMLPATH");
224 }
225
226 std::string PathResolverFindDataFile (const std::string& logical_file_name)
227 {
228 return PathResolver::find_file (logical_file_name, "DATAPATH");
229 }
230
231 } // System namespace
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:550
System.h
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:772
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:19
System::PathResolverFindXMLFile
GAUDI_API std::string PathResolverFindXMLFile(const std::string &logical_file_name)
Definition: PathResolver.cpp:221
System::PR_directory
Definition: PathResolver.cpp:37
System::PathResolver::SearchPathStatus
SearchPathStatus
Definition: PathResolver.h:14
std
STL namespace.
System::PR_file_type
PR_file_type
Definition: PathResolver.cpp:35
System::PR_local
Definition: PathResolver.cpp:41
System::PathResolverFindDataFile
GAUDI_API std::string PathResolverFindDataFile(const std::string &logical_file_name)
Definition: PathResolver.cpp:226
std::string::string
T string(T...args)
System::PR_regular_file
Definition: PathResolver.cpp:36
std::vector
STL class.
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:417
PathResolver.h
System::PR_recursive
Definition: PathResolver.cpp:42
System::PR_search_type
PR_search_type
Definition: PathResolver.cpp:40
System::PathResolver::SearchType
SearchType
Definition: PathResolver.h:21