summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2021-12-09 10:10:34 -0500
committerJacob McDonnell <jacob@simplelittledream.com>2021-12-09 10:10:34 -0500
commit5dacd371566db977923e6da4c7c9783050608321 (patch)
treeeb21925ffaabe8c1cb0d16faea22d348b5fcd2e5 /src
parent8d37ce9bf25ccd37e50e9f766bb8df710e855217 (diff)
Reorganized the file and fixed not reading file when name is added at the start
Diffstat (limited to 'src')
-rw-r--r--src/cmd.java151
-rw-r--r--src/filebuffer.java102
-rw-r--r--src/jed.java82
3 files changed, 177 insertions, 158 deletions
diff --git a/src/cmd.java b/src/cmd.java
deleted file mode 100644
index d49c589..0000000
--- a/src/cmd.java
+++ /dev/null
@@ -1,151 +0,0 @@
-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 '<': // <integer, integer>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
index 1ed0406..4015b55 100644
--- a/src/filebuffer.java
+++ b/src/filebuffer.java
@@ -6,15 +6,42 @@ import java.util.Scanner;
public class filebuffer {
- public ArrayList<String> buffer;
- public String fileName;
- public boolean hasName;
+ private ArrayList<String> buffer;
+ private String fileName;
+ private boolean hasName;
+ private int currentLine = 0;
public filebuffer(String fn, boolean hn) {
buffer = new ArrayList<String>(); // Arraylist that stores every line of a file in strings
hasName = hn;
- if (hasName)
- fileName = fn;
+ fileName = fn;
+ readFile();
+ }
+
+ /* getFileSize: gets the size of the buffer arraylist */
+ public int getFileSize() {
+ return buffer.size();
+ }
+
+ /* getCurrentLine: returns the current line value */
+ public int getCurrentLine() {
+ return currentLine;
+ }
+
+ /* changeCurrentLine: changes the current line value */
+ public void changeCurrentLine(int n) {
+ currentLine = n;
+ }
+
+ /* getLine: returns the selected line as a string */
+ public String getLine(int n) {
+ return buffer.get(n);
+ }
+
+ /* changeName: changes/adds the file name */
+ public void changeName(String name) {
+ fileName = name;
+ hasName = true;
}
/* printFile: prints strings from file arraylist, if num is true it includes line numbers */
@@ -73,4 +100,69 @@ public class filebuffer {
}
return 0;
}
+
+ /* range: gets the inputed range and runs the command with the range */
+ public 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 > buffer.size() || stop <= 0 || stop-1 > 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 */
+ public 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 */
+ public void replace(int start, int stop, String ex, String newEx) {
+ for (int i = start; i < stop; i++) {
+ String line = buffer.get(i);
+ buffer.add(i, line.replace(ex, newEx));
+ buffer.remove(i + 1);
+ }
+ }
+
+ /* find: finds user input in the file and prints it with line numbers */
+ public void find(String expression) {
+ for (int i = 0; i < buffer.size(); i++) {
+ String line = 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 */
+ public void changeLine(Scanner kbIn) {
+ buffer.remove(currentLine);
+ String input;
+ int line = currentLine;
+ while (!(input = kbIn.nextLine()).equals("."))
+ buffer.add(line++, input);
+ }
+
+ /* deleteLine: deletes the current line */
+ public void deleteLine() {
+ buffer.remove(currentLine);
+ }
+
+ /* append: appends user input to the end of a file */
+ public void append(Scanner kbIn) {
+ String input;
+ while (!(input = kbIn.nextLine()).equals("."))
+ buffer.add(input);
+ }
} \ No newline at end of file
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 '<': // <integer, integer>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