From 8d37ce9bf25ccd37e50e9f766bb8df710e855217 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 8 Dec 2021 21:52:01 -0500 Subject: Rewrote Jed to have multiple classes and cleaned up the code --- MANIFEST.MF | 1 - build/jed | 3 + jed | 3 - jed.java | 215 ---------------------------------------------------- makefile | 10 +-- src/MANIFEST.MF | 1 + src/cmd.java | 151 ++++++++++++++++++++++++++++++++++++ src/filebuffer.java | 76 +++++++++++++++++++ src/jed.java | 29 +++++++ 9 files changed, 265 insertions(+), 224 deletions(-) delete mode 100644 MANIFEST.MF create mode 100755 build/jed delete mode 100755 jed delete mode 100644 jed.java create mode 100644 src/MANIFEST.MF create mode 100644 src/cmd.java create mode 100644 src/filebuffer.java create mode 100644 src/jed.java diff --git a/MANIFEST.MF b/MANIFEST.MF deleted file mode 100644 index 43cb809..0000000 --- a/MANIFEST.MF +++ /dev/null @@ -1 +0,0 @@ -Main-Class: jed diff --git a/build/jed b/build/jed new file mode 100755 index 0000000..3ac0634 --- /dev/null +++ b/build/jed @@ -0,0 +1,3 @@ +#!/bin/sh + +java -jar ./jed.jar $@ diff --git a/jed b/jed deleted file mode 100755 index 8fb1d51..0000000 --- a/jed +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -java -jar jed.jar $@ diff --git a/jed.java b/jed.java deleted file mode 100644 index 8f6d249..0000000 --- a/jed.java +++ /dev/null @@ -1,215 +0,0 @@ -import java.util.Scanner; -import java.io.File; -import java.io.FileNotFoundException; -import java.util.ArrayList; -import java.io.FileWriter; -import java.io.PrintWriter; -import java.io.IOException; - -class jed { - - private static ArrayList file = new ArrayList(); // Arraylist that stores every line of a file in strings - private static Scanner kbIn = new Scanner(System.in); - private static int currentLine = 0; - private static boolean hasName = true; - - public static void main(String args[]) { - String fileName = null; - try { // checks if the user provided a file name - fileName = args[0]; - readFile(args[0]); - } catch (IndexOutOfBoundsException e) { - hasName = false; - } - - while (true) { - String line = kbIn.nextLine(); - int err = parser(line, fileName); - 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, String fileName) { - int err = 0; - if (cmd.equals("w")) // w writes the file - err = writeFile(fileName); - else if (cmd.equals("q")) // q quits - return 1; - else if (cmd.equals("a")) // a appends - append(); - else if (cmd.equals("p")) // p prints lines - printFile(false); - else if (cmd.equals("n")) //n prints lines with numbers - printFile(true); - else if (isNum(cmd)) { // some positive integer to change lines - currentLine = Integer.parseInt(cmd) - 1; - System.out.printf("%d %s\n", currentLine + 1, file.get(currentLine)); - } else if (cmd.equals("c")) // c change current line - changeLine(); - else if (cmd.equals("d")) // d delete current line - deleteLine(); - else if (cmd.substring(0,2).equals("w ")) { // w FILENAME writes a file with user inputed name - String s[] = new String[2]; - s = cmd.split(" "); - hasName = true; - err = writeFile(s[1]); - } else if (cmd.substring(0,1).equals("g")) { // g/expression/ finds user input - find(cmd.substring(2, cmd.length() - 1)); - } else if (cmd.substring(0,2).equals("%s")) { // %s/expression/newExpression/ replaces an expression with new user input through the whole file - String temp[] = new String[3]; - temp = cmd.split("/"); - replace(0, file.size(), temp[1], temp[2]); - } else if (cmd.substring(0,1).equals("<")) { // command works with d and s/ex/nex/ - String temp[] = new String[2]; - temp = cmd.split(">"); - range(temp[0], temp[1]); - } else if (cmd.substring(0,1).equals("s")) { // s/expression/newExpression/ replaces an expression with new user input in the current line - String temp[] = new String[3]; - temp = cmd.split("/"); - replace(currentLine, currentLine + 1, temp[1], temp[2]); - } else if (cmd.substring(0,1).equals("o")) { // o fileName opens a file - readFile(cmd.substring(2,cmd.length())); - } else // unknown command - System.out.println("?"); - if (err == 1) // error in writing the file - System.out.println("Error writing the file"); - return 0; - } - - /* range: gets the inputed range and runs the command with the range */ - private static void range(String range, String cmd) { - String num[] = new String[2]; - num = range.replace("<", "").split(","); - String temp[] = new String[3]; - temp = cmd.split("/"); - int start = Integer.parseInt(num[0]) - 1, stop = Integer.parseInt(num[1]); - if (start <= 0 || start-1 >file.size() || stop <= 0 || stop-1 > file.size() || stop <= start) - System.out.println("?"); - if (temp[0].equals("s")) - replace(start, stop, temp[1], temp[2]); - else if (temp[0].equals("d")) - deleteLine(start, stop); - } - - /* deleteLine: deletes line in the range of start to stop */ - private static void deleteLine(int start, int stop) { - for (int i = start; i < stop; i++) { - file.remove(start); - } - } - - /* replace: replaces an expression with new user input */ - private static void replace(int start, int stop, String ex, String newEx) { - for (int i = start; i < stop; i++) { - String line = file.get(i); - file.add(i, line.replace(ex, newEx)); - file.remove(i + 1); - } - } - - /* find: finds user input in the file and prints it with line numbers */ - private static void find(String expression) { - for (int i = 0; i < file.size(); i++) { - String line = file.get(i); - for (int j = 0, k = expression.length(); k < line.length(); j++, k++) { - if (line.substring(j, k).equals(expression)) { - System.out.printf("%d %s\n", i + 1, line); - break; - } - } - } - } - - /* changeLine: changes the current line */ - private static void changeLine() { - file.remove(currentLine); - String input; - int line = currentLine; - while (!(input = kbIn.nextLine()).equals(".")) - file.add(line++, input); - } - - /* deleteLine: deletes the current line */ - private static void deleteLine() { - file.remove(currentLine); - } - - /* 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; - } - - /* append: appends user input to the end of a file */ - private static void append() { - String input; - while (!(input = kbIn.nextLine()).equals(".")) - file.add(input); - } - - /* printFile: prints strings from file arraylist, if num is true it includes line numbers */ - private static void printFile(boolean num) { - for (int i = 0; i < file.size(); i++) { - if (num) - System.out.printf("%d %s\n", i+1, file.get(i)); - else - System.out.println(file.get(i)); - } - } - - /* createFile: creates a file, only to be called by writeFile */ - private static int createFile(String fileName) { - try { - File f = new File(fileName); - if (f.createNewFile()) - return 1; - return 0; - } catch (IOException e) { - return -1; - } - } - - /* writeFile: reads strings from file arraylist and prints them into a file */ - private static int writeFile(String fileName) { - if (!hasName) { - System.out.println("?"); - return 1; - } - int err = createFile(fileName); - if (err == -1) - return 1; - try { - FileWriter fw = new FileWriter(fileName); - PrintWriter pw = new PrintWriter(fw); - for (int i = 0; i < file.size(); i++) - pw.println(file.get(i)); - pw.close(); - } catch (IOException e) { - return 1; - } - return 0; - } - - /* readFile: opens a file and stores each line in a string arraylist called file */ - private static int readFile(String fileName) { - try { - File fileObj = new File(fileName); - Scanner fileReader = new Scanner(fileObj); - while(fileReader.hasNextLine()) - file.add(fileReader.nextLine()); - fileReader.close(); - } catch (FileNotFoundException e) { - createFile(fileName); - } - return 0; - } -} diff --git a/makefile b/makefile index afdc0d3..b2e4ba4 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ -all: jed.java - javac jed.java - jar cvmf MANIFEST.MF jed.jar jed.class +all: + javac -d . src/jed.java src/cmd.java src/filebuffer.java + jar cvmf src/MANIFEST.MF build/jed.jar jed/jed.class jed/cmd.class jed/filebuffer.class run: all - ./jed + . build/jed clean: - rm jed.jar jed.class + rm -r build/jed.jar jed diff --git a/src/MANIFEST.MF b/src/MANIFEST.MF new file mode 100644 index 0000000..9efb8fb --- /dev/null +++ b/src/MANIFEST.MF @@ -0,0 +1 @@ +Main-Class: jed/jed diff --git a/src/cmd.java b/src/cmd.java new file mode 100644 index 0000000..d49c589 --- /dev/null +++ b/src/cmd.java @@ -0,0 +1,151 @@ +package jed; + +public class cmd { + + private static int currentLine = 0; + + /* parser: the command parser for jed, takes user input and interprets the command */ + public static int parser(String cmd) { + int err = 0; + if (isNum(cmd)) { + currentLine = Integer.parseInt(cmd) - 1; + System.out.printf("%d %s\n", currentLine + 1, jed.file.buffer.get(currentLine)); + } else { + char c = cmd.charAt(0); + switch (c) { + case 'w': + if (cmd.length() > 1) { + String s[] = new String[2]; + s = cmd.split(" "); + jed.file.hasName = true; + jed.file.fileName = s[1]; + } + err = jed.file.writeFile(); + break; + case 'q': // q quits + return 1; + case 'a': // a appends + append(); + 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 + changeLine(); + break; + case 'd': + deleteLine(); // d delete current line + break; + case 'g': // g/expression/ finds user input + 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("/"); + replace(0, jed.file.buffer.size(), s[1], s[2]); + break; + case '<': // command works with d and s/ex/nex/ + String t[] = new String[2]; + t = cmd.split(">"); + 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("/"); + replace(currentLine, currentLine + 1, m[1], m[2]); + break; + case 'o': // o fileName opens a file + jed.file.fileName = cmd.substring(2,cmd.length()); + jed.file.hasName = true; + 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; + } + + /* range: gets the inputed range and runs the command with the range */ + private static void range(String range, String cmd) { + String num[] = new String[2]; + num = range.replace("<", "").split(","); + String temp[] = new String[3]; + temp = cmd.split("/"); + int start = Integer.parseInt(num[0]) - 1, stop = Integer.parseInt(num[1]); + if (start <= 0 || start-1 >jed.file.buffer.size() || stop <= 0 || stop-1 > jed.file.buffer.size() || stop <= start) + System.out.println("?"); + if (temp[0].equals("s")) + replace(start, stop, temp[1], temp[2]); + else if (temp[0].equals("d")) + deleteLine(start, stop); + } + + /* deleteLine: deletes line in the range of start to stop */ + private static void deleteLine(int start, int stop) { + for (int i = start; i < stop; i++) { + jed.file.buffer.remove(start); + } + } + + /* replace: replaces an expression with new user input */ + private static void replace(int start, int stop, String ex, String newEx) { + for (int i = start; i < stop; i++) { + String line = jed.file.buffer.get(i); + jed.file.buffer.add(i, line.replace(ex, newEx)); + jed.file.buffer.remove(i + 1); + } + } + + /* find: finds user input in the file and prints it with line numbers */ + private static void find(String expression) { + for (int i = 0; i < jed.file.buffer.size(); i++) { + String line = jed.file.buffer.get(i); + for (int j = 0, k = expression.length(); k < line.length(); j++, k++) { + if (line.substring(j, k).equals(expression)) { + System.out.printf("%d %s\n", i + 1, line); + break; + } + } + } + } + + /* changeLine: changes the current line */ + private static void changeLine() { + jed.file.buffer.remove(currentLine); + String input; + int line = currentLine; + while (!(input = jed.kbIn.nextLine()).equals(".")) + jed.file.buffer.add(line++, input); + } + + /* deleteLine: deletes the current line */ + private static void deleteLine() { + jed.file.buffer.remove(currentLine); + } + + /* 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; + } + + /* append: appends user input to the end of a file */ + private static void append() { + String input; + while (!(input = jed.kbIn.nextLine()).equals(".")) + jed.file.buffer.add(input); + } +} \ No newline at end of file diff --git a/src/filebuffer.java b/src/filebuffer.java new file mode 100644 index 0000000..1ed0406 --- /dev/null +++ b/src/filebuffer.java @@ -0,0 +1,76 @@ +package jed; + +import java.io.*; +import java.util.ArrayList; +import java.util.Scanner; + +public class filebuffer { + + public ArrayList buffer; + public String fileName; + public boolean hasName; + + public filebuffer(String fn, boolean hn) { + buffer = new ArrayList(); // Arraylist that stores every line of a file in strings + hasName = hn; + if (hasName) + fileName = fn; + } + + /* printFile: prints strings from file arraylist, if num is true it includes line numbers */ + public void printFile(boolean num) { + for (int i = 0; i < buffer.size(); i++) { + if (num) + System.out.printf("%d %s\n", i+1, buffer.get(i)); + else + System.out.println(buffer.get(i)); + } + } + + /* createFile: creates a file, only to be called by writeFile */ + private int createFile() { + try { + File f = new File(fileName); + if (f.createNewFile()) + return 1; + return 0; + } catch (IOException e) { + return -1; + } + } + + /* writeFile: reads strings from file arraylist and prints them into a file */ + public int writeFile() { + if (!hasName) { + System.out.println("?"); + return 1; + } + int err = createFile(); + if (err == -1) + return 1; + try { + FileWriter fw = new FileWriter(fileName); + PrintWriter pw = new PrintWriter(fw); + for (int i = 0; i < buffer.size(); i++) + pw.println(buffer.get(i)); + pw.close(); + } catch (IOException e) { + return 1; + } + return 0; + } + + /* readFile: opens a file and stores each line in a string arraylist called file */ + public int readFile() { + try { + File fileObj = new File(fileName); + Scanner fileReader = new Scanner(fileObj); + while(fileReader.hasNextLine()) + buffer.add(fileReader.nextLine()); + fileReader.close(); + } catch (FileNotFoundException e) { + createFile(); + } + return 0; + } +} \ No newline at end of file diff --git a/src/jed.java b/src/jed.java new file mode 100644 index 0000000..7c14070 --- /dev/null +++ b/src/jed.java @@ -0,0 +1,29 @@ +package jed; + +import java.util.Scanner; + +public class jed { + + public static Scanner kbIn = new Scanner(System.in); + public static filebuffer file; + + public static void main(String args[]) { + String fileName = null; + boolean hasName = true; + try { // checks if the user provided a file name + fileName = args[0]; + } catch (IndexOutOfBoundsException e) { + hasName = false; + } + + file = new filebuffer(fileName, hasName); + + while (true) { + String line = kbIn.nextLine(); + int err = cmd.parser(line); + if (err == 1) + break; + } + kbIn.close(); + } +} \ No newline at end of file -- cgit v1.2.3