Skip to content
Snippets Groups Projects
Commit 508c18bc authored by Wuttke, Joachim's avatar Wuttke, Joachim Committed by Wuttke, Joachim
Browse files

refactor -> separate function wordexp_multi

parent 4d302725
No related branches found
No related tags found
No related merge requests found
...@@ -97,7 +97,7 @@ void triv::fname_divide( const string& fname, string *fdir, string *fshort, stri ...@@ -97,7 +97,7 @@ void triv::fname_divide( const string& fname, string *fdir, string *fshort, stri
} }
//! Bash expansion of file name. //! Returns a unique file name obtained by Posix shell expansion of a given pattern.
string triv::wordexp_unique( const string& s ) string triv::wordexp_unique( const string& s )
{ {
...@@ -112,6 +112,23 @@ string triv::wordexp_unique( const string& s ) ...@@ -112,6 +112,23 @@ string triv::wordexp_unique( const string& s )
} }
//! Returns a list of file names obtained by Posix shell expansion of a given pattern.
vector<string> triv::wordexp_multi( const string& s )
{
wordexp_t p;
if( wordexp( s.c_str(), &p, 0 ) )
throw "cannot expand " + s;
if( p.we_wordc==0 )
throw "expansion of " + s + " is empty";
vector<string> ret;
for ( size_t i=0; i<p.we_wordc; ++i )
ret.push_back( p.we_wordv[i] );
wordfree( &p );
return ret;
}
//! Construct tmp file name that is not currently in use. //! Construct tmp file name that is not currently in use.
string triv::next_tmp_file( const string& path_format ) string triv::next_tmp_file( const string& path_format )
...@@ -133,9 +150,6 @@ string triv::next_tmp_file( const string& path_format ) ...@@ -133,9 +150,6 @@ string triv::next_tmp_file( const string& path_format )
vector<string> triv::glob_file_list( const string& pattern, const string& extension) vector<string> triv::glob_file_list( const string& pattern, const string& extension)
{ {
vector<string> ret;
// add extension to input pattern:
string extended_pattern = ""; string extended_pattern = "";
vector<string> words; vector<string> words;
split( pattern, words ); split( pattern, words );
...@@ -151,16 +165,7 @@ vector<string> triv::glob_file_list( const string& pattern, const string& extens ...@@ -151,16 +165,7 @@ vector<string> triv::glob_file_list( const string& pattern, const string& extens
break; break;
extended_pattern += " "; extended_pattern += " ";
} }
return triv::wordexp_multi(extended_pattern);
wordexp_t p;
if( wordexp( extended_pattern.c_str(), &p, 0 ) )
throw "cannot expand " + extended_pattern;
if( p.we_wordc==0 )
throw "expansion of " + extended_pattern + " is empty";
for ( size_t i=0; i<p.we_wordc; ++i )
ret.push_back( p.we_wordv[i] );
wordfree( &p );
return ret;
} }
......
...@@ -18,6 +18,7 @@ namespace triv { ...@@ -18,6 +18,7 @@ namespace triv {
std::vector<std::string> glob_file_list( std::vector<std::string> glob_file_list(
const std::string& pattern, const std::string& extension); const std::string& pattern, const std::string& extension);
std::string wordexp_unique( const std::string& s ); std::string wordexp_unique( const std::string& s );
std::vector<std::string> wordexp_multi( const std::string& s );
std::string next_tmp_file( const std::string& path_format ); std::string next_tmp_file( const std::string& path_format );
// File read: // File read:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment