core/vil/vil_image_list.cxx
Go to the documentation of this file.
00001 #include "vil_image_list.h"
00002 //:
00003 // \file
00004 #include <sys/stat.h>
00005 #include <vcl_cstdlib.h>
00006 #include <vil/vil_image_resource.h>
00007 #include <vil/vil_blocked_image_resource.h>
00008 #include <vil/vil_pyramid_image_resource.h>
00009 #include <vil/vil_load.h>
00010 
00011 #if defined(como4301) && defined(__linux__)
00012 # ifndef S_IFMT
00013 #  define S_IFMT 0170000
00014 # endif
00015 # ifndef S_IFDIR
00016 #  define S_IFDIR 0040000
00017 # endif
00018 #endif
00019 static bool il_verbose = false;
00020 bool vil_image_list::vil_is_directory(char const* fn)
00021 {
00022   struct stat fs;
00023   return stat(fn, &fs) == 0 && (fs.st_mode & S_IFMT) == S_IFDIR;
00024 }
00025 
00026 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00027 #if defined(VCL_BORLAND_56)
00028 # include <stdint.h> /* for intptr_t on Borland 5.6. */
00029 #endif
00030 #include <io.h>
00031 vcl_vector<vcl_string> vil_image_list::files()
00032 {
00033   vcl_vector<vcl_string> temp;
00034   if (!this->vil_is_directory(directory_.c_str()))
00035     return temp;
00036   // This mess should go away soon.
00037 # if defined VCL_VC_6 || defined VCL_VC_5 || defined VCL_BORLAND_55 || defined __MINGW32__
00038   typedef long handle_type;      // works with msvc6
00039 # else
00040   typedef intptr_t handle_type;  // not found by msvc6
00041 #endif
00042 
00043   handle_type handle;
00044   struct _finddata_t data;
00045 #ifdef VCL_BORLAND_55
00046   handle = _findfirst(const_cast<char*>((directory_+"\\*").c_str()), &data);
00047 #else
00048   handle = _findfirst((directory_+"\\*").c_str(), &data);
00049 #endif
00050   if (handle<0)
00051     return temp;
00052   vcl_string s = data.name;
00053   vcl_string filename = directory_+ "\\" + s;
00054   vil_image_resource_sptr resc;
00055   if (!this->vil_is_directory(filename.c_str()))
00056     temp.push_back(filename);
00057   while ( true )
00058   {
00059     if (_findnext(handle, &data) != 0) {
00060       _findclose(handle);
00061       return temp;
00062     }
00063     s = data.name;
00064     filename = directory_+ "\\" + s;
00065     if (!this->vil_is_directory(filename.c_str()))
00066       temp.push_back(filename);
00067   }
00068 
00069   return temp;
00070 }
00071 #else // !defined(VCL_WIN32) || defined(__CYGWIN__)
00072 
00073 #include <dirent.h>
00074 vcl_vector<vcl_string> vil_image_list::files()
00075 {
00076   vcl_vector<vcl_string> temp;
00077   if (!this->vil_is_directory(directory_.c_str()))
00078     return temp;
00079   DIR* dir_handle = opendir(directory_.c_str());
00080   dirent* de;
00081   de = readdir(dir_handle);
00082   if (de==0)
00083     return temp;
00084   vcl_string s = de->d_name;
00085   vcl_string filename = directory_+ "/" + s;
00086   if (!this->vil_is_directory(filename.c_str()))
00087   {
00088 #ifdef IL_DEBUG
00089     vcl_cout << "Found File(0) " << filename << '\n';
00090 #endif
00091     temp.push_back(filename);
00092   }
00093   while ( true )
00094   {
00095     de = readdir(dir_handle);
00096     if (de == 0) {
00097       closedir(dir_handle);
00098       return temp;
00099     }
00100     s = de->d_name;
00101     filename = directory_+ "/" + s;
00102     if (!this->vil_is_directory(filename.c_str()))
00103     {
00104 #ifdef IL_DEBUG
00105       vcl_cout << "Found File " << filename << '\n';
00106 #endif
00107       temp.push_back(filename);
00108     }
00109   }
00110   return temp;
00111 }
00112 
00113 #endif // !defined(VCL_WIN32) || defined(__CYGWIN__)
00114 vcl_vector<vil_image_resource_sptr> vil_image_list::resources()
00115 {
00116   vcl_vector<vil_image_resource_sptr>  temp;
00117   vcl_vector<vcl_string> fs = this->files();
00118   for (vcl_vector<vcl_string>::iterator fit = fs.begin();
00119        fit != fs.end(); ++fit)
00120   {
00121     vil_image_resource_sptr resc = vil_load_image_resource((*fit).c_str(), il_verbose);
00122     if (resc)
00123       temp.push_back(resc);
00124   }
00125   return temp;
00126 }
00127 
00128 vcl_vector<vil_image_resource_sptr> vil_image_list::blocked_resources()
00129 {
00130   vcl_vector<vil_image_resource_sptr>  temp;
00131   vcl_vector<vcl_string> fs = this->files();
00132   for (vcl_vector<vcl_string>::iterator fit = fs.begin();
00133        fit != fs.end(); ++fit)
00134   {
00135     vil_image_resource_sptr resc = vil_load_image_resource((*fit).c_str(), il_verbose);
00136     vil_image_resource_sptr bir = blocked_image_resource(resc).ptr();
00137     if (bir)
00138       temp.push_back(bir);
00139   }
00140   return temp;
00141 }
00142 
00143 vcl_vector<vil_image_resource_sptr> vil_image_list::pyramids()
00144 {
00145   vcl_vector<vil_image_resource_sptr>  temp;
00146   vcl_vector<vcl_string> fs = this->files();
00147   for (vcl_vector<vcl_string>::iterator fit = fs.begin();
00148        fit != fs.end(); ++fit)
00149   {
00150     vil_pyramid_image_resource_sptr pyr =
00151       vil_load_pyramid_resource((*fit).c_str(), il_verbose);
00152     if (pyr)
00153       temp.push_back(pyr.ptr());
00154   }
00155   return temp;
00156 }
00157 //:remove a file
00158 bool vil_image_list::remove_file(vcl_string& filename)
00159 {
00160 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00161   vcl_string command = "del " + filename;
00162 #else
00163   vcl_string command = "rm " + filename;
00164 #endif
00165   return vcl_system(command.c_str())==0;
00166 }
00167 
00168 //:removes all files from the directory. sub-directories are not touched
00169 bool vil_image_list::clean_directory()
00170 {
00171   vcl_vector<vcl_string> filez = this->files();
00172   bool good = true;
00173   vcl_cout << "starting to remove ..\n";
00174   for (vcl_vector<vcl_string>::iterator fit = filez.begin();
00175        fit != filez.end(); ++fit)
00176     if (!this->remove_file(*fit))
00177       good = false;
00178   vcl_cout << "finished remove ..\n";
00179   return good;
00180 }