Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NUMBERLINES 1000 // max number of lines that can be read
#define LINELENGTH 1000 // max length of a line
#define FNAMELENGTH 100 // max length of a file name
#define CMDLENGTH 102 // max length of a command (e.g.'f' + ' ' + filename)
typedef struct Node {
char line[LINELENGTH];
struct Node *next;
} NodeT;
void ScanInput(FILE *, char*);
void PrintCommand(void);
void PrintSpace(void);
void PrintAll(NodeT *);
NodeT *ReadFile(FILE *);
void Unknown(void);
void PrintCur(NodeT *);
NodeT *makeNode(char *);
//NodeT *GotoB(NodeT* );
//void Append(FILE *, char*);
int main(int argc, char **argv) {
FILE *fp;
fp = NULL;
if(argc > 2){
printf("Usage: ./led [filename]\n");
exit(1);
}
if(argv[1] != NULL){
if((fp = fopen(argv[1],"r")) != NULL) {
}
else {
fp = fopen(argv[1],"w");
printf("Creating file %s\n", argv[1]);
}
}
while(1) ScanInput(fp, argv[1]);
return 0;
}
void ScanInput(FILE *s, char *argv){
char input[CMDLENGTH];
char filename[FNAMELENGTH];
NodeT *head = NULL;
NodeT *cur = NULL;
strcpy(filename,argv);
if( s!= NULL){
head = ReadFile(s);
cur = head;
}
printf("? ");
scanf("%s",input);
if(strcmp(input,"h") == 0){
PrintCommand();
}
if(strcmp(input,"q") == 0){
exit(1);
}
if(strcmp(input,"p") == 0){
// cur = GotoB(head);
PrintAll(head);
}
if(strcmp(input,"x") == 0){
printf("Forced exit, changes not saved\n");
exit(1);
}
if(strcmp(input,".") == 0){
PrintCur(cur);
}
/*if(strcmp(input,"-") == 0){
DecP();
}
if((strcmp(input,"+") == 0) || (strcmp(input,"\n") == 0)){
IncP();
}
if(strcmp(input,"a") == 0){
Append(s, filename);
}
if(strcmp(input,"f") == 0){
if(scanf("%s",filename) != 0) {
Unknown();
}
if((fp1 = fopen(filename,"r")) != NULL){
fclose(fp1);
printf("File %s already exist, saving will overwrite", filename);
}
else {
printf("Creating file :%s\n", filename);
fp1 = fopen(filename,"w");
}
}*/
}
/*NodeT *GotoB(NodeT* head){
NodeT *tmp;
tmp = head;
if(tmp == NULL){
return NULL;
}
while(tmp->next != NULL){
tmp = tmp -> next;
}
return tmp;
}*/
void PrintCur(NodeT *cur){
if(cur == NULL) return ;
printf("%s",cur->line);
}
void PrintCommand(){
printf("Commands are (in upper or lower case):\n");
PrintSpace();
printf("q: quit\n");
PrintSpace();
printf("s: save\n");
PrintSpace();
printf("x: force exit\n");
PrintSpace();
printf("f <filename>: the file is called <filename>\n");
PrintSpace();
printf("h: print this help message\n");
PrintSpace();
printf("d: delete current line\n");
PrintSpace();
printf("a: append after current line, terminated by '.'\n");
PrintSpace();
printf("i: insert before current line, terminated by '.'\n");
PrintSpace();
printf("p: print all lines\n");
PrintSpace();
printf(".: print current line\n");
PrintSpace();
printf("+: increment line and print\n");
PrintSpace();
printf("<return>: same as '+'\n");
PrintSpace();
printf("-: decrement line and print\n");
PrintSpace();
printf("number: make 'number' the current line\n");
}
void PrintSpace(){
printf(" ");
}
void Unknown(){
printf("Unknown command: ignoring\n");
}
void PrintAll (NodeT *s) {
NodeT *cur;
int line = 1;
cur = s;
while(cur != NULL) {
printf("LINE %d %s", line, cur->line);
cur = cur->next;
line++;
}
}
NodeT *ReadFile(FILE *fp){
int i = 1;
char line [LINELENGTH ];
char *get;
NodeT *head;
NodeT *cur;
NodeT *new;
if(fp == NULL){
printf("Empty File\n");
return NULL;
}
if((get = fgets(line, LINELENGTH , fp)) != NULL) {
head = makeNode(line);
cur = head;
}
while (fgets(line, LINELENGTH , fp) != NULL) {
new = makeNode(line);
cur->next = new;
cur = new;
i++;
}
if((i == 1) && (get == NULL)) {
printf("Empty file");
return NULL;
}
fclose(fp);
return head;
}
/*void Append(FILE *fp, char *filename){
int i = 1;
char *input = NULL;
NodeT *temp;
fp = fopen(filename,"w+");
while(strcmp(input,".") != 0) {
scanf("%s",input);
*/
NodeT *makeNode(char *line) {
NodeT *new;
new = malloc(sizeof(NodeT));
if (new == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1); // program should terminate
}
strcpy(new->line,line);
new->next = NULL;
return new;
}