μHAL (v2.7.9)
Part of the IPbus software repository
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  //struct which will store the shell expansions of the expression
74  wordexp_t lShellExpansion;
75  wordexp ( aFilenameExpr.c_str() , &lShellExpansion , 0 );
76 
77  for ( std::size_t i = 0 ; i != lShellExpansion.we_wordc ; i++ )
78  {
79  boost::filesystem::path lPath ( lShellExpansion.we_wordv[i] );
80  log ( Debug() , "lPath was " , Quote ( lPath.c_str() ) );
81  log ( Debug() , "aParentPath is " , Quote ( aParentPath.c_str() ) );
82  lPath = boost::filesystem::absolute ( lPath , aParentPath );
83  log ( Debug() , "lPath now " , Quote ( lPath.c_str() ) );
84 
85  if ( boost::filesystem::exists ( lPath ) )
86  {
87  if ( boost::filesystem::is_regular_file ( lPath ) )
88  {
89  aFiles.push_back ( lPath );
90  }
91  }
92  }
93 
94  wordfree ( &lShellExpansion );
95  }
96  catch ( const std::exception& aExc )
97  {
98  uhal::exception::ExpandingShellExpressionFailed lExc;
99  log ( lExc , "Caught exception during shell expansion: " , Quote ( aExc.what() ) );
100  throw lExc;
101  }
102 
103  if ( ! aFiles.size() )
104  {
105  uhal::exception::FileNotFound lExc;
106  log ( lExc , "No matching files for expression " , Quote ( aFilenameExpr ) , " with parent path " , Quote ( aParentPath.c_str() ) );
107  throw lExc;
108  }
109  else
110  {
111  log ( Debug() , "Shell expansion of " , Quote ( aFilenameExpr.c_str() ) , " returned:" );
112 
113  for ( std::vector< boost::filesystem::path >::iterator lIt = aFiles.begin() ; lIt != aFiles.end() ; ++lIt )
114  {
115  log ( Debug() , " > [file] " , lIt->c_str() );
116  }
117  }
118  }
119  }
120 }
121 
uhal::utilities::ShellExpandFilenameExpr
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
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::utilities::ParseSemicolonDelimitedUriList
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
files.hpp
uhal::log
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:20
uhal::grammars::SemicolonDelimitedUriListGrammar
The BOOST::SPIRIT grammar for parsing the Semicolon delimited URI list into a vector of protocol-URI ...
Definition: SemicolonDelimitedUriListGrammar.hpp:52
uhal::Quote
_Quote< T > Quote(const T &aT)
Definition: log_inserters.quote.hxx:49
uhal::Debug
DebugLevel Debug
Definition: LogLevels.cpp:133
log.hpp