From 5dacd371566db977923e6da4c7c9783050608321 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Thu, 9 Dec 2021 10:10:34 -0500 Subject: Reorganized the file and fixed not reading file when name is added at the start --- src/jed.java | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) (limited to 'src/jed.java') diff --git a/src/jed.java b/src/jed.java index 7c14070..ddd3718 100644 --- a/src/jed.java +++ b/src/jed.java @@ -4,7 +4,7 @@ import java.util.Scanner; public class jed { - public static Scanner kbIn = new Scanner(System.in); + private static Scanner kbIn = new Scanner(System.in); public static filebuffer file; public static void main(String args[]) { @@ -20,10 +20,88 @@ public class jed { while (true) { String line = kbIn.nextLine(); - int err = cmd.parser(line); + int err = parser(line); if (err == 1) break; } kbIn.close(); } + + /* parser: the command parser for jed, takes user input and interprets the command */ + private static int parser(String cmd) { + int err = 0; + if (isNum(cmd)) { + jed.file.changeCurrentLine(Integer.parseInt(cmd) - 1); + System.out.printf("%d %s\n", jed.file.getCurrentLine() + 1, jed.file.getLine(jed.file.getCurrentLine())); + } else { + char c = cmd.charAt(0); + switch (c) { + case 'w': + if (cmd.length() > 1) { + String s[] = new String[2]; + s = cmd.split(" "); + jed.file.changeName(s[1]); + } + err = jed.file.writeFile(); + break; + case 'q': // q quits + return 1; + case 'a': // a appends + jed.file.append(kbIn); + break; + case 'p': // p prints lines + jed.file.printFile(false); + break; + case 'n': //n prints lines with numbers + jed.file.printFile(true); + break; + case 'c': // c change current line + jed.file.changeLine(kbIn); + break; + case 'd': + jed.file.deleteLine(); // d delete current line + break; + case 'g': // g/expression/ finds user input + jed.file.find(cmd.substring(2, cmd.length() - 1)); + break; + case '%': // %s/expression/newExpression/ replaces an expression with new user input through the whole file + String s[] = new String[3]; + s = cmd.split("/"); + jed.file.replace(0, jed.file.getFileSize(), s[1], s[2]); + break; + case '<': // command works with d and s/ex/nex/ + String t[] = new String[2]; + t = cmd.split(">"); + jed.file.range(t[0], t[1]); + break; + case 's': // s/expression/newExpression/ replaces an expression with new user input in the current line + String m[] = new String[3]; + m = cmd.split("/"); + jed.file.replace(jed.file.getCurrentLine(), jed.file.getCurrentLine() + 1, m[1], m[2]); + break; + case 'o': // o fileName opens a file + jed.file.changeName(cmd.substring(2,cmd.length())); + jed.file.readFile(); + break; + default: + System.out.println("?"); + break; + } + } + if (err == 1) // error in writing the file + System.out.println("Error writing the file"); + return 0; + } + + /* isNum: checks if a string is a number, only to be used for changing lines */ + private static boolean isNum(String s) { + try { + Integer.parseInt(s); + } catch (NumberFormatException e) { + return false; + } catch (NullPointerException e) { + return false; + } + return true; + } } \ No newline at end of file -- cgit v1.2.3