-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_TextEditor.cpp
259 lines (213 loc) · 7.42 KB
/
app_TextEditor.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* editor.cpp: This file implements the methods
* and functions in editorBuffer.h
*/
#include "app_TextEditor.h"
#include <taskManagement.h>
#include "mem.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void toUpperCaseInPlace(string& str) {
int nChars = str.length();
for (int i = 0; i < nChars; i++) {
str[i] = toupper(str[i]);
}
}
string toUpperCase(const string& str) {
string str2 = str;
toUpperCaseInPlace(str2);
return str2;
}
EditorBuffer::EditorBuffer(){
start = cursor = new Cell;
start->link = NULL;
}
EditorBuffer::~EditorBuffer(){
Cell *cp = start;
while(cp != NULL){
Cell *next = cp->link;
delete cp;
cp = next;
}
}
void EditorBuffer::moveCursorForward(){
if (cursor->link !=NULL){
cursor = cursor->link;
}
}
void EditorBuffer::moveCursorBackward(){
Cell *cp = start;
if (cursor != start){
while(cp->link != cursor){
cp = cp->link;
}
cursor = cp;
}
}
void EditorBuffer::moveCursorToStart(){
cursor = start;
}
void EditorBuffer::moveCursorToEnd(){
while (cursor->link != NULL){
cursor = cursor->link;
}
}
void EditorBuffer::insertCharacter(char ch){
Cell *cp = new Cell;
cp->ch = ch;
cp->link = cursor->link;
cursor->link = cp;
cursor = cp;
}
void EditorBuffer::deleteCharter(){
if (cursor->link != NULL){
Cell *oldCell = cursor->link;
cursor->link = cursor->link->link;
delete oldCell;
}
}
void EditorBuffer::showContents(){
for (Cell *cp = start->link; cp!=NULL; cp = cp->link){
cout << ' '<< cp->ch;
}
cout << endl;
for (Cell *cp = start; cp != cursor; cp = cp->link){
cout << " ";
}
cout << '^'<< endl;
}
bool EditorBuffer::HangOnEditor(int taskid, vector <string> user_name, string username, string & jsonmem,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue,
vector<PCB_type> & blockQueue,
vector<PCB_type> & readyQueue){
int textsize = 0;
bool flag = true;
for (Cell *cp = start->link; cp!=NULL; cp = cp->link){
textsize++;
string key = "text" + to_string(textsize);
string x;
x = cp->ch;
flag = task_data_write(taskid, key, x, user_name, username, jsonmem);
if (!flag) break;
}
flag = task_data_write(taskid, "textsize", to_string(textsize), user_name, username, jsonmem);
if (flag) {
cout << "Hangon succeeded!" << endl;
block(username, taskid, mem,
runningQueue, blockQueue, readyQueue);
return true;
} else{
kill(username, taskid, jsonmem,
mem, runningQueue, blockQueue, readyQueue);
cout << "Hangon failed! Lack of memory space" << endl;
return false;
}
}
void editorHelp(){
cout << endl;
cout << "+--------------------------------------------------------------+"<<endl;
cout << "| Commands for text editor |"<< endl;
cout << "+--------------------------------------------------------------+"<<endl;
cout << "|"<< right << setw(7)<<" "<< left<<
setw(55)<< " (Following commands are case-insensitive) "<<"|"<<endl;
cout << "|"<< right << setw(7)<<" "<< left<< setw(55)<< " "<<"|"<<endl;
cout << "|"<< right << setw(7)<<"Itext:"<< left<<
setw(55)<< " Inserts the text following 'I' into the buffer "<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"J:"<< left<<
setw(55)<< " Jumps the current point (the cursor) to the beginning"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"E:"<< left<<
setw(55)<< " Moves the cursor to the end of the buffer"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"F:"<< left<<
setw(55)<< " Moves the cursor forward one character"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"B:"<< left<<
setw(55)<< " Moves the cursor backward one character"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"D:"<< left<<
setw(55)<< " Deletes the character after the cursor"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"H:"<< left<<
setw(55)<< " Prints a help message listing the commands"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"Q:"<< left<<
setw(55)<< " Quits from the editor"<<"|"<<endl;
cout << "|"<< right<< setw(7)<<"L:"<< left<<
setw(55)<< " Hang On the editor task and jump to next task"<<"|"<<endl;
cout << "+--------------------------------------------------------------+"<<endl;
cout << endl;
}
void editText(EditorBuffer &editor, vector <string> user_name, string username, string & jsonmem, int taskid,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue,
vector<PCB_type> & blockQueue,
vector<PCB_type> & readyQueue){
string command;
editorHelp();
editor.showContents();
while (true){
cout << "Please enter your command for text editing: ";
getline(cin, command);
if (command.length()==1){
command = toUpperCase(command);
if (command == "J") {
editor.moveCursorToStart();
editor.showContents();
}
if (command == "E"){
editor.moveCursorToEnd();
editor.showContents();
}
if (command == "F"){
editor.moveCursorForward();
editor.showContents();
}
if (command == "B"){
editor.moveCursorBackward();
editor.showContents();
}
if (command == "D"){
editor.deleteCharter();
editor.showContents();
}
if (command == "H"){
editorHelp();
}
if (command == "Q"){
cout << "Texteditor killed" <<endl;
kill(username, taskid, jsonmem,
mem, runningQueue, blockQueue, readyQueue);
return;
}
if (command == "L"){
editor.HangOnEditor(taskid, user_name, username, jsonmem, mem,
runningQueue, blockQueue, readyQueue);
return;
}
}
else if (command[0]=='I'){
command = command.c_str();
for (int i = 1; i < int(command.length()); i++){
editor.insertCharacter(command[i]);
}
editor.showContents();
}
else cout << "Invalid Command (Enter H for help)"<<endl;
}
}
void TextEditor(int taskid, bool wakeup, string & jsonmem,
string username, vector<string> user_name, PCB_type (&mem)[100],
vector<PCB_type> & runningQueue,
vector<PCB_type> & blockQueue,
vector<PCB_type> & readyQueue){
EditorBuffer editor;
if (wakeup){
int textsize = stoi(task_data_read(taskid, "textsize", jsonmem));
for (int i = 1; i <= textsize; i++){
string key = "text" + to_string(i);
string data = task_data_read(taskid, key, jsonmem);
data = data.c_str();
editor.insertCharacter(data[0]);
}
}
editText(editor, user_name, username, jsonmem,
taskid, mem, runningQueue, blockQueue, readyQueue);
}