init
This commit is contained in:
		
							
								
								
									
										42
									
								
								examples/SConstruct
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								examples/SConstruct
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| import os | ||||
|  | ||||
| # SConstruct for dtl examples | ||||
|  | ||||
| env   = Environment(CPPPATH='..') | ||||
| debug = ARGUMENTS.get('debug', 'n') | ||||
| if debug == 'y' or debug == 'yes': | ||||
|     env.Append(CPPFLAGS = ['-Wall', '-g']) | ||||
| else: | ||||
|     env.Append(CPPFLAGS = ['-Wall', '-O2']) | ||||
|  | ||||
| if os.sys.platform != "win32": | ||||
|     env.Append(CPPDEFINES = ['HAVE_UNISTD_H']) | ||||
|  | ||||
| conf = Configure(env); | ||||
|  | ||||
| if not conf.CheckCXX(): | ||||
|     print "The C++ compiler is not installed!" | ||||
|     Exit(1) | ||||
|  | ||||
| libs = ['stdc++'] | ||||
| for lib in libs: | ||||
|     if not conf.CheckLib(lib): | ||||
|         print "library " + lib + " not installed!" | ||||
|         Exit(1) | ||||
|  | ||||
| conf.Finish() | ||||
|  | ||||
| targets = { 'strdiff'    : ['strdiff.cpp',    'common.cpp'], # diff between two string sequences | ||||
|             'intdiff'    : ['intdiff.cpp'],                  # diff between two integer sequences | ||||
|             'unidiff'    : ['unidiff.cpp',    'common.cpp'], # unified diff between two files | ||||
|             'unistrdiff' : ['unistrdiff.cpp', 'common.cpp'], # unified diff between two strings | ||||
|             'bdiff'      : ['bdiff.cpp',      'common.cpp'], # diff between two byte sequences | ||||
|             'strdiff3'   : ['strdiff3.cpp',   'common.cpp'], # three-way string diff program using dtl | ||||
|             'intdiff3'   : ['intdiff3.cpp'],                 # three-way integer diff program using dtl | ||||
|             'patch'      : ['patch.cpp',      'common.cpp'], # string patch program using dtl | ||||
|             'fpatch'     : ['fpatch.cpp',     'common.cpp'], # file patch program using dtl | ||||
|             'st2ses'     : ['st2ses.cpp',     'common.cpp'], # convert SES format file to SES instance | ||||
|             'strdiff_cp' : ['strdiff_cp.cpp', 'common.cpp'], # diff between two string sequences with custom printer | ||||
|             } | ||||
|  | ||||
| [ env.Program(target, targets[target]) for target in targets ] | ||||
							
								
								
									
										73
									
								
								examples/bdiff.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								examples/bdiff.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
|  | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <string> | ||||
| #include <cstdio> | ||||
| #include <sys/types.h> | ||||
| #include <sys/stat.h> | ||||
| #include <fcntl.h> | ||||
| #ifdef HAVE_UNISTD_H | ||||
| #include <unistd.h> | ||||
| #endif // HAVE_UNISTD_H | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| typedef unsigned char  elem; | ||||
| typedef vector< elem > sequence; | ||||
|  | ||||
| static int create_byte_seq(const char *fs, sequence& seq); | ||||
| static int create_byte_seq(const char *fs, sequence& seq) | ||||
| { | ||||
|     int  fd; | ||||
|     int  siz; | ||||
|     elem buf[BUFSIZ]; | ||||
|     if ((fd = open(fs, O_RDONLY)) == -1) { | ||||
|         cout << "Opening failed." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|     while ((siz = read(fd, buf, sizeof(buf))) > 0) { | ||||
|         for (int i=0;i<siz;++i) { | ||||
|             seq.push_back(buf[i]); | ||||
|         } | ||||
|     } | ||||
|     if (siz < 0) { | ||||
|         close(fd); | ||||
|         cout << "Read error." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|     close(fd); | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     string   fs1(argv[1]); | ||||
|     string   fs2(argv[2]); | ||||
|     sequence seq1; | ||||
|     sequence seq2; | ||||
|      | ||||
|     create_byte_seq(fs1.c_str(), seq1); | ||||
|     create_byte_seq(fs2.c_str(), seq2); | ||||
|      | ||||
|     Diff< elem, sequence > d(seq1, seq2); | ||||
|     d.compose(); | ||||
|  | ||||
|     if (d.getEditDistance() == 0) { | ||||
|         cout << fs1 << " is the same as "    << fs2 << endl; | ||||
|     } else { | ||||
|         cout << fs1 << " is different from " << fs2 << endl; | ||||
|     } | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										20
									
								
								examples/common.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								examples/common.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
|  | ||||
| #include "common.hpp" | ||||
|  | ||||
| bool isFileExist (string& fs) { | ||||
|     FILE *fp; | ||||
|     if ((fp = fopen(fs.c_str(), "r")) == NULL) { | ||||
|         return false; | ||||
|     } | ||||
|     fclose(fp); | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| bool isFewArgs (int argc, int limit) { | ||||
|     bool ret = false; | ||||
|     if (argc < limit) { | ||||
|         ret = true; | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
							
								
								
									
										13
									
								
								examples/common.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								examples/common.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
|  | ||||
| #ifndef DTL_EXAMPLE_COMMON_H | ||||
| #define DTL_EXAMPLE_COMMON_H | ||||
|  | ||||
| #include <string> | ||||
| #include <cstdio> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| bool isFileExist (string& fs); | ||||
| bool isFewArgs (int argc, int limit = 3); | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										73
									
								
								examples/fpatch.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								examples/fpatch.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <sstream> | ||||
| #include <fstream> | ||||
| #include <vector> | ||||
| #include <cassert> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     string A(argv[1]); | ||||
|     string B(argv[2]); | ||||
|     bool   fileExist = true; | ||||
|      | ||||
|     if (!isFileExist(A)) { | ||||
|         cerr << "file A does not exist" << endl; | ||||
|         fileExist = false; | ||||
|     } | ||||
|      | ||||
|     if (!isFileExist(B)) { | ||||
|         cerr << "file B does not exist" << endl; | ||||
|         fileExist = false; | ||||
|     } | ||||
|      | ||||
|     if (!fileExist) { | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef string elem; | ||||
|     typedef vector< elem > sequence; | ||||
|  | ||||
|     ifstream      Aifs(A.c_str()); | ||||
|     ifstream      Bifs(B.c_str()); | ||||
|     elem          buf; | ||||
|     sequence      ALines,  BLines; | ||||
|     ostringstream ossLine, ossInfo; | ||||
|      | ||||
|     while(getline(Aifs, buf)){ | ||||
|         ALines.push_back(buf); | ||||
|     } | ||||
|     while(getline(Bifs, buf)){ | ||||
|         BLines.push_back(buf); | ||||
|     } | ||||
|      | ||||
|     Diff< elem > d(ALines, BLines); | ||||
|     d.compose(); | ||||
|      | ||||
|     sequence s1 = ALines; | ||||
|     sequence s2 = d.patch(s1); | ||||
|      | ||||
|     // fpatch  | ||||
|     assert(BLines == s2); | ||||
|     cout << "fpatch succeeded" << endl; | ||||
|      | ||||
|     d.composeUnifiedHunks(); | ||||
|     sequence s3 = d.uniPatch(s1); | ||||
|      | ||||
|     // unipatch  | ||||
|     assert(BLines == s3); | ||||
|     cout << "unipatch succeeded" << endl; | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										49
									
								
								examples/intdiff.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								examples/intdiff.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
|  | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <dtl/dtl.hpp> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| int main(int, char**){ | ||||
|      | ||||
|     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | ||||
|     int b[] = {3, 5, 1, 4, 5, 1, 7, 9, 6, 10}; | ||||
|     int asiz = sizeof(a) / sizeof(int); | ||||
|     int bsiz = sizeof(b) / sizeof(int); | ||||
|     for (int i=0;i<asiz;++i) { | ||||
|         cout << a[i] << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|     for (int i=0;i<bsiz;++i) { | ||||
|         cout << b[i] << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|      | ||||
|     typedef  int elem; | ||||
|     typedef  vector< int > sequence; | ||||
|  | ||||
|     sequence A(&a[0], &a[asiz]); | ||||
|     sequence B(&b[0], &b[bsiz]); | ||||
|     Diff< elem > d(A, B); | ||||
|     d.compose(); | ||||
|  | ||||
|     // editDistance | ||||
|     cout << "editDistance:" << d.getEditDistance() << endl; | ||||
|  | ||||
|     // Longest Common Subsequence | ||||
|     sequence lcs_v = d.getLcsVec(); | ||||
|     cout << "LCS: "; | ||||
|     for (sequence::iterator vit=lcs_v.begin();vit!=lcs_v.end();++vit) { | ||||
|         cout << *vit << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|      | ||||
|     // Shortest Edit Script | ||||
|     cout << "SES" << endl; | ||||
|     d.printSES(); | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										57
									
								
								examples/intdiff3.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								examples/intdiff3.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <cassert> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff3; | ||||
|  | ||||
| int main(int, char**) { | ||||
|      | ||||
|     int a[10]      = {1, 2, 3, 4, 5, 6, 7, 3, 9, 10}; | ||||
|     int b[10]      = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | ||||
|     int c[10]      = {1, 2, 3, 9, 5, 6, 7, 8, 9, 10}; | ||||
|     int answer[10] = {1, 2, 3, 9, 5, 6, 7, 3, 9, 10}; | ||||
|      | ||||
|     cout << "a:"; | ||||
|     for (int i=0;i<10;++i) { | ||||
|         cout << a[i] << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|     cout << "b:"; | ||||
|     for (int i=0;i<10;++i) { | ||||
|         cout << b[i] << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|     cout << "c:"; | ||||
|     for (int i=0;i<10;++i) { | ||||
|         cout << c[i] << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|      | ||||
|     typedef int elem; | ||||
|     typedef vector< int > sequence; | ||||
|     sequence A(&a[0], &a[10]); | ||||
|     sequence B(&b[0], &b[10]); | ||||
|     sequence C(&c[0], &c[10]); | ||||
|     sequence Answer(&answer[0], &answer[10]); | ||||
|     Diff3< elem > diff3(A, B, C); | ||||
|     diff3.compose(); | ||||
|     if (!diff3.merge()) { | ||||
|         cerr << "conflict." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|     sequence s = diff3.getMergedSequence(); | ||||
|     cout << "s:"; | ||||
|     for (sequence::iterator it=s.begin();it!=s.end();++it) { | ||||
|         cout << *it << " "; | ||||
|     } | ||||
|     cout << endl; | ||||
|      | ||||
|     assert(s == Answer); | ||||
|     cout << "intdiff3 OK" << endl; | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										44
									
								
								examples/patch.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								examples/patch.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <cassert> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| int main(int argc, char *argv[]) { | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef char elem; | ||||
|     typedef string sequence; | ||||
|  | ||||
|     sequence A(argv[1]); | ||||
|     sequence B(argv[2]); | ||||
|      | ||||
|     Diff< elem, sequence > d(A, B); | ||||
|     d.compose(); | ||||
|      | ||||
|     sequence s1(A); | ||||
|     sequence s2 = d.patch(s1); | ||||
|     d.composeUnifiedHunks(); | ||||
|     sequence s3 = d.uniPatch(s1); | ||||
|      | ||||
|     cout << "before:" << s1 << endl; | ||||
|     cout << "after :" << s2 << endl; | ||||
|     assert(B == s2); | ||||
|     cout << "patch succeeded" << endl; | ||||
|      | ||||
|     cout << "before:" << s1 << endl; | ||||
|     cout << "after :" << s3 << endl; | ||||
|     assert(B == s3); | ||||
|     cout << "unipatch succeeded" << endl; | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										26
									
								
								examples/printers.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								examples/printers.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| #ifndef DTL_PRINTERS | ||||
| #define DTL_PRINTERS | ||||
|  | ||||
| template <typename sesElem, typename stream = ostream > | ||||
| class customChangePrinter : public Printer < sesElem, stream > | ||||
| { | ||||
| public : | ||||
|     customChangePrinter () : Printer < sesElem, stream > () {} | ||||
|     customChangePrinter (stream& out) : Printer < sesElem, stream > (out) {} | ||||
|     ~customChangePrinter () {} | ||||
|     void operator() (const sesElem& se) const { | ||||
|         switch (se.second.type) { | ||||
|         case SES_ADD: | ||||
|             this->out_ << "Add: " << se.first << endl; | ||||
|             break; | ||||
|         case SES_DELETE: | ||||
|             this->out_ << "Delete: " << se.first << endl; | ||||
|             break; | ||||
|         case SES_COMMON: | ||||
|             this->out_ << "Common: " << se.first << endl; | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
| }; | ||||
|  | ||||
| #endif // DTL_PRINTERS | ||||
							
								
								
									
										35
									
								
								examples/st2ses.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								examples/st2ses.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <sstream> | ||||
| #include <vector> | ||||
| #include <string> | ||||
|  | ||||
| using namespace std; | ||||
| using namespace dtl; | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc, 2)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|  | ||||
|     typedef string elem; | ||||
|     typedef vector< string > sequence; | ||||
|  | ||||
|     string s(argv[1]); | ||||
|  | ||||
|     if (!isFileExist(s)) { | ||||
|         cerr << s << " is invalid." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|  | ||||
|     ifstream fs(s.c_str()); | ||||
|     const Ses< elem > ses = Diff< elem, sequence >::composeSesFromStream< ifstream >(fs); | ||||
|     dtl::Diff< elem, sequence >::printSES(ses); | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										42
									
								
								examples/strdiff.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								examples/strdiff.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <string> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef char   elem; | ||||
|     typedef string sequence; | ||||
|  | ||||
|     sequence A(argv[1]); | ||||
|     sequence B(argv[2]); | ||||
|      | ||||
|     Diff< elem, sequence > d(A, B); | ||||
|     //d.onOnlyEditDistance(); | ||||
|     d.compose(); | ||||
|      | ||||
|     // editDistance | ||||
|     cout << "editDistance:" << d.getEditDistance() << endl; | ||||
|      | ||||
|     // Longest Common Subsequence | ||||
|     vector< elem > lcs_v = d.getLcsVec(); | ||||
|     sequence       lcs_s(lcs_v.begin(), lcs_v.end()); | ||||
|     cout << "LCS:" << lcs_s << endl; | ||||
|      | ||||
|     // Shortest Edit Script | ||||
|     cout << "SES" << endl; | ||||
|     d.printSES(); | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										35
									
								
								examples/strdiff3.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								examples/strdiff3.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <string> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff3; | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc, 4)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef char   elem; | ||||
|     typedef string sequence; | ||||
|  | ||||
|     sequence A(argv[1]); | ||||
|     sequence B(argv[2]); | ||||
|     sequence C(argv[3]); | ||||
|      | ||||
|     Diff3< elem, sequence > diff3(A, B, C); | ||||
|     diff3.compose(); | ||||
|     if (!diff3.merge()) { | ||||
|         cerr << "conflict." << endl; | ||||
|         return 0; | ||||
|     } | ||||
|     cout << "result:" << diff3.getMergedSequence() << endl; | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										40
									
								
								examples/strdiff_cp.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								examples/strdiff_cp.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <sstream> | ||||
| #include <string> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
| using dtl::SES_ADD; | ||||
| using dtl::SES_DELETE; | ||||
| using dtl::SES_COMMON; | ||||
| using dtl::Printer; | ||||
|  | ||||
| #include "printers.hpp" | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef char   elem; | ||||
|     typedef string sequence; | ||||
|  | ||||
|     sequence A(argv[1]); | ||||
|     sequence B(argv[2]); | ||||
|      | ||||
|     Diff< elem, sequence > d(A, B); | ||||
|     d.compose(); | ||||
|      | ||||
|     // Shortest Edit Script | ||||
|     cout << "SES" << endl; | ||||
|  | ||||
|     d.printSES < ostream, customChangePrinter > (cout); | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										111
									
								
								examples/unidiff.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								examples/unidiff.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,111 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <sstream> | ||||
| #include <vector> | ||||
|  | ||||
| #include <time.h> | ||||
| #include <sys/stat.h> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
| using dtl::elemInfo; | ||||
| using dtl::uniHunk; | ||||
|  | ||||
| static void showStats (string fp1, string fp2); | ||||
| static void unifiedDiff (string fp1, string fp2);  | ||||
|  | ||||
| static void showStats (string fp1, string fp2)  | ||||
| { | ||||
|     const int    MAX_LENGTH    = 255; | ||||
|     char         time_format[] = "%Y-%m-%d %H:%M:%S %z"; | ||||
|     time_t       rawtime[2]; | ||||
|     struct tm   *timeinfo[2]; | ||||
|     struct stat  st[2]; | ||||
|      | ||||
|     if (stat(fp1.c_str(), &st[0]) == -1) { | ||||
|         cerr << "argv1 is invalid." << endl; | ||||
|         exit(-1); | ||||
|     } | ||||
|     if (stat(fp2.c_str(), &st[1]) == -1) { | ||||
|         cerr << "argv2 is invalid" << endl; | ||||
|         exit(-1); | ||||
|     } | ||||
|      | ||||
|     char buf[2][MAX_LENGTH + 1]; | ||||
|     rawtime[0] = st[0].st_mtime; | ||||
|     timeinfo[0] = localtime(&rawtime[0]); | ||||
|     strftime(buf[0], MAX_LENGTH, time_format, timeinfo[0]); | ||||
|     cout << "--- " << fp1 << '\t' << buf[0] << endl; | ||||
|     rawtime[1] = st[1].st_mtime; | ||||
|     timeinfo[1] = localtime(&rawtime[1]); | ||||
|     strftime(buf[1], MAX_LENGTH, time_format, timeinfo[1]); | ||||
|     cout << "+++ " << fp2 << '\t' << buf[1] << endl; | ||||
| } | ||||
|  | ||||
| static void unifiedDiff (string fp1, string fp2)  | ||||
| { | ||||
|     typedef string                 elem; | ||||
|     typedef vector< elem >         sequence; | ||||
|     typedef pair< elem, elemInfo > sesElem; | ||||
|  | ||||
|     ifstream      Aifs(fp1.c_str()); | ||||
|     ifstream      Bifs(fp2.c_str()); | ||||
|     elem          buf; | ||||
|     sequence      ALines, BLines; | ||||
|      | ||||
|     while(getline(Aifs, buf)){ | ||||
|         ALines.push_back(buf); | ||||
|     } | ||||
|     while(getline(Bifs, buf)){ | ||||
|         BLines.push_back(buf); | ||||
|     } | ||||
|      | ||||
|     Diff< elem > diff(ALines, BLines); | ||||
|     diff.onHuge(); | ||||
|     //diff.onUnserious(); | ||||
|     diff.compose(); | ||||
|      | ||||
|     // type unihunk definition test | ||||
|     uniHunk< sesElem > hunk; | ||||
|      | ||||
|     if (diff.getEditDistance() > 0) { | ||||
|         showStats(fp1, fp2);             // show file info | ||||
|     } | ||||
|      | ||||
|     diff.composeUnifiedHunks(); | ||||
|     diff.printUnifiedFormat(); | ||||
| } | ||||
|  | ||||
|  | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     string s1(argv[1]); | ||||
|     string s2(argv[2]); | ||||
|     bool   fileExist = true; | ||||
|      | ||||
|     if (!isFileExist(s1)) { | ||||
|         cerr << s1 << " is invalid." << endl; | ||||
|         fileExist = false; | ||||
|     } | ||||
|      | ||||
|     if (!isFileExist(s2)) { | ||||
|         cerr << s2 << " is invalid." << endl; | ||||
|         fileExist = false; | ||||
|     } | ||||
|      | ||||
|     if (!fileExist) { | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     unifiedDiff(s1, s2); | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										40
									
								
								examples/unistrdiff.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								examples/unistrdiff.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
|  | ||||
| #include <dtl/dtl.hpp> | ||||
| #include "common.hpp" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
|  | ||||
| using namespace std; | ||||
|  | ||||
| using dtl::Diff; | ||||
|  | ||||
| int main(int argc, char *argv[]){ | ||||
|      | ||||
|     if (isFewArgs(argc)) { | ||||
|         cerr << "Too few arguments." << endl; | ||||
|         return -1; | ||||
|     } | ||||
|      | ||||
|     typedef char elem; | ||||
|     typedef string sequence; | ||||
|  | ||||
|     sequence A(argv[1]); | ||||
|     sequence B(argv[2]); | ||||
|      | ||||
|     Diff<elem, sequence > d(A, B); | ||||
|     d.compose(); | ||||
|     d.composeUnifiedHunks(); | ||||
|      | ||||
|     // editDistance | ||||
|     cout << "editDistance:" << d.getEditDistance() << endl; | ||||
|      | ||||
|     // Longest Common Subsequence | ||||
|     vector<elem> lcs_v = d.getLcsVec(); | ||||
|     sequence     lcs_s(lcs_v.begin(), lcs_v.end()); | ||||
|     cout << "LCS:" << lcs_s << endl; | ||||
|      | ||||
|     // print Unified Format | ||||
|     d.printUnifiedFormat(); | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user