filelist.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * nvbio
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <string>
  28. #include <vector>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <algorithm>
  32. #ifdef _WIN32
  33. #include <io.h>
  34. #else
  35. #include <glob.h>
  36. #endif
  37. #include <string.h>
  38. #include <nvbio/basic/types.h>
  39. #include <nvbio/basic/console.h>
  40. /* Appends sorted list of files matching pattern to out_list.
  41. * Should work on linux or windows */
  42. void list_files(const char* pattern, std::vector<std::string>& out_list)
  43. {
  44. #ifdef _WIN32
  45. const char dirsep = '\\';
  46. #else
  47. const char dirsep = '/';
  48. #endif
  49. std::string base_input_name(pattern);
  50. size_t last_slash = base_input_name.rfind(dirsep);
  51. std::string base_path = base_input_name.substr( 0, last_slash+1 );
  52. log_info(stderr, "directory : \"%s\"\n", base_path.c_str());
  53. typedef std::pair<nvbio::uint32, std::string> sortkey;
  54. std::vector<sortkey> files;
  55. sortkey temp;
  56. #ifdef _WIN32
  57. _finddata_t file_info;
  58. intptr_t find_handle = _findfirst( base_input_name.c_str(), &file_info );
  59. if (find_handle == -1)
  60. {
  61. log_error(stderr, "unable to locate \"%s\"", base_input_name.c_str());
  62. exit(1);
  63. }
  64. do {
  65. temp.second = base_path + std::string( file_info.name );
  66. files.push_back(temp);
  67. } while (_findnext( find_handle, &file_info) != -1);
  68. #else
  69. glob_t info;
  70. int stat = glob( base_input_name.c_str(), 0, 0, &info );
  71. if( stat != 0 )
  72. {
  73. fprintf(stderr, "unable to locate \"%s\"", base_input_name.c_str());
  74. exit(1);
  75. }
  76. for(nvbio::uint32 i=0; i<info.gl_pathc; ++i) {
  77. temp.second = std::string( info.gl_pathv[i] );
  78. files.push_back(temp);
  79. }
  80. globfree(&info);
  81. #endif
  82. // Figure out what the sort number should be for each file
  83. // We sort by the last block of numbers before the last dot
  84. std::vector<sortkey>::iterator iter;
  85. for(iter=files.begin(); iter!=files.end(); ++iter) {
  86. size_t end, begin;
  87. std::string numstring;
  88. end = iter->second.find_last_of(".");
  89. end = iter->second.find_last_of("0123456789", end);
  90. if( end == std::string::npos ) {
  91. // no numbers in file
  92. iter->first = -1; // sort will be random but before numbers
  93. } else {
  94. begin = 1 + iter->second.find_last_not_of("0123456789", end);
  95. numstring = iter->second.substr(begin, end-begin+1);
  96. iter->first = atoi( numstring.c_str() );
  97. }
  98. }
  99. std::sort(files.begin(), files.end());
  100. for(iter=files.begin(); iter!=files.end(); ++iter) {
  101. out_list.push_back(iter->second);
  102. }
  103. }