-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathunidiff.cpp
111 lines (89 loc) · 2.51 KB
/
unidiff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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;
}