/// Find the most appropriate configuration file for a given run /// @param run :: run number std::string EQSANSLoad::findConfigFile(const int &run) { // Append the standard location of EQSANS config file to the data search // directory list std::string sns_folder = "/SNS/EQSANS/shared/instrument_configuration"; if (Poco::File(sns_folder).exists()) Kernel::ConfigService::Instance().appendDataSearchDir(sns_folder); const std::vector<std::string> &searchPaths = Kernel::ConfigService::Instance().getDataSearchDirs(); int max_run_number = 0; std::string config_file = ""; static boost::regex re1("eqsans_configuration\\.([0-9]+)$"); boost::smatch matches; for (const auto &searchPath : searchPaths) { Poco::DirectoryIterator file_it(searchPath); Poco::DirectoryIterator end; for (; file_it != end; ++file_it) { if (boost::regex_search(file_it.name(), matches, re1)) { std::string s = matches[1]; int run_number = 0; Poco::NumberParser::tryParse(s, run_number); if (run_number > max_run_number && run_number <= run) { max_run_number = run_number; config_file = file_it.path().toString(); } } } } return config_file; }
/// Find the most appropriate configuration file for a given run /// @param run :: run number std::string EQSANSLoad::findConfigFile(const int& run) { // Append the standard location of EQSANS config file to the data search directory list std::string sns_folder = "/SNS/EQSANS/shared/instrument_configuration"; if (Poco::File(sns_folder).exists()) Kernel::ConfigService::Instance().appendDataSearchDir(sns_folder); const std::vector<std::string>& searchPaths = Kernel::ConfigService::Instance().getDataSearchDirs(); std::vector<std::string>::const_iterator it = searchPaths.begin(); int max_run_number = 0; std::string config_file = ""; Poco::RegularExpression re1("eqsans_configuration.[0-9]+"); Poco::RegularExpression re2("[0-9]+"); for (; it != searchPaths.end(); ++it) { Poco::DirectoryIterator file_it(*it); Poco::DirectoryIterator end; for (; file_it != end; ++file_it) { if (re1.match(file_it.name())) { std::string s; if (re2.extract(file_it.name(), s)==1) { int run_number = 0; Poco::NumberParser::tryParse(s, run_number); if (run_number > max_run_number && run_number <= run) { max_run_number = run_number; config_file = file_it.path().toString(); } } } } } return config_file; }
/** \brief An analogue of the MATLAB dir function * \param[in] dir_path input directory * \param[out] content_list a vector of strings representing names of files/folders in the directory */ inline void dir(const std::string &dir_path, std::vector<std::string> &content_list) { std::string dirPath (dir_path); // First normalize the beginning of the path // If directory path does not start with '/', './' or '../' append './' if (!(((dirPath.length() >= 1) && (dirPath.substr(0,1) == "/")) || ((dirPath.length() >= 2) && (dirPath.substr(0,2) == "./")) || ((dirPath.length() >= 3) && (dirPath.substr(0,3) == "../")))) { dirPath.insert(0, "./"); } // Find the rightmost '/' delimeter and split along it std::string left, right; size_t found = dirPath.find_last_of("/"); if (found == std::string::npos) { std::cout << "[dir] something wrong\n"; exit(EXIT_FAILURE); } else { left = dirPath.substr(0, found+1); right = dirPath.substr(found+1); } // Now analyze the right part std::string fileFormatStr; // If right part is empty list everything in left if (right.empty()) { fileFormatStr = "*"; dirPath = left; } // If right side containts a wildcard or an extension list everything in left that matches else if ((right.find("*") != std::string::npos) || (boost::filesystem::path(right).has_extension())) { fileFormatStr = right; dirPath = left; } // If right is a directory append it to left and list everything else if (!boost::filesystem::path(right).has_extension()) { fileFormatStr = "*"; dirPath = left + right; } // Generate regex expression std::string regexExpression (fileFormatStr); utl::str::replaceSubstring(regexExpression, ".", "[.]"); // Replace all occurences of '.' with '[.]' utl::str::replaceSubstring(regexExpression, "*", ".*"); // Replace all occurences of '*' with '.*' boost::regex filenameFormatRgx(regexExpression); // List content_list.resize(0); for (boost::filesystem::directory_iterator file_it(dirPath), end; file_it != end; ++file_it) { std::string curFileName = file_it->path().leaf().string(); // Get current file name if (boost::regex_match(curFileName, filenameFormatRgx)) // Check if it matches the filename format content_list.push_back(curFileName); } // Sort the list std::sort(content_list.begin(), content_list.end(), doj::alphanum_less<std::string>()); }
int main( int argc, char **argv ) try { if( argc != 2 ) { fcppt::io::cerr() << FCPPT_TEXT("Usage: ") << argv[0] << FCPPT_TEXT(" <dir1>\n"); return EXIT_FAILURE; } boost::filesystem::path const dir( fcppt::from_std_string( argv[1] ) ); for( boost::filesystem::recursive_directory_iterator dir_it( dir ), dir_end; dir_it != dir_end; ++dir_it ) { if( !needs_header( dir_it->path() ) ) continue; boost::filesystem::path const header( make_header( dir_it->path() ) ); fcppt::filesystem::ofstream file( header, std::ios_base::trunc ); string_vector filenames; if( !file.is_open() ) { fcppt::io::cerr() << FCPPT_TEXT("Failed to open ") << header << FCPPT_TEXT('\n'); return EXIT_FAILURE; } for( boost::filesystem::directory_iterator file_it( dir_it->path() ), file_end; file_it != file_end; ++file_it ) { if( file_it->path() == header ) continue; if( needs_header( file_it->path() ) ) filenames.push_back( fcppt::filesystem::path_to_string( make_header( file_it->path() ) ) ); else if( !boost::filesystem::is_directory( file_it->path() ) ) filenames.push_back( fcppt::filesystem::path_to_string( file_it->path() ) ); } std::sort( filenames.begin(), filenames.end() ); fcppt::string const include_guard_name( make_include_guard( header ) ); file << FCPPT_TEXT("#ifndef ") << include_guard_name << FCPPT_TEXT("\n#define ") << include_guard_name << FCPPT_TEXT("\n\n"); for( string_vector::const_iterator cur_name( filenames.begin() ); cur_name != filenames.end(); ++cur_name ) file << FCPPT_TEXT("#include <") << *cur_name << FCPPT_TEXT(">\n"); file << FCPPT_TEXT("\n#endif\n"); } } catch( fcppt::exception const &_error ) { fcppt::io::cerr() << _error.string() << FCPPT_TEXT('\n'); return EXIT_FAILURE; } catch( std::exception const &_error ) { std::cerr << _error.what() << '\n'; return EXIT_FAILURE; }