-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (157 loc) · 3.63 KB
/
main.c
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
#include "func.h"
#include <ncurses.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define CTRL_KEYPRESS(k) ((k) & 0x1f)
int cRow, cCol;
int maxRows, maxCols;
int main(int argc, char *argv[]) {
int c, i = 0, e = 0, mode, err, fileflag = 0, firstTab = 0;
char ch;
node *temp;
buffer text;
FILE *fp, *fp_new;
//User must enter name of file along with ./project
if(argc != 2) {
errno = EINVAL;
perror("Usage - <./project> <file_name> ");
return errno;
}
e = fileExists(argv[1]); //To check if the file already exists
//Condition when the file does not exist
if(e == -1) {
fp = fopen(argv[1], "w+");
if(fp == NULL) {
perror("Could not open file ");
return errno;
}
mode = 0;
initNcurses(mode);
initBuffer(&text);
}
//Conditon when the file exists
else {
fp = fopen(argv[1], "r+");
if(fp == NULL) {
perror("Could not open file ");
return errno;
}
mode = 1;
initNcurses(mode);
initBuffer(&text);
readFileToBuffer(&text, fp);
fclose(fp);
//To erase the previous contents of the file in order to rewrite
fp = fopen(argv[1], "w");
move(0,0);
}
//Handling user input
while((c = getch()) != CTRL_KEYPRESS('q')) {
if((c > 31 && c < 127) || (c == 10)) {
insertBuffer(&text, c);
}
else if(c == 9) {
if(firstTab == 0) {
tabPrompt();
firstTab = 1;
}
insertTab(&text);
}
else if(c == KEY_BACKSPACE) {
backBuffer(&text);
}
else if(c == KEY_DC) {
deleteBuffer(&text);
}
else if(c == KEY_RIGHT) {
arrowRight(&text);
}
else if(c == KEY_LEFT) {
arrowLeft(&text);
}
else if(c == KEY_UP) {
arrowUp(&text);
}
else if(c == KEY_DOWN) {
arrowDown(&text);
}
else if(c == KEY_MOUSE) {
mouseLeftKey(&text);
}
else if(c == KEY_SRIGHT) {
selectRight(&text);
}
else if(c == KEY_SLEFT) {
selectLeft(&text);
}
else if(c == KEY_RESIZE) {
resizeTerminal(&text);
}
else if (c == CTRL_KEYPRESS('f')) {
search(&text); //Ctrl F to search text
}
else if (c == CTRL_KEYPRESS('c')) {
copy(&text); //Ctrl C to copy text
}
else if (c == CTRL_KEYPRESS('x')) {
cut(&text); //Ctrl X to cut text
}
else if (c == CTRL_KEYPRESS('v')) {
paste(&text); //Ctrl V to paste text
}
else if (c == CTRL_KEYPRESS('a')) {
selectAll(&text); //Ctrl A to select all
}
else if (c == CTRL_KEYPRESS('d')) {
deleteRow(&text); //Ctrl D to delete row
}
else if (c == CTRL_KEYPRESS('y')) {
copyRow(&text); //Ctrl Y to copy row
}
else if (c == CTRL_KEYPRESS('r')) {
ctrlRight(&text); //Ctrl R to point to the next word
}
else if (c == CTRL_KEYPRESS('l')) {
ctrlLeft(&text); //Ctrl L to point to the previous word
}
else if (c == CTRL_KEYPRESS('s')) {
err = saveAs(&text); //Ctrl S to Save As
if(err == -1) {
destroyBuffer(&text);
clear();
endwin();
return 0;
}
else
break; //Stop accepting input after saving and quit
}
else if (c == CTRL_KEYPRESS('o')) {
fp_new = openNewFile(&text, fp); //Ctrl O to Open new
if(fp_new == NULL) {
perror("Could not open file ");
endwin();
return 0;
}
fileflag = 1; //Original file closed, write data in new file on quiting
}
}
//Writing the data structure into the file on quiting - Ctrl Q
temp = text.head;
while(temp != NULL) {
while(temp->p[i] != '\0') {
ch = (char)temp->p[i];
if(fileflag == 0)
fwrite(&ch, 1, 1, fp);
else
fwrite(&ch, 1, 1, fp_new);
i++;
}
temp = temp->next;
i = 0;
}
fclose(fp);
destroyBuffer(&text);
clear();
endwin();
}