Go to the documentation of this file.00001
00008 #include "ui.h"
00009
00010
00011 Dedupe::CLI::UI::UI( std::ostream &MessageOut )
00012 : Message( MessageOut ), Header( "" )
00013 {}
00014
00015
00016 bool Dedupe::CLI::UI::WriteDuplicatesToFile( Dedupe::Core::Duplicates Dups,
00017 Dedupe::FilePath PathToFile )
00018 {
00019 boost::filesystem::ofstream Outputfile;
00020
00021 Outputfile.open( PathToFile, ofstream::out );
00022 if( !Outputfile )
00023 {
00024 Message << "ERROR: Could not open Outputfile\n" ;
00025 Outputfile.close();
00026 return false;
00027 }
00028
00029 Outputfile << "# " << Header << std::endl;
00030
00031 for( auto it = Dups.begin(); it != Dups.end(); ++it )
00032 {
00033 Outputfile << "GroupBegin:\n";
00034
00035 for( auto InIt= it->begin(); InIt != it->end(); ++InIt )
00036 {
00037 Outputfile << InIt->GetPath() << std::endl;
00038 }
00039 Outputfile <<"GroupEnd\n";
00040 }
00041
00042 Outputfile.close();
00043 return true;
00044 }
00045
00046 Dedupe::Core::FilesToProcess
00047 Dedupe::CLI::UI::ReadDecidedDuplicatesFromFile( Dedupe::FilePath PathToFile )
00048 {
00049 boost::filesystem::ifstream Inputfile;
00050
00051 Dedupe::Core::FilesToProcess Dups;
00052
00053 std::string Line, Decision, LinkPath;
00054
00055 Inputfile.open( PathToFile, ifstream::in );
00056 if( !Inputfile )
00057 {
00058 Message << "ERROR: Could not open Inputfile\n" ;
00059 Inputfile.close();
00060 Dups.clear();
00061 return Dups;
00062 }
00063 while( !Inputfile.eof() )
00064 {
00065 std::getline( Inputfile, Line );
00066 EraseComments( Line );
00067 EraseWhitespaces( Line );
00068 if( Line == "" )continue;
00069 if( !SplitPathFromDecision( Line, Decision, LinkPath ))
00070 {
00071 Message << "ERROR: Could not parse current Line: " << Line << std::endl;
00072 };
00073 if( Decision == "k" || Decision == "" || Decision == "keep" )
00074 {
00075 Dups.push_back( std::make_pair( Line, Dedupe::Core::HandleDuplicates(Dedupe::Core::HandleDuplicates::Keep )));
00076 }
00077 else if( Decision == "m" || Decision == "mark")
00078 {
00079 Dups.push_back( std::make_pair( Line, Dedupe::Core::HandleDuplicates(Dedupe::Core::HandleDuplicates::MarkAsKeep )));
00080 }
00081 else if( Decision == "d" || Decision == "delete")
00082 {
00083 Dups.push_back( std::make_pair( Line, Dedupe::Core::HandleDuplicates(Dedupe::Core::HandleDuplicates::Delete )));
00084 }
00085 else if( Decision == "l" || Decision == "link" )
00086 {
00087 Dedupe::Core::HandleDuplicates SpezialHandle( Dedupe::Core::HandleDuplicates::LinkTo );
00088 SpezialHandle.SetLinkPath( LinkPath );
00089 Dups.push_back( std::make_pair( Line, SpezialHandle ));
00090 }
00091 else
00092 {
00093 Dups.push_back( std::make_pair( Line, Dedupe::Core::HandleDuplicates(Dedupe::Core::HandleDuplicates::Error )));
00094 }
00095 }
00096
00097 return Dups;
00098 }
00099
00100 void Dedupe::CLI::UI::SetTitle( std::string Fileheader )
00101 {
00102 Header = Fileheader;
00103 }
00104
00105 void Dedupe::CLI::UI::EraseComments( std::string &FileLine )
00106 {
00107 int Commentsign;
00108
00109 Commentsign = FileLine.find_first_of('#');
00110
00111 if( Commentsign == -1 ) return;
00112 else FileLine.erase(Commentsign, FileLine.length());
00113 }
00114
00115 void Dedupe::CLI::UI::EraseWhitespaces( std::string &FileLine )
00116 {
00117
00118 while( FileLine.find(' ',FileLine.find_last_of('"')) != -1u )
00119 {
00120 FileLine.erase( FileLine.find( ' ', FileLine.find_last_of('"') ), 1 );
00121 }
00122 }
00123
00124
00125 bool Dedupe::CLI::UI::SplitPathFromDecision( std::string &LineString, std::string &Decision, std::string &Link )
00126 {
00127 boost::regex Path, DecisionPattern, PathDecision, PathDecisionPath;
00128 boost::smatch PathMatch, LinkMatch;
00129
00130 std::string tmp, tmp1;
00131
00132 Path = "\"([^\"]*)\"";
00133
00134 PathDecision = "\"([^\"]*)\"([\\w ]{1,})";
00135
00136 PathDecisionPath = "\"(.*?)\" [\\w ]*? \".*?\"";
00137
00138 if( boost::regex_match( LineString, Path ) )
00139 {
00140 Decision = "";
00141 Link = "";
00142
00143 return true;
00144 }
00145 else if( boost::regex_match( LineString, PathDecision ) )
00146 {
00147 boost::regex_search(LineString, PathMatch, Path );
00148
00149 tmp = PathMatch[ 0 ];
00150
00151 Decision = LineString.substr(tmp.size(), LineString.size());
00152
00153 LineString = PathMatch[ 0 ];
00154 Link = "";
00155
00156 return true;
00157 }
00158 else if( boost::regex_match( LineString, PathDecisionPath ) )
00159 {
00160 boost::regex_search( LineString, PathMatch, Path, boost::regex_constants::_match_flags::match_any);
00161
00162 tmp = PathMatch[0];
00163
00164 boost::regex_search( LineString, LinkMatch, Path, boost::regex_constants::_match_flags::match_all);
00165
00166 tmp1 = LinkMatch[0];
00167
00168 LineString = LineString.substr( tmp.size(), LineString.size());
00169 LineString = LineString.substr( 0, LineString.size() - tmp1.size());
00170
00171 Decision = LineString;
00172
00173 for( size_t i = 0 ; i <= Decision.size(); ++i )
00174 {
00175 if( Decision[ i ] == ' ' ) Decision.erase( i, 1 );
00176 }
00177
00178 LineString = PathMatch[ 0 ];
00179 Link = LinkMatch [ 0 ];
00180
00181 return true;
00182 }
00183
00184 else
00185 {
00186 Message << "ERROR: Could not parse Line: " << LineString << std::endl;
00187 return false;
00188 }
00189
00190 return false;
00191 }