wildcards.h 1.95 KB

#include <sstream>
#include <iomanip>

//#include <boost/regex.hpp>

namespace stim{

class wildcards{
	//generate a sequence of strings where '?' is replaced by integer increments
	static std::vector<std::string> increment(std::string input,
											  unsigned int start,
											  unsigned int end,
											  unsigned int step){

		size_t a = input.find_first_of('?');	//find the first ?
		size_t b = input.find_last_of('?');		//find the last ?
		size_t n = b - a + 1;					//calculate the number of ?

		std::string str_a = input.substr(0, a);		//split the strings along the wildcard
		std::string str_b = input.substr(b + 1, std::string::npos);

		//create a vector to hold the output strings
		std::vector<std::string> out;

		//create a stringstream to handle the padding
		for (unsigned int i = start; i <= end; i += step){

			std::stringstream ss;
			ss << str_a
				<< std::setfill('0')
				<< std::setw(n)
				<< i
				<< str_b;


		return out;

	//returns a list of files that match the specified wildcards in 'd'
	static std::vector<std::string> get_file_list(std::string s){

		stim::filename f(s);
		std::string target_path(f.dir());
		boost::regex filter(f.name());

		std::vector< std::string > all_matching_files;

		boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
		for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i )
			// Skip if not a file
			if( !boost::filesystem::is_regular_file( i->status() ) ) continue;

			boost::smatch what;

			// Skip if no match
			if( !boost::regex_match( i->path().filename().string(), what, filter ) ) continue;

			// File matches, store it
			all_matching_files.push_back( i->path().filename().string() );

		return all_matching_files;



}	//end namespace stim