-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProgram.cs
More file actions
228 lines (198 loc) · 5.58 KB
/
Program.cs
File metadata and controls
228 lines (198 loc) · 5.58 KB
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
using System.Text;
using Waher.Content;
using Waher.Runtime.Console;
using Waher.Runtime.IO;
using Waher.Runtime.Text;
namespace Waher.Utility.TextDiff
{
class Program
{
/// <summary>
/// Compares two text files and outputs the differences between them.
///
/// Command line switches:
///
/// -f FROM_FILE File name of XML file.
/// -t TO_FILE XSLT transform to use.
/// -o OUTPUT_FILE File name of output file.
/// If no output file is provided, output will be
/// made to the console.
/// -enc ENCODING Text encoding. Default=UTF-8
/// -? Help.
/// </summary>
static int Main(string[] args)
{
try
{
Encoding Encoding = Encoding.UTF8;
string? FromFileName = null;
string? ToFileName = null;
string? OutputFileName = null;
string s;
int i = 0;
int c = args.Length;
bool Help = false;
while (i < c)
{
s = args[i++].ToLower();
switch (s)
{
case "-o":
if (i >= c)
throw new Exception("Missing output file name.");
if (string.IsNullOrEmpty(OutputFileName))
OutputFileName = args[i++];
else
throw new Exception("Only one output file name allowed.");
break;
case "-f":
if (i >= c)
throw new Exception("Missing from file name.");
if (string.IsNullOrEmpty(FromFileName))
FromFileName = args[i++];
else
throw new Exception("Only one from file name allowed.");
break;
case "-t":
if (i >= c)
throw new Exception("Missing to file name.");
if (string.IsNullOrEmpty(ToFileName))
ToFileName = args[i++];
else
throw new Exception("Only one to file name allowed.");
break;
case "-enc":
if (i >= c)
throw new Exception("Text encoding missing.");
Encoding = Encoding.GetEncoding(args[i++]);
break;
case "-?":
Help = true;
break;
default:
throw new Exception("Unrecognized switch: " + s);
}
}
if (Help || c == 0)
{
ConsoleOut.WriteLine("Compares two text files and outputs the differences between them.");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Command line switches:");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("-f FROM_FILE File name of XML file.");
ConsoleOut.WriteLine("-t TO_FILE XSLT transform to use.");
ConsoleOut.WriteLine("-o OUTPUT_FILE File name of output file.");
ConsoleOut.WriteLine(" If no output file is provided, output will be");
ConsoleOut.WriteLine(" made to the console.");
ConsoleOut.WriteLine("-enc ENCODING Text encoding. Default=UTF-8");
ConsoleOut.WriteLine("-? Help.");
return 0;
}
if (string.IsNullOrEmpty(FromFileName))
throw new Exception("No from filename specified.");
string From = ReadTextFile(FromFileName, Encoding);
string[] FromRows = Difference.ExtractRows(From);
if (string.IsNullOrEmpty(ToFileName))
throw new Exception("No to filename specified.");
string To = ReadTextFile(ToFileName, Encoding);
string[] ToRows = Difference.ExtractRows(To);
EditScript<string> Script = Difference.Analyze(FromRows, ToRows);
TextWriter Output;
bool DisposeOutput;
if (string.IsNullOrEmpty(OutputFileName))
{
Output = ConsoleOut.Writer;
DisposeOutput = false;
}
else
{
Output = new StreamWriter(OutputFileName, Encoding, new FileStreamOptions()
{
Access = FileAccess.Write,
Mode = FileMode.Create
});
DisposeOutput = true;
}
try
{
int NrAdded = 0;
int NrRemoved = 0;
foreach (Step<string> Step in Script)
{
switch (Step.Operation)
{
case EditOperation.Delete:
NrRemoved += Step.Symbols.Length;
break;
case EditOperation.Insert:
NrAdded += Step.Symbols.Length;
break;
}
}
if (NrAdded == 0 && NrRemoved == 0)
Output.WriteLine("Files are identical.");
else
{
if (NrAdded == 1)
Output.WriteLine("1 row added.");
else if (NrAdded > 1)
Output.WriteLine(NrAdded.ToString() + " rows added.");
if (NrRemoved == 1)
Output.WriteLine("1 row removed.");
else if (NrRemoved > 1)
Output.WriteLine(NrRemoved.ToString() + " rows removed.");
Output.WriteLine();
foreach (Step<string> Step in Script)
{
int i1 = Step.Index1;
int i2 = Step.Index2;
switch (Step.Operation)
{
case EditOperation.Delete:
foreach (string Row in Step.Symbols)
{
Output.Write((i1++).ToString());
Output.Write('/');
Output.Write(i2.ToString());
Output.Write("\t-\t");
Output.WriteLine(Row);
}
break;
case EditOperation.Insert:
foreach (string Row in Step.Symbols)
{
Output.Write(i1.ToString());
Output.Write('/');
Output.Write((i2++).ToString());
Output.Write("\t+\t");
Output.WriteLine(Row);
}
break;
}
}
}
}
finally
{
if (DisposeOutput)
Output.Dispose();
}
return 0;
}
catch (Exception ex)
{
ConsoleOut.WriteLine(ex.Message);
return -1;
}
finally
{
ConsoleOut.Flush(true);
}
}
private static string ReadTextFile(string FileName, Encoding DefaultEncoding)
{
byte[] Data = File.ReadAllBytes(FileName);
return Strings.GetString(Data, DefaultEncoding);
}
}
}