Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
112 lines (97 loc) · 3.37 KB

File metadata and controls

112 lines (97 loc) · 3.37 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package diffr.diff;
import com.google.common.base.Optional;
import com.google.common.io.Files;
import diffr.util.ArgumentsProcessor;
import diffr.util.instruction.Instruction;
import diffr.util.instruction.InstructionComposer;
import diffr.util.instruction.Instructions;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* Main entry point to diffr's DIFF tool.
* <p/>
* <p>
* Expects two arguments:
* <ul>
* <li>&lt;original-file&gt; - The original file to diff.</li>
* <li>&lt;new-file&gt; - The new version of the original file to diff.</li>
* </ul>
* </p>
*
* @author Jakub D Kozlowski
* @author Sarina Gurung
* @since 0.1
*/
public final class Main {
/**
* Prints the usage of this tool.
*/
private static void printUsage() {
System.out.println("Usage: \n" +
" diffr <original-file> <new-file>\n" +
" diffr <original-file> <new-file> -o <output-file>");
}
/**
* Runs the diff tool on two files.
*
* @param args arguments to this tool.
*
* @return exit code.
*/
public static int run(String... args) {
try {
if (ArgumentsProcessor.containsHelpArgument(args)
|| (2 != args.length
&& 4 != args.length)) {
printUsage();
return -1;
}
final File firstFile = new File(args[0]);
final File secondFile = new File(args[1]);
if (!firstFile.exists()) {
System.err.println("File " + firstFile + " not found.");
return -1;
}
if (!secondFile.exists()) {
System.err.println("File " + secondFile + " not found.");
return -1;
}
final List<String> originalFile = Files.readLines(firstFile, Charset.defaultCharset());
final List<String> newFile = Files.readLines(secondFile, Charset.defaultCharset());
final List<Instruction> instructions = new Diffr(originalFile, newFile).diff();
final Optional<String> outputFile = ArgumentsProcessor.extractOutputFile(args);
if (4 == args.length
&& outputFile.isPresent()) {
final File file = new File(outputFile.get());
final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
for (final Instruction instruction : instructions) {
Instructions.writeInstruction(instruction, bufferedWriter);
}
bufferedWriter.close();
}
else {
for (final Instruction instruction : instructions) {
System.out.println(InstructionComposer.composeString(instruction));
}
System.out.flush();
}
return 0;
}
catch (final IOException io) {
System.err.println("There was a problem reading the files: " + io);
return -1;
}
}
/**
* Invokes {@link #run(String...)} and calls {@link System#exit(int)}.
*
* @param args arguments to this tool.
*/
public static void main(String... args) {
System.exit(run(args));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.