-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFileParser.java
More file actions
95 lines (83 loc) · 2.73 KB
/
InputFileParser.java
File metadata and controls
95 lines (83 loc) · 2.73 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
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/**
* Contains functionality for reading and interpreting input files
*/
public class InputFileParser {
private static final boolean INPUT_DEBUG = false;
/**
* Parse an input file into a list of processes contained therein
* @param fileName The file to parse
* @return A list of processes contained therein
*/
public static ArrayList<Process> parseInputFile(String fileName) {
ArrayList<Process> processList = new ArrayList<Process>();
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine(); //First line is number of processes... throw it out
while ( (line = br.readLine()) != null) {
ArrayList<Process> processesInLine = parseInputLine(line);
for (Process p : processesInLine) {
processList.add( p );
}
}
} catch (IOException ioe) {
System.err.println("Couldn't read input file '" + fileName + "'");
System.exit(1);
}
Collections.sort(processList);
return processList;
}
/**
* Parse an input line into a sublist of Processes
* @param line The line to parse
* @return A list of Process objects that can be parsed from the line
*/
private static ArrayList<Process> parseInputLine(String line) {
ArrayList<Process> processesToReturn = new ArrayList<Process>();
debugPrint("Got line " + line);
String[] lineParts = line.split("\\s");
debugPrintln(" (" + lineParts.length + ")");
//Need at least 4 items: pid, mem, and an entry/exit pair
//Also, must be an even number of items (pairs of times only)
if (lineParts.length < 4 || lineParts.length % 2 != 0) {
System.err.println("Malformed input file with line '" + line + "'");
System.exit(1);
}
char pid = lineParts[0].charAt(0);
int memSize = Integer.parseInt(lineParts[1]);
for (int i = 2; i < lineParts.length; i += 2) {
int start = Integer.parseInt( lineParts[i] );
int end = Integer.parseInt( lineParts[i+1] );
//Also, times must be strictly increasing
if ( start > end ) {
//Time wasn't strictly increasing
System.err.println("Times not strictly increasing on line '" + line + "'");
System.exit(1);
}
processesToReturn.add( new Process(pid, memSize, start, end) );
}
return processesToReturn;
}
/**
* Print a string if a debug flag is set.
* Do not include a newline.
* @param toPrint The string to print
*/
private static void debugPrint(String toPrint) {
if (INPUT_DEBUG == true) {
System.out.print(toPrint);
}
}
/**
* Print a string if a debug flag is set.
* Include a newline.
* @param toPrint The string to print
*/
private static void debugPrintln(String toPrint) {
if (INPUT_DEBUG == true) {
System.out.println(toPrint);
}
}
}