vil_image_list.cxx
Go to the documentation of this file.
1 #include <cstdlib>
2 #include "vil_image_list.h"
3 //:
4 // \file
5 #include <sys/stat.h>
6 #ifdef _MSC_VER
7 # include <vcl_msvc_warnings.h>
8 #endif
12 #include <vil/vil_load.h>
13 
14 static bool il_verbose = false;
16 {
17  struct stat fs;
18  return stat(fn, &fs) == 0 && (fs.st_mode & S_IFMT) == S_IFDIR;
19 }
20 
21 #if defined(_WIN32) && !defined(__CYGWIN__)
22 #include <io.h>
23 std::vector<std::string> vil_image_list::files()
24 {
25  std::vector<std::string> temp;
26  if (!this->vil_is_directory(directory_.c_str()))
27  return temp;
28  // This mess should go away soon.
29 # if defined __MINGW32__
30  typedef long handle_type; // works with msvc6
31 # else
32  typedef intptr_t handle_type; // not found by msvc6
33 #endif
34 
35  handle_type handle;
36  struct _finddata_t data;
37  handle = _findfirst((directory_+"\\*").c_str(), &data);
38  if (handle<0)
39  return temp;
40  std::string s = data.name;
41  std::string filename = directory_+ "\\" + s;
43  if (!this->vil_is_directory(filename.c_str()))
44  temp.push_back(filename);
45  while ( true )
46  {
47  if (_findnext(handle, &data) != 0) {
48  _findclose(handle);
49  return temp;
50  }
51  s = data.name;
52  filename = directory_+ "\\" + s;
53  if (!this->vil_is_directory(filename.c_str()))
54  temp.push_back(filename);
55  }
56 
57  return temp;
58 }
59 #else // !defined(_WIN32) || defined(__CYGWIN__)
60 
61 #include <dirent.h>
62 std::vector<std::string> vil_image_list::files()
63 {
64  std::vector<std::string> temp;
65  if (!this->vil_is_directory(directory_.c_str()))
66  return temp;
67  DIR* dir_handle = opendir(directory_.c_str());
68  dirent* de;
69  de = readdir(dir_handle);
70  if (de==nullptr)
71  return temp;
72  std::string s = de->d_name;
73  std::string filename = directory_+ "/" + s;
74  if (!this->vil_is_directory(filename.c_str()))
75  {
76 #ifdef IL_DEBUG
77  std::cout << "Found File(0) " << filename << '\n';
78 #endif
79  temp.push_back(filename);
80  }
81  while ( true )
82  {
83  de = readdir(dir_handle);
84  if (de == nullptr) {
85  closedir(dir_handle);
86  return temp;
87  }
88  s = de->d_name;
89  filename = directory_+ "/" + s;
90  if (!this->vil_is_directory(filename.c_str()))
91  {
92 #ifdef IL_DEBUG
93  std::cout << "Found File " << filename << '\n';
94 #endif
95  temp.push_back(filename);
96  }
97  }
98  return temp;
99 }
100 
101 #endif // !defined(_WIN32) || defined(__CYGWIN__)
102 std::vector<vil_image_resource_sptr> vil_image_list::resources()
103 {
104  std::vector<vil_image_resource_sptr> temp;
105  std::vector<std::string> fs = this->files();
106  for (auto & f : fs)
107  {
108  vil_image_resource_sptr resc = vil_load_image_resource(f.c_str(), il_verbose);
109  if (resc)
110  temp.push_back(resc);
111  }
112  return temp;
113 }
114 
115 std::vector<vil_image_resource_sptr> vil_image_list::blocked_resources()
116 {
117  std::vector<vil_image_resource_sptr> temp;
118  std::vector<std::string> fs = this->files();
119  for (auto & f : fs)
120  {
121  vil_image_resource_sptr resc = vil_load_image_resource(f.c_str(), il_verbose);
123  if (bir)
124  temp.push_back(bir);
125  }
126  return temp;
127 }
128 
129 std::vector<vil_image_resource_sptr> vil_image_list::pyramids()
130 {
131  std::vector<vil_image_resource_sptr> temp;
132  std::vector<std::string> fs = this->files();
133  for (auto & f : fs)
134  {
136  vil_load_pyramid_resource(f.c_str(), il_verbose);
137  if (pyr)
138  temp.emplace_back(pyr.ptr());
139  }
140  return temp;
141 }
142 //:remove a file
143 bool vil_image_list::remove_file(std::string& filename)
144 {
145 #if defined(_WIN32) && !defined(__CYGWIN__)
146  std::string command = "del " + filename;
147 #else
148  std::string command = "rm " + filename;
149 #endif
150  return std::system(command.c_str())==0;
151 }
152 
153 //:removes all files from the directory. sub-directories are not touched
155 {
156  std::vector<std::string> filez = this->files();
157  bool good = true;
158  std::cout << "starting to remove ..\n";
159  for (auto & fit : filez)
160  if (!this->remove_file(fit))
161  good = false;
162  std::cout << "finished remove ..\n";
163  return good;
164 }
std::vector< vil_image_resource_sptr > resources()
finds all the image files in the directory, regardless of extension.
std::vector< vil_image_resource_sptr > blocked_resources()
finds all the blocked image files in the directory, regardless of extension.
static bool vil_is_directory(char const *)
utility functions.
bool clean_directory()
cleans the directory, i.e. removes all the files.
vil_image_resource_sptr vil_load_image_resource(char const *filename, bool verbose)
Load an image resource object from a file.
Definition: vil_load.cxx:68
read an image from a file
std::vector< vil_image_resource_sptr > pyramids()
finds all the pyramid files in the directory, regardless of extension.
A blocked representation of the image_resource.
vil_blocked_image_resource_sptr blocked_image_resource(const vil_image_resource_sptr &ir)
cast to blocked resource if possible.
Representation of a pyramid resolution hierarchy.
vil_pyramid_image_resource_sptr vil_load_pyramid_resource(char const *directory_or_file, bool verbose)
Load a pyramid image resource object from a file or directory.
Definition: vil_load.cxx:100
An image resource list reader. Finds all resources of a type in the given directory.
std::vector< std::string > files()
finds all the files in the directory, regardless of extension.
Representation of a generic image source or destination.
bool remove_file(std::string &filename)
remove a file.
std::string directory_
T * ptr() const
These methods all return the raw/dumb pointer.