summaryrefslogtreecommitdiff
path: root/src/filebuffer.java
blob: 4015b55e81f3078fa627cefc565de3a005ba3a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package jed;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class filebuffer {

    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;
        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 */
    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;
    }

    /* 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);
    }
}