123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include <string>
- #include <vector>
- #include <stdio.h>
- #include <stdlib.h>
- #include <algorithm>
- #ifdef _WIN32
- #include <io.h>
- #else
- #include <glob.h>
- #endif
- #include <string.h>
- #include <nvbio/basic/types.h>
- #include <nvbio/basic/console.h>
- void list_files(const char* pattern, std::vector<std::string>& out_list)
- {
- #ifdef _WIN32
- const char dirsep = '\\';
- #else
- const char dirsep = '/';
- #endif
- std::string base_input_name(pattern);
- size_t last_slash = base_input_name.rfind(dirsep);
- std::string base_path = base_input_name.substr( 0, last_slash+1 );
- log_info(stderr, "directory : \"%s\"\n", base_path.c_str());
- typedef std::pair<nvbio::uint32, std::string> sortkey;
- std::vector<sortkey> files;
- sortkey temp;
- #ifdef _WIN32
- _finddata_t file_info;
- intptr_t find_handle = _findfirst( base_input_name.c_str(), &file_info );
- if (find_handle == -1)
- {
- log_error(stderr, "unable to locate \"%s\"", base_input_name.c_str());
- exit(1);
- }
- do {
- temp.second = base_path + std::string( file_info.name );
- files.push_back(temp);
- } while (_findnext( find_handle, &file_info) != -1);
- #else
- glob_t info;
- int stat = glob( base_input_name.c_str(), 0, 0, &info );
- if( stat != 0 )
- {
- fprintf(stderr, "unable to locate \"%s\"", base_input_name.c_str());
- exit(1);
- }
- for(nvbio::uint32 i=0; i<info.gl_pathc; ++i) {
- temp.second = std::string( info.gl_pathv[i] );
- files.push_back(temp);
- }
- globfree(&info);
- #endif
-
-
- std::vector<sortkey>::iterator iter;
- for(iter=files.begin(); iter!=files.end(); ++iter) {
- size_t end, begin;
- std::string numstring;
- end = iter->second.find_last_of(".");
- end = iter->second.find_last_of("0123456789", end);
- if( end == std::string::npos ) {
-
- iter->first = -1;
- } else {
- begin = 1 + iter->second.find_last_not_of("0123456789", end);
- numstring = iter->second.substr(begin, end-begin+1);
- iter->first = atoi( numstring.c_str() );
- }
- }
- std::sort(files.begin(), files.end());
-
- for(iter=files.begin(); iter!=files.end(); ++iter) {
- out_list.push_back(iter->second);
- }
- }
|