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