NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
filelist.cpp
Go to the documentation of this file.
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 
28 #include <string>
29 #include <vector>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <algorithm>
33 #ifdef _WIN32
34 #include <io.h>
35 #else
36 #include <glob.h>
37 #endif
38 #include <string.h>
39 #include <nvbio/basic/types.h>
40 #include <nvbio/basic/console.h>
41 
42 /* Appends sorted list of files matching pattern to out_list.
43  * Should work on linux or windows */
44 void list_files(const char* pattern, std::vector<std::string>& out_list)
45 {
46 #ifdef _WIN32
47  const char dirsep = '\\';
48 #else
49  const char dirsep = '/';
50 #endif
51 
52  std::string base_input_name(pattern);
53  size_t last_slash = base_input_name.rfind(dirsep);
54  std::string base_path = base_input_name.substr( 0, last_slash+1 );
55  log_info(stderr, "directory : \"%s\"\n", base_path.c_str());
56 
57  typedef std::pair<nvbio::uint32, std::string> sortkey;
58  std::vector<sortkey> files;
59  sortkey temp;
60 
61 #ifdef _WIN32
62 
63  _finddata_t file_info;
64  intptr_t find_handle = _findfirst( base_input_name.c_str(), &file_info );
65  if (find_handle == -1)
66  {
67  log_error(stderr, "unable to locate \"%s\"", base_input_name.c_str());
68  exit(1);
69  }
70 
71  do {
72  temp.second = base_path + std::string( file_info.name );
73  files.push_back(temp);
74  } while (_findnext( find_handle, &file_info) != -1);
75 
76 #else
77 
78  glob_t info;
79  int stat = glob( base_input_name.c_str(), 0, 0, &info );
80  if( stat != 0 )
81  {
82  fprintf(stderr, "unable to locate \"%s\"", base_input_name.c_str());
83  exit(1);
84  }
85  for(nvbio::uint32 i=0; i<info.gl_pathc; ++i) {
86  temp.second = std::string( info.gl_pathv[i] );
87  files.push_back(temp);
88  }
89  globfree(&info);
90 
91 #endif
92 
93  // Figure out what the sort number should be for each file
94  // We sort by the last block of numbers before the last dot
95  std::vector<sortkey>::iterator iter;
96  for(iter=files.begin(); iter!=files.end(); ++iter) {
97  size_t end, begin;
98  std::string numstring;
99  end = iter->second.find_last_of(".");
100  end = iter->second.find_last_of("0123456789", end);
101  if( end == std::string::npos ) {
102  // no numbers in file
103  iter->first = -1; // sort will be random but before numbers
104  } else {
105  begin = 1 + iter->second.find_last_not_of("0123456789", end);
106  numstring = iter->second.substr(begin, end-begin+1);
107  iter->first = atoi( numstring.c_str() );
108  }
109  }
110  std::sort(files.begin(), files.end());
111 
112  for(iter=files.begin(); iter!=files.end(); ++iter) {
113  out_list.push_back(iter->second);
114  }
115 }
116