00001
00002
00003
00004
00005
00006
00007 #include "ComPlusPlus/Directory.h"
00008
00009
00010 #include <regex.h>
00011
00012
00013 compp::Directory::Directory() {
00014
00015 SortModesAsc = true;
00016
00017 }
00018
00019 void compp::Directory::setPath( const std::string & Path, const std::string & Filter ) {
00020
00021 SortModesAsc = true;
00022
00023 CreateListing ( Path, Filter );
00024 DirPath = Path;
00025
00026 }
00027
00028
00029 compp::Directory::Directory( const std::string & Path, const std::string & Filter ) {
00030
00031 setPath(Path, Filter);
00032
00033 }
00034
00035 compp::Directory::~Directory() {
00036
00037
00038 }
00039
00040 std::list<compp::DirEntry>::iterator compp::Directory::getFirst() {
00041
00042 return DirectoryList.begin();
00043
00044
00045 }
00046
00047 std::list<compp::DirEntry>::iterator compp::Directory::end() {
00048
00049 return DirectoryList.end();
00050
00051 }
00052
00053
00054 void compp::Directory::Refresh() {
00055
00056 CreateListing ( DirPath );
00057
00058 }
00059
00060 void compp::Directory::SortName(){
00061
00062 if ( SortModesAsc )
00063 DirectoryList.sort ( compp::DirEntry::lessName );
00064 else
00065 DirectoryList.sort ( compp::DirEntry::greaterName );
00066
00067 }
00068
00069 void compp::Directory::setSortAsc() {
00070
00071 SortModesAsc = true;
00072
00073 }
00074
00075 void compp::Directory::setSortDesc() {
00076
00077 SortModesAsc = false;
00078
00079 }
00080
00081 void compp::Directory::CreateListing( const std::string & s, const std::string & Filter ) {
00082
00083 regex_t reg;
00084 if ( Filter != "" ) {
00085 if ( 0 != regcomp( & reg, Filter.c_str(), REG_EXTENDED ) ) {
00086 std::string errstr = "Regex " ;
00087 errstr += Filter ;
00088 errstr += " is invalid." ;
00089 throw syspp::ComException ( errstr.c_str() );
00090 }
00091 }
00092
00093 if ( ! DirectoryList.empty () ) {
00094 DirectoryList.clear();
00095 }
00096
00097 DIR *de;
00098 struct dirent *di;
00099
00100 de = opendir( s.c_str() );
00101
00102 if ( de == NULL ) {
00103 std::string errstr = "Directory " ;
00104 errstr += s ;
00105 errstr += " does not exist" ;
00106 throw syspp::ComException ( errstr.c_str() );
00107 }
00108
00109 while ( NULL != ( di = readdir(de) ) ) {
00110 std::string full = s;
00111 full += "/";
00112 full += di->d_name;
00113
00114 compp::DirEntry e;
00115 e.setPath (full);
00116
00117 if ( Filter != "" ) {
00118 regmatch_t pmatch[256];
00119 if ( 0 == regexec( ®, full.c_str(), 1 , pmatch, 0) )
00120 DirectoryList.push_back ( e );
00121 } else
00122 DirectoryList.push_back ( e );
00123 }
00124 closedir ( de );
00125 }
00126