Skip to content

Commit 27a5d62

Browse files
committed
Code Update
* Move the file path conversion logics into IOStreams * Fix paths issue in APIs (Part 1) * Consolidate the debug related options to be under debug mode in Loader * Add documentation (Part 1)
1 parent b6615b0 commit 27a5d62

File tree

9 files changed

+536
-285
lines changed

9 files changed

+536
-285
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"command": "make",
88
"args": [
99
"project=Cataphract",
10-
"kernel"
10+
"all"
1111
],
1212
"group": {
1313
"kind": "build",

Source/Cataphract/API/IOStreams.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package Cataphract.API;
1717

1818
import java.io.Console;
19+
import java.io.File;
20+
import java.util.regex.Matcher;
1921

2022
/**
2123
* A class implementing console output with prefixes, colors, and a "Press RETURN To Continue" functionality
@@ -175,4 +177,26 @@ public static String confirmReturnToContinue(String prefix, String suffix)
175177
{
176178
return console.readLine(prefix + "Press RETURN to Continue" + suffix);
177179
}
180+
181+
/**
182+
* Logic to convert from Nion File Separator format of file paths to an OS dependent file separator format.
183+
*
184+
* @param nionPath String that contains the file path in Nion File Separator format
185+
* @return String The value of the String converted to the OS dependent file separator format
186+
*/
187+
public static String convertFileSeparator(String nionPath)
188+
{
189+
return nionPath.replaceAll("\\|", Matcher.quoteReplacement(File.separator));
190+
}
191+
192+
/**
193+
* Logic to convert from an OS dependent file separator format of file paths to Nion File Separator format.
194+
*
195+
* @param filePath String that contains the file path in OS dependent file separator format.
196+
* @return String The value of the String converted to Nion File Separator format.
197+
*/
198+
public static String convertToNionSeparator(String filePath)
199+
{
200+
return filePath.replaceAll(Matcher.quoteReplacement(File.separator), "|");
201+
}
178202
}

Source/Cataphract/API/Wraith/FileManagement.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public FileManagement(String username)throws Exception
6161
// Set the name of the user to the global variable
6262
_name = new Login(username).getNameLogic();
6363
// Initialize the present working directory
64-
_presentWorkingDirectory = "./Users/Cataphract/" + _username + "/";
64+
_presentWorkingDirectory = ".|Users|Cataphract|" + _username + "|";
6565
}
6666

6767
/*****************************************
@@ -93,7 +93,7 @@ private final boolean login()throws Exception
9393
*/
9494
private boolean checkEntityExistence(String fileName)throws Exception
9595
{
96-
return new File(fileName).exists();
96+
return new File(IOStreams.convertFileSeparator(fileName)).exists();
9797
}
9898

9999
/**
@@ -106,7 +106,7 @@ private final void deleteEntity(String fileName)throws Exception
106106
{
107107
try
108108
{
109-
fileName = _presentWorkingDirectory+fileName;
109+
fileName = IOStreams.convertFileSeparator(_presentWorkingDirectory+fileName);
110110
if(checkEntityExistence(fileName))
111111
{
112112
File f=new File(fileName);
@@ -150,7 +150,7 @@ private final void deleteEntityHelper(File fileName)throws Exception
150150
private void viewDirectoryTree()throws Exception
151151
{
152152
// Create a File object for the present working directory
153-
File treeView = new File(_presentWorkingDirectory);
153+
File treeView = new File(IOStreams.convertFileSeparator(_presentWorkingDirectory));
154154

155155
// Print the header for the tree view
156156
IOStreams.println("\n--- [ TREE VIEW ] ---\n");
@@ -202,10 +202,11 @@ private final void navPreviousDirectory()throws Exception
202202

203203
// Replace the last directory in the path with a single slash
204204
_presentWorkingDirectory = _presentWorkingDirectory.replace(
205-
_presentWorkingDirectory.substring(_presentWorkingDirectory.lastIndexOf('/'), _presentWorkingDirectory.length()),"/");
205+
_presentWorkingDirectory.substring(_presentWorkingDirectory.lastIndexOf('|'), _presentWorkingDirectory.length()),"|");
206206

207207
// Check if the present working directory is the restricted user home directory
208-
if (_presentWorkingDirectory.equals("./Users/Cataphract/")) {
208+
if (_presentWorkingDirectory.equals(IOStreams.convertFileSeparator(".|Users|Cataphract|")))
209+
{
209210
// Print an error message if access is denied
210211
IOStreams.printError("Permission Denied.");
211212

@@ -219,7 +220,7 @@ private final void navPreviousDirectory()throws Exception
219220
*/
220221
private final void resetToHomeDirectory()
221222
{
222-
_presentWorkingDirectory = "./Users/Cataphract/" + _username + '/' ;
223+
_presentWorkingDirectory = ".|Users|Cataphract|" + _username + '|' ;
223224
}
224225

225226
/**
@@ -265,6 +266,10 @@ private final void renameEntity(String fileName, String newFileName) throws Exce
265266
*/
266267
private final void copyMoveEntity(String fileName, String destination, boolean move)throws Exception
267268
{
269+
// Convert paths from Nion paths to OS specific paths
270+
fileName = IOStreams.convertFileSeparator(fileName);
271+
destination = IOStreams.convertFileSeparator(destination);
272+
268273
// Check if the specified file or directory is valid
269274
if(!checkEntityExistence(fileName) && !checkEntityExistence(destination))
270275
IOStreams.printError("Invalid file name or destination.");
@@ -284,7 +289,8 @@ private final void copyMoveEntity(String fileName, String destination, boolean m
284289
private final void copyMoveHelper(File source, File destination, boolean move)throws Exception
285290
{
286291
// Check if the source is a directory
287-
if (source.isDirectory()) {
292+
if (source.isDirectory())
293+
{
288294
// Create the destination directory
289295
destination.mkdirs();
290296

@@ -319,11 +325,13 @@ private final void listEntities()throws Exception
319325
String format = "%1$-32s| %2$-24s| %3$-10s\n";
320326
String c = "-";
321327

328+
String ls = IOStreams.convertFileSeparator(_presentWorkingDirectory);
329+
322330
// Check if the present working directory exists
323-
if (checkEntityExistence(_presentWorkingDirectory))
331+
if (checkEntityExistence(ls))
324332
{
325333
// Create a File object for the present working directory
326-
File dPath = new File(_presentWorkingDirectory);
334+
File dPath = new File(ls);
327335

328336
// Print a newline for better formatting
329337
System.out.println("\n");
@@ -358,7 +366,8 @@ private final void listEntities()throws Exception
358366
private final void changeDirectory(String destination)throws Exception
359367
{
360368
// Check if the destination is the parent directory
361-
if (destination.equals("..")) {
369+
if (destination.equals(".."))
370+
{
362371
// Navigate to the previous directory
363372
navPreviousDirectory();
364373

@@ -369,7 +378,7 @@ private final void changeDirectory(String destination)throws Exception
369378
if (checkEntityExistence(_presentWorkingDirectory + destination))
370379
{
371380
// Update the present working directory to the new destination
372-
_presentWorkingDirectory = _presentWorkingDirectory + destination + "/";
381+
_presentWorkingDirectory = _presentWorkingDirectory + destination + "|";
373382
}
374383
else
375384
{
@@ -396,12 +405,13 @@ public void fileManagementLogic()throws Exception
396405
// Check if the user is logged in
397406
if (login())
398407
{
408+
System.out.println(IOStreams.convertToNionSeparator(IOStreams.convertFileSeparator(_presentWorkingDirectory)));
399409
String inputValue = "";
400410
// Loop to continuously read and execute commands until 'exit' is entered
401411
do
402412
{
403413
// Read a line of input from the console
404-
inputValue = console.readLine(_name + "@" + _presentWorkingDirectory.replace(_username, _name) + "> ");
414+
inputValue = console.readLine(_name + "@" + IOStreams.convertFileSeparator(_presentWorkingDirectory).replace(_username, _name) + "> ");
405415

406416
// Interpret and execute the command
407417
grinchInterpreter(inputValue);
@@ -432,7 +442,8 @@ public void fileManagementLogic(String scriptFileName)throws Exception
432442
else
433443
{
434444
// Check if the user is logged in
435-
if (login()) {
445+
if (login())
446+
{
436447
// Initialize a stream to read the given file
437448
BufferedReader br = new BufferedReader(new FileReader(scriptFileName));
438449

@@ -489,7 +500,7 @@ private void grinchInterpreter(String command)throws Exception
489500
else
490501
// Call the method to move the entity
491502
copyMoveEntity(commandArray[1], commandArray[2], true);
492-
break;
503+
break;
493504

494505
case "copy":
495506
case "cp":
@@ -499,7 +510,7 @@ private void grinchInterpreter(String command)throws Exception
499510
else
500511
// Call the method to copy the entity
501512
copyMoveEntity(commandArray[1], commandArray[2], false);
502-
break;
513+
break;
503514

504515
case "delete":
505516
case "del":
@@ -510,7 +521,7 @@ private void grinchInterpreter(String command)throws Exception
510521
else
511522
// Call the method to delete the entity
512523
deleteEntity(commandArray[1]);
513-
break;
524+
break;
514525

515526
case "rename":
516527
// Check if the command has the correct number of arguments
@@ -519,7 +530,7 @@ private void grinchInterpreter(String command)throws Exception
519530
else
520531
// Call the method to rename the entity
521532
renameEntity(commandArray[1], commandArray[2]);
522-
break;
533+
break;
523534

524535
case "mkdir":
525536
// Check if the command has the correct number of arguments
@@ -528,7 +539,7 @@ private void grinchInterpreter(String command)throws Exception
528539
else
529540
// Call the method to create a directory
530541
makeDirectory(commandArray[1]);
531-
break;
542+
break;
532543

533544
case "edit":
534545
// Check if the command has the correct number of arguments
@@ -537,7 +548,7 @@ private void grinchInterpreter(String command)throws Exception
537548
else
538549
// Call the method to edit a file
539550
new FileWrite(_username).editFile(commandArray[1], _presentWorkingDirectory);
540-
break;
551+
break;
541552

542553
case "read":
543554
// Check if the command has the correct number of arguments
@@ -546,12 +557,12 @@ private void grinchInterpreter(String command)throws Exception
546557
else
547558
// Call the method to read a file
548559
new FileRead(_username).readUserFile(_presentWorkingDirectory + commandArray[1]);
549-
break;
560+
break;
550561

551562
case "pwd":
552563
// Print the present working directory
553564
IOStreams.println((_presentWorkingDirectory).replace(_username, _name));
554-
break;
565+
break;
555566

556567
case "cd":
557568
// Check if the command has the correct number of arguments
@@ -560,23 +571,23 @@ private void grinchInterpreter(String command)throws Exception
560571
else
561572
// Call the method to change the directory
562573
changeDirectory(commandArray[1]);
563-
break;
574+
break;
564575

565576
case "cd..":
566577
// Navigate to the previous directory
567578
navPreviousDirectory();
568-
break;
579+
break;
569580

570581
case "tree":
571582
// Display the directory tree
572583
viewDirectoryTree();
573-
break;
584+
break;
574585

575586
case "dir":
576587
case "ls":
577588
// List the entities in the current directory
578589
listEntities();
579-
break;
590+
break;
580591

581592
case "download":
582593
// Check if the command has the correct number of arguments
@@ -585,17 +596,17 @@ private void grinchInterpreter(String command)throws Exception
585596
else
586597
// Call the method to download a file
587598
new FileDownload(_username).downloadFile(commandArray[1], commandArray[2]);
588-
break;
599+
break;
589600

590601
case "home":
591602
// Reset to the home directory
592603
resetToHomeDirectory();
593-
break;
604+
break;
594605

595606
case "exit":
596607
case "":
597608
// Exit the interpreter
598-
break;
609+
break;
599610

600611
default:
601612
// Pass the command to the Anvil interpreter for further processing

0 commit comments

Comments
 (0)