Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

My Elite Text Editor in C


ghost's Avatar
0 0

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(&quot;Invalid command.&#92;n&quot;);
     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 &#39;w&#39;:
  	writelines=write_file(array[1]);
 		printf(&quot;%d lines written to %s&quot;, writelines, array[1]);
 	break;
                   
  case &#39;t&#39;: cur_line();break;
  case &#39;+&#39;: next_line();break;
  case &#39;-&#39;: previous_line();break;
  case &#39;d&#39;: delete_line();break;
  case &#39;i&#39;: insert_line();break;
  case &#39;a&#39;: append_line();break;
  case &#39;q&#39;:
  	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-&gt;text, out_file);
if(current_line-&gt;next != NULL)fputc(&#39;&#92;n&#39;, out_file);
  ++writelines;
  current_line=current_line-&gt;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-&gt;next=temp;
     temp-&gt;prev=current_line;
 		current_line=temp;

 		temp = (struct line *) malloc(sizeof(struct line));
     temp-&gt;text=(char *) malloc(sizeof(char));
 		m=1;
 		++linesread;

 		continue;
  }
  ++m;
  temp-&gt;text = (char *) realloc(temp-&gt;text, (sizeof(char)) * m );
  if ((realloc(temp-&gt;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(&quot;Illegal operation: no file has been read in.&quot;);
  return;

} temp = current_line;

if(current_line-&gt;next != NULL){
  current_line=current_line-&gt;prev;
  current_line-&gt;next=temp-&gt;next;
  current_line = temp-&gt;next;
  current_line-&gt;prev = temp-&gt;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-&gt;text=(char *) malloc(sizeof(char));

while (c=getchar())
{
  if(c==EOF||c==&#39;&#92;n&#39;||c==&#39;&#92;r&#39;)break;
  temp-&gt;text[m-1]=c;
 	++m;
 	temp-&gt;text = (char *) realloc(temp-&gt;text, (sizeof(char)) * m );
 	if ((realloc(temp-&gt;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==&#39;&#92;n&#39;||c==&#39;&#92;r&#39;)break;
  else
  {
  	temp-&gt;text[m-1]=c;
 		++m;
 		temp-&gt;text = (char *) realloc(temp-&gt;text, (sizeof(char)) * m );
 		if ((realloc(temp-&gt;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-&gt;prev != NULL){
	garbage = current_line-&gt;prev;
     current_line-&gt;next = NULL;
     current_line-&gt;prev = NULL;
  	free (current_line);
  	current_line = garbage;
}
  current_line-&gt;next = NULL;
  garbage-&gt;next = NULL;
  bookmark-&gt;next= NULL;
  bookmark-&gt;prev= NULL;

 	free (current_line);
  free (garbage);
free (bookmark);
}

return; }


rockbll4's Avatar
[TheN00bHacker]
0 0

would it work with windows?


ghost's Avatar
0 0

Actually ya it does compile in windows, i just compiled it in Visual C++ .net lol… I never program in that though

Also I haven't added any comments yet on how to use it so u will have to figure it out using the code for now.

btw thanks ac1d


ghost's Avatar
0 0

Hey by the way what do you guys think of it? Its more advanced but its a bit of everything, structs, memory allocation, linked lists, reading from files… etc


Uber0n's Avatar
Member
0 0

Looks great, but why post it in the forum and not in the code bank? :right:


lukem_95's Avatar
Member
0 0

nice code, and it is in the code bank aswell…

why dont you make a gui version… actually i think i did one somewhere, not quite as advanced (:$ actually pretty simple)


ghost's Avatar
0 0

i havent fully read the source code but it looks pretty good to me lol :P. just wondering can it write hex values as characters as in if i entered \x41 would it write a capital A? :)