-
Notifications
You must be signed in to change notification settings - Fork 0
/
cproject.c
399 lines (324 loc) · 10 KB
/
cproject.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include<stdio.h> //including header files //stdio.h used for input or output operations
#include<string.h> //used for strcpy function
#include<stdlib.h> //used for system("clear") function
#include<termios.h> //used for getch function
#include<unistd.h> //used for getch function
struct dll //definition of the structure double linked list
{
char s[200]; //character array
int index;
struct dll*prev,*next; //pointers to next and previous nodes
};
int getch() //getch function definition
{
struct termios oldt, newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
void editcommands(void); //function prototypes
void addline(struct dll *temp);
void inp(void);
void printlist(void);
void closer(void);
void edit(void);
void addnode(char t[],struct dll *q);
void delnode(struct dll *p);
void clealist(void);
void editnode(struct dll *p);
void save(void);
struct dll *head; //header node declaration
char file[20];
FILE *fp=NULL; //file pointer declaration
main()
{
char c;
system("\e[0;36m");
head=(struct dll*)malloc(sizeof(struct dll)); //header node memory allocation
(head->next)=(head->prev)=NULL; //initialization
(head->index)=0;
while(1) //infinite while loop for editing multiple number of tiles
{
system("clear"); //clearing the screen
//Displaying editor options
printf("\n TEXT EDITOR -- VERSION 21.2 \n\n");
printf("\n\n\n R : To OPEN a file; If file does not exist, new one is created.\n");
printf(" E : To EDIT an already open file\n");
printf(" X : To open a file by closing the currently open file\n");
printf(" Q : EXIT\n");
c=getch(); //taking user input
switch(c) //testing with switch
{
case 'r' :
case 'R' :
inp();
break;
case 'e' :
case 'E' :
edit();
break;
case 'x' :
case 'X' :
closer();
break;
case 'q' :
case 'Q' :
system("clear");
exit(1);
break;
}
}
} //end of main
void addnode(char t[],struct dll *q) //function to add a new node after a node q
{
struct dll*p=(struct dll*)malloc(sizeof(struct dll));
struct dll *temp=q->next;
strcpy(p->s,t);
p->prev=q;
p->next=q->next;
if((q->next)!=NULL) //adding the node to the list by manipulating pointers accordingly
{
((q->next)->prev)=p;
while(temp!=NULL)
{
(temp->index)++; //incrementing the index of the later nodes
temp=temp->next;
}
}
q->next=p;
p->index = q->index + 1; //setting the index of the new node
}
void delnode(struct dll *p) //function to delete a node
{
struct dll *temp=p->next;
((p->prev->next))=p->next;
if(p->next!=NULL)
{
((p->next)->prev)=p->prev;
while(temp!=NULL)
{
(temp->index)--; //decrementing the index of the later nodes
temp=temp->next;
}
}
free(p); //freeing ht memory of the deleted node
}
void clearlist(void) //function to clear the list
{
while(head->next!=NULL)
delnode(head->next); //deleting all nodes except head
}
void editnode(struct dll *p) //function to edit a line
{
printf("\nThe original line is :\n%s",p->s);
printf("\nEnter the new line :\n");
gets(p->s); //taking the new line input
printf("\nLine edited\n");
}
void printlist(void) //function to print all the lines stored in the buffer
{
struct dll *temp=head;
system("clear");
while(temp->next!=NULL)
{
temp=temp->next;
printf("%d %s\n",temp->index,temp->s); //printing the lines on the screen
}
}
void closer(void) //function to close the file opened for editing
{
if(fp==NULL)
return;
fclose(fp);
fp=NULL;
clearlist(); //the list is also cleared at this point
}
void inp(void)
{
struct dll *buff=head; //temporaty variable
char c;
char buf[200]; //array to store input line
if(fp!=NULL) //checking for files opened earlier
{
printf("\nThere is another file open it will be closed\ndo you want to continue ?(Y/N):");
c=getch();
if(c=='n'||c=='N')
return;
else
closer();
}
fflush(stdin);
printf("\nEnter the file name you want to open :");
scanf("%s",file);
getchar();
fflush(stdin);
clearlist();
fp=fopen(file,"r"); //opening the specified file
if(fp==NULL) //checking if the file previously exists
{
printf("\nThe file doesnot exist do you want to create one?(Y/N) :");
c=getchar();
getchar();
if(c=='N'||c=='n')
return;
else
{
fp=fopen(file,"w"); //creating new file
edit();
return;
}
}
if(feof(fp))
return;
while((fgets(buf,201,fp))!=NULL) //taking input from file
{
addnode(buf,buff);
buff=buff->next;
}
edit(); //calling the edit function
}
void edit(void) //the edit function
{
struct dll *temp=head->next; //pionter used to mark the current position during traversing
char c,d;
system("clear"); //clearing the screen
if(fp==NULL) //checking for files previously open
{
printf("\nNo file is open\n");
return;
}
printf("\nThe file contents will be displayed along with the line number\npress any key\n");
getch();
system("clear");
printlist(); //printing the entire buffered list
if(temp!=NULL)
printf("You are at line number %d",temp->index); //printing the line number of control
else
temp=head;
editcommands(); //prints the list of commands available
while(1) //infinite loop for multiple command usage
{
c=getch();
switch(c) //switch -->condition checkig
{
case 'c' :
case 'C' :
editnode(temp); //edit the current line pointed to by temp
break;
case 'p' :
case 'P' : //move to the previous line
if(temp==head)
{
printf("\nFile empty"); //message displayed if list is empty
break;
}
if(temp->prev!=head)
{
temp=temp->prev;
printf("\nYou are at line number %d",temp->index);
}
else //message display if already at first line
printf("\nalready at first line");
break;
case 'n' :
case 'N' : //move to the next line
if(temp->next!=NULL)
{
temp=temp->next;
printf("\nYou are at line number %d",temp->index);
}
else if(temp==head)
printf("\nFile empty"); //message printed if list is empty
else
printf("\nalready at last line");//message printed if already at last line
break;
case 'a' :
case 'A' : //adding a new line after node ponted by temp
addline(temp); //addline function takes input and creates a new node via addnode function
temp=temp->next;
printlist();
printf("\nYou are at line number %d",temp->index);
break;
case 'h' :
case 'H' : //HELP command displays the list of available commmands
system("clear");
editcommands(); //notice that there is no break as after help the entire list is printed
system("clear");
case 'v' :
case 'V' : //printing the entire list via printlist function
printlist();
printf("\nYou are at line number %d",temp->index);
break;
case 'D' :
case 'd' : //deleting a line pointed to by temp
if(temp==head) //checking if list is empty
{
printf("\nFile empty\n");
break;
}
temp=temp->prev;
delnode(temp->next); //deleting the node
printf("\nLine deleted\n");
printlist(); //printing the list
if(temp->index)
printf("\nYou are currently at line number %d",temp->index);
if(((temp->prev)==NULL)&&(temp->next)!=NULL)
temp=temp->next;
else if((temp==head)&&((temp->next)==NULL))
printf("\nFile empty"); //printing message if list is empty
break;
case 'X' :
case 'x' : //exit the editor to main menu
printf("\nDo you want to save the file before exiting?(y/n) :");
d=getch(); //warning for saving before exit
if(d=='y'||d=='Y')
save();
closer();
return;
break;
case 's' :
case 'S' : //saving and exitting
save();
closer();
return;
break;
}
}
}
void addline(struct dll *temp) //adding a new line via input from user
{
char buff[200];
printf("\nenter the new line :\n");
gets(buff); //taking the new line
addnode(buff,temp); //ceatng the new node
}
void save(void) //function to save the file
{
struct dll *temp=head->next;
fclose(fp);
fp=fopen(file,"w"); //opening the file in write mode
while(temp!=NULL)
{
fprintf(fp,"%s",temp->s); //writing the linked list contents to file
temp=temp->next;
}
}
void editcommands(void) //function to print the list of editer commands
{
printf("\n\n\n Editor commands\n");
printf("H :show the list of commands\n\n");
printf("C :edit the current line\n");
printf("P :move one line up\n");
printf("N :move one line down\n");
printf("D :delete the current line\n\n");
printf("V :display the contents of the open file\n");
printf("A :add a line after the line at which you are at\n");
printf("S :save changes and exit to main menu\n\n");
printf("X :exit discarding any changes\n");
getch();
}