-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (101 loc) · 3.45 KB
/
Main.java
File metadata and controls
118 lines (101 loc) · 3.45 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
package diffr.patch;
import com.google.common.base.Optional;
import com.google.common.io.Files;
import diffr.util.ArgumentsProcessor;
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 PATCH tool.
* <p/>
* <p>
* Expects two arguments:
* <ul>
* <li><original-file> - The original file.</li>
* <li><diff-file> - The diff file created using the diff tool.</li>
* </ul>
* </p>
*
* @author Jakub D Kozlowski
* @author Amaury Couste
* @author William Martin
* @since 0.1
*/
public final class Main {
/**
* Prints the usage of this tool.
*/
private static void printUsage() {
System.out.println("Usage: \n" +
" patchr <original-file> <patch-file>\n" +
" patchr <original-file> <patch-file> -o <output-file>");
}
/**
* Runs the patch tool on the original file.
*
* @param args arguments to this tool.
*
* @return exit code.
*/
public static int run(final 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 patchFile = new File(args[1]);
if (!firstFile.exists()) {
System.err.println("File " + firstFile + " not found.");
return -1;
}
if (!patchFile.exists()) {
System.err.println("File " + patchFile + " not found.");
return -1;
}
final List<String> firstFileStrings = Files.readLines(firstFile, Charset.defaultCharset());
final List<String> patchFileStrings = Files.readLines(patchFile, Charset.defaultCharset());
final Patchr patchr = new Patchr(firstFileStrings, patchFileStrings);
final List<String> newFileStrings = patchr.patch();
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 String line : newFileStrings) {
bufferedWriter.write(line);
bufferedWriter.write("\n");
}
bufferedWriter.close();
}
else {
for (final String line : newFileStrings) {
System.out.println(line);
}
System.out.flush();
}
return 0;
}
catch (final IOException io) {
System.err.println("There was a problem reading the files: " + io);
return -1;
}
catch (final IllegalPatchFileException ipfe) {
System.err.println("The patch file is incorrect, exiting.");
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));
}
}