diff --git a/pub/lib/file_in.cpp b/pub/lib/file_in.cpp index 76fa5a8b612152af6134b40fac887dd352de20c1..194ff34b1c0b6136426d92db438fe133e24d5d6e 100644 --- a/pub/lib/file_in.cpp +++ b/pub/lib/file_in.cpp @@ -42,8 +42,7 @@ namespace NFileIn { void NFileIn::load(void) { string fnames = sask( "Load file(s)" ); - vector<string> vflong; // unix names - triv::glob_file_list( fnames, "y08", &vflong ); + vector<string> vflong = triv::glob_file_list( fnames, "y08" ); for( int i=0; i<vflong.size(); ++i ) { FILE *F_in = fopen(vflong[i].c_str(), "r"); if( !F_in ) diff --git a/pub/lib/import.cpp b/pub/lib/import.cpp index 696aa90853800ab4c651ae40581c0722daadf1a8..dfab88c88e4442ca684b53a5f8e8dbd21cd873ed 100644 --- a/pub/lib/import.cpp +++ b/pub/lib/import.cpp @@ -325,7 +325,7 @@ void NImport::read_tab( string qualif ) triv::system( script ); } else { string fnames = sask( "Read tab from file(s)" ); - triv::glob_file_list( fnames, "", &inFiles ); + inFiles = triv::glob_file_list( fnames, "" ); } if( choosecol ){ diff --git a/pub/trivia/file_ops.cpp b/pub/trivia/file_ops.cpp index bffb31e175383ec889fd762711845216e47ef22b..80727989e1de3691b42300a9509e82cd6b860c40 100644 --- a/pub/trivia/file_ops.cpp +++ b/pub/trivia/file_ops.cpp @@ -1,9 +1,9 @@ -//***************************************************************************// -//* libtrivia: various little routines for C++ *// -//* file_ops: file operations *// -//* (C) Joachim Wuttke 2001, 2012- *// -//* http://apps.jcns.fz-juelich.de *// -//***************************************************************************// +//************************************************************************************************** +//* libtrivia: various little routines for C++ +//* file_ops: file operations +//* (C) Joachim Wuttke 2001, 2012- +//* http://apps.jcns.fz-juelich.de +//************************************************************************************************** #include <sys/stat.h> #include <wordexp.h> @@ -16,16 +16,19 @@ #include <boost/format.hpp> using boost::format; -using namespace std; +using std::string; +using std::vector; +using std::cout; +using std::cerr; #include "string_convs.hpp" #include "string_ops.hpp" #include "file_ops.hpp" -//**************************************************************************// -//* improved libc calls *// -//**************************************************************************// +//************************************************************************************************** +//* improved libc calls +//************************************************************************************************** //! Execute operation system call, and assert return code 0. @@ -58,10 +61,9 @@ string triv::system_read( string cmd, bool debug ) } -//**************************************************************************// -//* file names, globbing *// -//**************************************************************************// - +//************************************************************************************************** +//* file names, globbing +//************************************************************************************************** //! Test whether file exists. @@ -76,12 +78,10 @@ bool triv::file_exists( const string& fname ) } -//! Analyse file name: divide "dir/short.ext" into "dir", "short", "ext". +//! Analyses a file name: divides "dir/short.ext" into "dir", "short", "ext". -void triv::fname_divide( const string& fname, - string *fdir, string *fshort, string *fext ) -// if some of the three return values are not needed, the user may supply 0 -// instead of a pointer to a string. +void triv::fname_divide( const string& fname, string *fdir, string *fshort, string *fext ) +// each of the three output arguments, when called with a nullptr, will not be computed { size_t idir = fname.rfind("/") + 1; if( fdir ) *fdir = fname.substr( 0, idir ); @@ -122,19 +122,19 @@ string triv::next_tmp_file( const string& path_format ) if ( !triv::file_exists( fname ) ) return fname; if( i==tmpmax/2 ) - cerr << "WARNING: there more than " << i << - " temporay files of form " << path_format << + cerr << "WARNING: there more than " << i << " temporay files of form " << path_format << "; please clean up soon\n"; } throw "Too many temporary files in use"; } -//! Expansion of file list. +//! Returns a list of file names obtained by shell-like expansion of pattern with optional extension -void triv::glob_file_list( - const string& pattern, const string& extension, vector<string> *fnames ) +vector<string> triv::glob_file_list( const string& pattern, const string& extension) { + vector<string> ret; + // add extension to input pattern: string extended_pattern = ""; vector<string> words; @@ -143,7 +143,7 @@ void triv::glob_file_list( throw string( "empty file list" ); for ( size_t i=0; ; ) { string fmain, fext; - fname_divide( words[i], 0, &fmain, &fext ); + fname_divide( words[i], nullptr, &fmain, &fext ); extended_pattern += words[i]; if ( fext=="" && extension!="" ) extended_pattern += "." + extension; @@ -158,8 +158,9 @@ void triv::glob_file_list( if( p.we_wordc==0 ) throw "expansion of " + extended_pattern + " is empty"; for ( size_t i=0; i<p.we_wordc; ++i ) - fnames->push_back( p.we_wordv[i] ); + ret.push_back( p.we_wordv[i] ); wordfree( &p ); + return ret; } diff --git a/pub/trivia/file_ops.hpp b/pub/trivia/file_ops.hpp index dba3b470edc11a4f9a06d008559164c482906c74..627acf53996eb6fcad42db496a600455007f1235 100644 --- a/pub/trivia/file_ops.hpp +++ b/pub/trivia/file_ops.hpp @@ -15,8 +15,8 @@ namespace triv { bool file_exists( const std::string& fname ); void fname_divide( const std::string& fname, std::string *fdir, std::string *fshort, std::string *fext); - void glob_file_list( const std::string& pattern, const std::string& extension, - std::vector<std::string> *fnames ); + std::vector<std::string> glob_file_list( + const std::string& pattern, const std::string& extension); std::string wordexp_unique( const std::string& s ); std::string next_tmp_file( const std::string& path_format );