My Elite Text Editor in C
Here is a full working text editor I programmed myself in C in university. This was made in unix, but you can compile it in windows, you can use it for your own use / learning whatever.
Also I have tons of more C and C++ projects I could add to the code bank so if anyone wants any or to learn I have alot to offer
By: Chris Johansson
#include <stdlib.h> #include <string.h> #include <stdio.h>
struct line {
char *text;
struct line *prev;
struct line *next;
};
struct line *current_line=NULL, *bookmark=NULL;
static int linesread=0;
struct line* get_line(FILE *f);
char *read_command();
int process_command(char *);
int read_file(char *);
int write_file(char *);
void next_line(void);
void previous_line(void);
void cur_line(void);
void delete_line(void); void insert_line(void); void append_line(void); void free_space(void);
/————— MAIN —————/ int main (void) { char *command; int ret=0;
while(ret==0) { printf("\n? "); fflush(stdin); command = read_command(); ret=process_command(command);
if (ret!=0)
{
printf("Invalid command.\n");
getchar();
exit(ret);
}
} return 0; } /————— READ COMMAND —————/ char *read_command(){ char *command; int i = 1;
command = (char *) malloc(sizeof(char));
while ((command[i-1] = getchar()) != '\n') { ++i; command = (char ) realloc(command, (sizeof(char)) * i ); if ((realloc(command, (sizeof(char)) * i))==NULL)break; } command[i-1] = '\0'; return command; } /————— PROCESS COMMAND —————*/ int process_command(char *command) { char *array[50]; int ret=0, i, writelines;
array[0]=strtok(command, " ");
for(i=1; i>0; i++){ array[i]=strtok(NULL, " "); if(array[i]==NULL) i=(-1); }
if (command[0] == NULL)command[0]='+';
switch(command[0]) { case 'r': free_space(); ret = read_file(array[1]); if (ret != 0)exit(ret); else printf("%d lines were read", linesread+1); break;
case 'w':
writelines=write_file(array[1]);
printf("%d lines written to %s", writelines, array[1]);
break;
case 't': cur_line();break;
case '+': next_line();break;
case '-': previous_line();break;
case 'd': delete_line();break;
case 'i': insert_line();break;
case 'a': append_line();break;
case 'q':
free_space();
exit(0);
break;
default: ret=1;
} return ret; } /————— READ FILE —————/ int read_file(char *filename) { FILE *in_file; in_file = fopen(filename, "r");
if (!in_file){ printf("Error Accessing Filename\n"); return 1; } current_line = get_line(in_file); while(current_line->prev->prev!=NULL){current_line=current_line->prev;}
bookmark=current_line; fclose(in_file); return 0; } /————— WRITE FILE —————/ int write_file(char *filename ) { FILE *out_file; int flag=0, writelines=0;
out_file = fopen(filename, "w"); while(current_line->prev->prev!=NULL){current_line=current_line->prev;}
while(flag==0) { if (current_line==NULL)break;
fputs(current_line->text, out_file);
if(current_line->next != NULL)fputc('\n', out_file);
++writelines;
current_line=current_line->next;
} current_line=bookmark; fclose(out_file); return writelines; } /————— GET LINE —————/ struct line *get_line(FILE in_file) { struct line temp = NULL; int m = 1;
temp=(struct line *) malloc (sizeof(struct line)); temp->text=(char *) malloc(sizeof(char));
temp->next=NULL;
current_line = (struct line * ) malloc(sizeof(struct line)); current_line->text=(char *) malloc(sizeof(char));
linesread=0;
while ((temp->text[m-1] = fgetc(in_file)) != EOF) { if(temp->text[m-1]=='\n' || temp->text[m-1]=='\r') { temp->text[m-1] = '\0';
current_line->next=temp;
temp->prev=current_line;
current_line=temp;
temp = (struct line *) malloc(sizeof(struct line));
temp->text=(char *) malloc(sizeof(char));
m=1;
++linesread;
continue;
}
++m;
temp->text = (char *) realloc(temp->text, (sizeof(char)) * m );
if ((realloc(temp->text, (sizeof(char)) * m))==NULL)break;
} temp->text[m-1]='\0'; current_line->next=temp; temp->prev=current_line; current_line=temp;
return current_line; } /————— CUR LINE —————/ void cur_line(void) { if (current_line==NULL)printf("Illegal operation: no file has been read in."); else{ printf("%s",current_line->text); bookmark=current_line; } return; } /————— NEXT LINE —————/ void next_line(void) { if (current_line==NULL)printf("Illegal operation: no file has been read in."); else if(current_line->next!= NULL){ current_line=current_line->next; printf("%s", current_line->text); bookmark=current_line; } else printf("Illegal operation: the current line is at end of file."); return; } /————— PREVIOUS LINE —————/ void previous_line(void) { if (current_line==NULL)printf("Illegal operation: no file has been read in."); else if(current_line->prev->prev != NULL){ current_line=current_line->prev; printf("%s", current_line->text); bookmark=current_line; } else printf("Illegal operation: the current line is at the begining of the file."); return; } /————— DELETE LINE —————/ void delete_line(void) { struct line *temp = NULL;
if (current_line == NULL){
printf("Illegal operation: no file has been read in.");
return;
} temp = current_line;
if(current_line->next != NULL){
current_line=current_line->prev;
current_line->next=temp->next;
current_line = temp->next;
current_line->prev = temp->prev;
bookmark = current_line;
} else if (current_line->prev->prev != NULL){ current_line = temp->prev; current_line->next = NULL; bookmark = current_line; } else{ current_line = temp->prev; current_line->next = NULL; current_line = NULL; } temp->next = NULL; temp->prev = NULL; free (temp); return; } /————— APPEND LINE —————/ void append_line(void) { int m=1; char c; struct line *temp=NULL, *swap=NULL;
if (current_line == NULL){ printf("Illegal operation: no file has been read in."); return; }
temp=(struct line *) malloc (sizeof(struct line));
temp->text=(char *) malloc(sizeof(char));
while (c=getchar())
{
if(c==EOF||c=='\n'||c=='\r')break;
temp->text[m-1]=c;
++m;
temp->text = (char *) realloc(temp->text, (sizeof(char)) * m );
if ((realloc(temp->text, (sizeof(char)) * m))==NULL)break;
}
temp->text[m-1]='\0';
if(current_line->next == NULL){ current_line->next = temp; temp->prev = current_line; } else{ swap = current_line->next; current_line->next = temp; temp->prev = current_line; temp->next = swap; swap->prev = temp; } current_line = temp; bookmark = current_line;
return; } /————— INSERT LINE —————/ void insert_line(void) { int m = 1; char c; struct line *temp = NULL, *swap = NULL;
if (current_line == NULL){ printf("Illegal operation: no file has been read in."); return; }
temp=(struct line *) malloc (sizeof(struct line)); temp->text=(char *) malloc(sizeof(char));
while (c=getchar())
{
if(c==EOF||c=='\n'||c=='\r')break;
else
{
temp->text[m-1]=c;
++m;
temp->text = (char *) realloc(temp->text, (sizeof(char)) * m );
if ((realloc(temp->text, (sizeof(char)) * m))==NULL)break;
}
}
temp->text[m-1]='\0';
swap = current_line->prev; swap->next = temp; temp->prev = swap; temp->next = current_line; current_line->prev = temp;
current_line = temp; bookmark = current_line;
return; } /————— FREE SPACE —————/ void free_space(void) { struct line *garbage = NULL;
if(current_line != NULL) { while(current_line->next != NULL){ current_line = current_line->next; }
while(current_line->prev != NULL){
garbage = current_line->prev;
current_line->next = NULL;
current_line->prev = NULL;
free (current_line);
current_line = garbage;
}
current_line->next = NULL;
garbage->next = NULL;
bookmark->next= NULL;
bookmark->prev= NULL;
free (current_line);
free (garbage);
free (bookmark);
}
return; }