/
diff.cpp
269 lines (223 loc) · 6.64 KB
/
diff.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2002
//
// CIYAM Pty. Ltd.
// ACN 093 704 539
//
// ALL RIGHTS RESERVED
//
// Permission to use this software for non-commercial purposes is hereby granted. Permission to
// distribute this software privately is granted provided that the source code is unaltered and
// complete or that any alterations and omissions have been first approved by CIYAM. Commercial
// usage of this software is not permitted without first obtaining a license for such a purpose
// from CIYAM. This software may not be publicly distributed unless written permission to do so
// has been obtained from CIYAM.
#ifdef __BORLANDC__
# include "precompile.h"
#endif
#pragma hdrstop
#ifndef HAS_PRECOMPILED_STD_HEADERS
# include <stdio.h>
# ifdef _WIN32
# include <io.h>
# else
# include <unistd.h>
# endif
# include <memory>
# include <iostream>
# include <stdexcept>
#endif
#ifdef __GNUG__
# define _access access
#endif
//#define DEBUG
#include "diff.h"
#include "text_file_buffer.h"
using namespace std;
const char* const c_quiet_opt = "-q";
const char* const c_min_opt = "-min";
const char* const c_rcs_opt = "-rcs";
const int c_path_items_initial_reserve = 1000;
string append_first_if_second_is_path( const string& first, const string& second )
{
string retval( second );
bool second_is_path = false;
if( !second.empty( ) )
{
string::size_type lpos = second.size( ) - 1;
#ifndef _WIN32
if( second[ lpos ] == '/' )
second_is_path = true;
#else
if( second[ lpos ] == '/' || second[ lpos ] == '\\' || second[ lpos ] == ':' )
second_is_path = true;
#endif
if( second_is_path || _access( second.c_str( ), 0 ) == 0 )
{
string tmp( retval );
#ifndef _WIN32
if( !second_is_path )
tmp += '/';
string::size_type fpos = first.find_last_of( "/" );
#else
if( !second_is_path )
tmp += '\\';
string::size_type fpos = first.find_last_of( ":/\\" );
#endif
if( fpos != string::npos )
++fpos;
else
fpos = 0;
tmp += first.substr( fpos );
if( second_is_path || _access( tmp.c_str( ), 0 ) == 0 )
retval = tmp;
}
}
return retval;
}
void output_context( int x, int y, int ox, int oy, bool rcs_format )
{
if( x != ox || y != oy )
{
if( rcs_format )
{
if( x != ox )
cout << 'd' << ( ox + 1 ) << ' ' << ( x - ox ) << '\n';
if( y != oy )
cout << 'a' << x << ' ' << ( y - oy ) << '\n';
}
else
{
if( x == ox || x == ox + 1 )
cout << x;
else
cout << ( ox + 1 ) << ',' << x;
if( x == ox )
cout << 'a';
else if( y == oy )
cout << 'd';
else
cout << 'c';
if( y == oy || y == oy + 1 )
cout << y;
else
cout << ( oy + 1 ) << ',' << y;
cout << '\n';
}
}
}
int main( int argc, char* argv[ ] )
{
int first_arg = 1;
if( argc < 3 || argc > 6
|| ( argc == 6 && argv[ 1 ] != string( c_quiet_opt )
&& argv[ 2 ] != string( c_min_opt ) && argv[ 3 ] != string( c_rcs_opt ) )
|| ( argc == 5 && ( argv[ 1 ] != string( c_quiet_opt ) && argv[ 2 ] != string( c_min_opt ) )
&& ( argv[ 1 ] != string( c_quiet_opt ) && argv[ 2 ] != string( c_rcs_opt ) )
&& ( argv[ 1 ] != string( c_min_opt ) && argv[ 2 ] != string( c_rcs_opt ) ) )
|| ( argc == 4 && ( argv[ 1 ] != string( c_quiet_opt )
&& argv[ 1 ] != string( c_min_opt ) && argv[ 1 ] != string( c_rcs_opt ) ) ) )
{
cout << "DIFF v0.1f (c) 2002 CIYAM Pty. Ltd.\n";
cout << "Usage: diff [" << c_quiet_opt
<< "] [" << c_min_opt << "] [" << c_rcs_opt << "] <file1> <file2 or path>" << endl;
return 1;
}
bool quiet = false;
if( argc > 2 + first_arg && argv[ first_arg ] == string( c_quiet_opt ) )
{
++first_arg;
quiet = true;
}
bool minimal = false;
if( argc > 2 + first_arg && argv[ first_arg ] == string( c_min_opt ) )
{
++first_arg;
minimal = true;
}
bool rcs_format = false;
if( argc > 2 + first_arg && argv[ first_arg ] == string( c_rcs_opt ) )
{
++first_arg;
rcs_format = true;
}
try
{
int ed;
string file1( argv[ first_arg ] );
string file2( append_first_if_second_is_path( argv[ first_arg ], argv[ first_arg + 1 ] ) );
text_file_buffer a( file1 );
text_file_buffer b( file2 );
if( !quiet )
cout << "Files: " << file1 << "\t\t" << file2 << endl;
diff< text_file_buffer > diffs( a, b, true, minimal, a.size( ), b.size( ), c_path_items_initial_reserve );
ed = diffs.get_edit_distance( );
#ifdef DEBUG
cout << "(edit distance = " << ed << ")\n" << endl;
#endif
int len = 0;
int num_matches;
pair< int, int > match_point;
int last_x = 0;
int last_y = 0;
while( diffs.get_next_match_chunk( match_point, num_matches ) )
{
len += num_matches;
int x = match_point.first;
int y = match_point.second;
output_context( x, y, last_x, last_y, rcs_format );
if( rcs_format )
{
last_x = x;
while( last_y < y )
cout << b[ last_y++ ] << '\n';
}
else
{
bool output_divider = false;
if( last_x < x && last_y < y )
output_divider = true;
while( last_x < x )
cout << "< " << a[ last_x++ ] << '\n';
if( output_divider )
cout << "---\n";
while( last_y < y )
cout << "> " << b[ last_y++ ] << '\n';
}
last_x += num_matches;
last_y += num_matches;
}
int x = a.size( );
int y = b.size( );
output_context( x, y, last_x, last_y, rcs_format );
if( rcs_format )
{
while( last_y < y )
cout << b[ last_y++ ] << '\n';
}
else
{
bool output_divider = false;
if( last_x < x && last_y < y )
output_divider = true;
while( last_x < x )
cout << "< " << a[ last_x++ ] << '\n';
if( output_divider )
cout << "---\n";
while( last_y < y )
cout << "> " << b[ last_y++ ] << '\n';
}
if( minimal )
{
int lcs = ( a.size( ) + b.size( ) - ed ) / 2;
if( lcs != len )
cerr << "\n*** lcs was not found (path length is "
<< len << " but if is lcs should be " << lcs << ") ***\n";
}
}
catch( exception& e )
{
cerr << "error: " << e.what( ) << endl;
return 1;
}
return 0;
}