Skip to content

Commit d7a1acd

Browse files
committed
Code update
* Activate File reading, writing and download options * Precompile the regex to optimize resource usage * Remove filemanagement legacy code * Fix fileread to read from the correct path * declared editFile() in filewrite as static
1 parent 2d61847 commit d7a1acd

File tree

5 files changed

+33
-337
lines changed

5 files changed

+33
-337
lines changed

Source/Cataphract/API/Anvil.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package Cataphract.API;
1717

18+
import java.util.regex.Pattern;
19+
1820
import Cataphract.API.Astaroth.Calendar;
1921
import Cataphract.API.Astaroth.Time;
2022

@@ -27,6 +29,11 @@
2729
*/
2830
public class Anvil
2931
{
32+
/**
33+
* Precompile the regex to split string to array, saves resources by precompilation
34+
*/
35+
private static final Pattern SPLIT_PATTERN = Pattern.compile(" (?=([^\"]*\"[^\"]*\")*[^\"]*$)");
36+
3037
/**
3138
* Sole constructor. (For invocation by subclass constructors, typically implicit.)
3239
*/
@@ -147,13 +154,13 @@ public static void anvilInterpreter(String commandArray[])throws Exception
147154
public static String[] splitStringToArray(String command)
148155
{
149156
//Regex to split the string at the occurrence of a blank space
150-
String[] arr = command.split(" (?=([^\"]*\"[^\"]*\")*[^\"]*$)");
157+
String[] arr = SPLIT_PATTERN.split(command);
151158

152159
//Fix to remove the quotes, make the logic to split the input at every space only.
153160
//Check the EasyGuide Documentation on why this is implemented the way it is.
154161
for(int i = 0; i < arr.length; i++)
155162
if(arr[i].startsWith("\"") && arr[i].endsWith("\""))
156-
arr[i] = arr[i].substring(1, arr[i].length()-1);
163+
arr[i] = arr[i].substring(1, arr[i].length()-1);
157164

158165
//return the array of words split.
159166
return arr;

Source/Cataphract/API/Wraith/FileManagement.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private final void viewDirTreeHelper(int indent, File file) {
104104

105105
private final void navPreviousDirectory()throws Exception
106106
{
107-
_presentWorkingDirectory = _presentWorkingDirectory.substring(0, _presentWorkingDirectory.length() - 1);
107+
_presentWorkingDirectory = _presentWorkingDirectory.substring(0, _presentWorkingDirectory.length() - 1);
108108
_presentWorkingDirectory = _presentWorkingDirectory.replace(_presentWorkingDirectory.substring(_presentWorkingDirectory.lastIndexOf('/'), _presentWorkingDirectory.length()), "/");
109109
if (_presentWorkingDirectory.equals("./Users/Cataphract/"))
110110
{
@@ -156,7 +156,7 @@ private final void copyMoveHelper(File source, File destination, boolean move)th
156156
else
157157
{
158158
Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
159-
if (move)
159+
if (move)
160160
{
161161
Files.delete(source.toPath());
162162
}
@@ -218,7 +218,7 @@ public void fileManagementLogic()throws Exception
218218
String inputValue = "";
219219
do
220220
{
221-
inputValue = console.readLine(_name + "@" + _presentWorkingDirectory.replace(_username, _name) + "> ");
221+
inputValue = console.readLine(_name + "@" + _presentWorkingDirectory.replace(_username, _name) + "> ");
222222
grinchInterpreter(inputValue);
223223
}
224224
while(!inputValue.equalsIgnoreCase("exit"));
@@ -274,6 +274,17 @@ private void grinchInterpreter(String command)throws Exception
274274
break;
275275

276276
case "edit":
277+
if(commandArray.length < 2)
278+
IOStreams.printError("Invalid Syntax.");
279+
else
280+
FileWrite.editFile(commandArray[1], _presentWorkingDirectory);
281+
break;
282+
283+
case "read":
284+
if(commandArray.length < 2)
285+
IOStreams.printError("Invalid Syntax.");
286+
else
287+
new FileRead().readUserFile(_presentWorkingDirectory + commandArray[1]);
277288
break;
278289

279290
case "pwd":
@@ -301,10 +312,14 @@ private void grinchInterpreter(String command)throws Exception
301312
break;
302313

303314
case "download":
315+
if(commandArray.length < 3)
316+
IOStreams.printError("Invalid Syntax.");
317+
else
318+
new FileDownload(_username).downloadFile(commandArray[1], commandArray[2]);
304319
break;
305320

306321
case "home":
307-
resetToHomeDirectory();
322+
resetToHomeDirectory();
308323
break;
309324

310325
case "exit":
@@ -315,5 +330,4 @@ private void grinchInterpreter(String command)throws Exception
315330
Anvil.anvilInterpreter(commandArray);
316331
}
317332
}
318-
319333
}

0 commit comments

Comments
 (0)