μHAL (v2.6.5)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
files.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
31  email: tom.williams <AT> cern.ch
32 
33 ---------------------------------------------------------------------------
34 */
35 
36 #include "uhal/utilities/files.hpp"
37 
38 #include "uhal/log/log.hpp"
39 
40 
41 namespace uhal
42 {
43  namespace utilities
44  {
45 
46  void ParseSemicolonDelimitedUriList ( const std::string& aSemicolonDelimitedUriList , std::vector< std::pair<std::string, std::string> >& aUriList )
47  {
48  try
49  {
51  boost::spirit::qi::phrase_parse ( aSemicolonDelimitedUriList.begin() , aSemicolonDelimitedUriList.end() , lGrammar , boost::spirit::ascii::space , aUriList );
52  }
53  catch ( const std::exception& aExc )
54  {
55  exception::UriListParsingError lExc;
56  log ( lExc , "Expression " , Quote ( aSemicolonDelimitedUriList ) , " must be a semicolon delimeted list and all files must be in the form " , Quote ( "protocol://address" ) );
57  throw lExc;
58  }
59 
60  log ( Debug() , "Parsed " , Quote ( aSemicolonDelimitedUriList ) , " to:" );
61 
62  for ( std::vector< std::pair<std::string, std::string> >::iterator lIt = aUriList.begin() ; lIt != aUriList.end() ; ++lIt )
63  {
64  log ( Debug() , " > [" , lIt->first , "] " , Quote ( lIt->second ) );
65  }
66  }
67 
68 
69  void ShellExpandFilenameExpr ( const std::string& aFilenameExpr , const boost::filesystem::path& aParentPath , std::vector< boost::filesystem::path >& aFiles )
70  {
71  try
72  {
73  // boost::lock_guard<boost::mutex> lLock ( gUtilityMutex );
74  //struct which will store the shell expansions of the expression
75  wordexp_t lShellExpansion;
76  wordexp ( aFilenameExpr.c_str() , &lShellExpansion , 0 );
77 
78  for ( std::size_t i = 0 ; i != lShellExpansion.we_wordc ; i++ )
79  {
80  boost::filesystem::path lPath ( lShellExpansion.we_wordv[i] );
81  log ( Debug() , "lPath was " , Quote ( lPath.c_str() ) );
82  log ( Debug() , "aParentPath is " , Quote ( aParentPath.c_str() ) );
83  lPath = boost::filesystem::absolute ( lPath , aParentPath );
84  log ( Debug() , "lPath now " , Quote ( lPath.c_str() ) );
85 
86  if ( boost::filesystem::exists ( lPath ) )
87  {
88  if ( boost::filesystem::is_regular_file ( lPath ) )
89  {
90  aFiles.push_back ( lPath );
91  }
92  }
93  }
94 
95  wordfree ( &lShellExpansion );
96  }
97  catch ( const std::exception& aExc )
98  {
99  uhal::exception::ExpandingShellExpressionFailed lExc;
100  log ( lExc , "Caught exception: " , Quote ( aExc.what() ) );
101  throw lExc;
102  }
103 
104  if ( ! aFiles.size() )
105  {
106  uhal::exception::FileNotFound lExc;
107  log ( lExc , "No matching files for expression " , Quote ( aFilenameExpr ) , " with parent path " , Quote ( aParentPath.c_str() ) );
108  throw lExc;
109  }
110  else
111  {
112  log ( Debug() , "Shell expansion of " , Quote ( aFilenameExpr.c_str() ) , " returned:" );
113 
114  for ( std::vector< boost::filesystem::path >::iterator lIt = aFiles.begin() ; lIt != aFiles.end() ; ++lIt )
115  {
116  log ( Debug() , " > [file] " , lIt->c_str() );
117  }
118  }
119  }
120  }
121 }
122 
void ParseSemicolonDelimitedUriList(const std::string &aSemicolonDelimitedUriList, std::vector< std::pair< std::string, std::string > > &aUriList)
Parse a semicolon delimited list of URIs into a vector of protocol/address pairs. ...
Definition: files.cpp:46
void ShellExpandFilenameExpr(const std::string &aFilenameExpr, const boost::filesystem::path &aParentPath, std::vector< boost::filesystem::path > &aFiles)
Perform shell expansion of a linux shell expression ( e.g.
Definition: files.cpp:69
_Quote< T > Quote(const T &aT)
The BOOST::SPIRIT grammar for parsing the Semicolon delimited URI list into a vector of protocol-URI ...
DebugLevel Debug
Definition: LogLevels.cpp:133