This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
140 lines (126 loc) · 4.98 KB
/
Main.java
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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import Buffer.PageBuffer;
import Catalog.Catalog;
import StorageManager.StorageManager;
import IO.BinaryWriter;
import InputHandler.InputHandler;
import IO.BinaryReader;
public class Main {
public static void main(String[] args) throws IOException {
if (!(args.length == 3 || args.length == 4)) {
System.out.println("Expected 3 or 4 arguments, got " + args.length);
return;
}
if (args.length == 4 && !args[3].equals("indexing")) {
System.out.println("Expected argument 4 as 'indexing', got " + args[3]);
return;
}
if (!args[0].substring(args[0].length() - 1).equals("/")) {
System.out.println("First arg must be a full path");
return;
}
// read program parameters
String path = null;
int pageSize = 0;
int bufferSize = 0;
boolean indexing = false;
try {
path = args[0];
pageSize = Integer.parseInt(args[1]);
bufferSize = Integer.parseInt(args[2]);
if (args.length == 4) {
indexing = true;
}
} catch (Exception _e) {
System.out.println("Unexpected program parameters. See usage");
System.exit(0);
}
// remove debug items
BinaryReader reader = new BinaryReader();
BinaryWriter writer = new BinaryWriter();
Catalog catalog;
File file = new File(path);
if (!(file.exists() && file.isDirectory())) {
file.mkdirs();
if (!new File(path + "catalog.txt").exists()) {
System.out.println("No existing db found.");
System.out.println("Creating new db at " + path);
writer.createCatalog(path, pageSize);
}
} else {
System.out.println("DB already exist, using predefined page size.");
}
catalog = reader.getCatalog(path, pageSize, bufferSize, indexing);
System.out.println("Page size: " + catalog.getPageSize());
writer.setCatalog(catalog);
PageBuffer pageBuffer = new PageBuffer(bufferSize, reader, writer, catalog);
StorageManager storageManager = new StorageManager(pageBuffer, catalog);
InputHandler inputHandler = new InputHandler(writer, storageManager);
Scanner scan = new Scanner(System.in);
boolean programRunning = true;
while (programRunning) {
StringBuilder input = new StringBuilder();
System.out.println("Enter command: ");
while (true) {
String line = scan.nextLine();
input.append(line + " ");
// check to see if there is a semicolon in the input
int semicolonIndex = findSemicolonOutsideQuotes(input.toString());
if (semicolonIndex != -1) {
// if there is a semicolon, split the input at that point and handle each command separately
String[] commands = input.substring(0, semicolonIndex).trim().split(";");
for (String command : commands) {
String commandWithSemi = command + ";";
String finalInput = removeExtraWhitespace(commandWithSemi);
programRunning = inputHandler.handleInput(finalInput);
if (!programRunning) {
break;
}
}
break;
}
}
// String finalInput = removeExtraWhitespace(input.toString());
// programRunning = inputHandler.handleInput(finalInput);
}
scan.close();
}
// helper method to find the index of the first semicolon that appears outside
// of a quoted string
private static int findSemicolonOutsideQuotes(String input) {
boolean inQuotes = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '"') {
inQuotes = !inQuotes;
} else if (!inQuotes && c == ';') {
return i;
}
}
return -1;
}
private static String removeExtraWhitespace(String input) {
StringBuilder output = new StringBuilder();
input = input.trim();
boolean insideQuotes = false;
boolean prevCharWasSpace = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '\"') {
insideQuotes = !insideQuotes;
output.append(c);
} else if (!insideQuotes) {
if (prevCharWasSpace && c == ' ') {
continue;
}
prevCharWasSpace = c == ' ';
output.append(Character.toLowerCase(c));
} else {
output.append(c);
}
}
return output.toString();
}
}