Você está na página 1de 6

////////////////////////////////// ///////////////////// #include <stdio.h> #include <stdlib.h> #include <string.

h> int gn = 0; char *cp;

buffer.c

////////

struct line { char text[200]; struct line *prior; struct line *next; }; struct line *current; struct line *start= NULL; /* pointer to first entry in list */ struct line *last= NULL; /* pointer to last entry */

struct line *prevl(struct line *i) /* item to delete */ { if(i->prior) i = i->prior; printf (i->text); printf("\n\n"); return i; } struct struct struct struct struct { line line line line line *appl( *i, /* new element */ **start, /* first element in list */ **last, /* last element in list */ *p)

if(*last==NULL) { /* first element in list */ i->next = NULL; i->prior = NULL; *last = i; *start = i; return *start; } // if there exists a next element if(p->next) { p->next->prior = i; i->next = p->next; p->next = i; i->prior = p; return i; } p->next = i; /* new last element if there is no last item*/ i->prior = p; i->next = NULL; *last = i; return *last; }

/*This function internally calls append function*/ struct line *telins(struct line *in) { char lstr[200]; struct line *info; int cnt = 0,t; for(;;) { info = (struct line *)malloc(sizeof(struct line)); t = cnt; if(!info) { printf("\n? out of memory\n"); return in; } gets(lstr); printf("\n"); if(!lstr[0]) return in; strcpy(info->text,lstr);/* remove newline character */ if(!info->text[0]) break; /* stop entering */ while(t) { t--; if(!in->next) break;//To accomodate 2 nd multiple lines at fir st write in = in->next ;//continue appending till he presses enter } in = appl(info, &start, &last, in); cnt++; if(!in) in = start;//To accomodate multiple lines at first write } } struct struct struct struct ){ line line line line *insertl( *i, /* new element */ **start, /* first element in list */ **last /* last element in list */

struct line *p, *info, *old; int cnt = 0, t; char lstr[200]; if(!(*start)) i = telins(i); for(;;) { info = (struct line *)malloc(sizeof(struct line)); t = cnt; if(!info) { printf("\n? out of memory\n"); return i; } gets(lstr); printf("\n"); if(!lstr[0]) return old;

strcpy(info->text,lstr); if(!info->text[0]) break; /* stop entering */ (*start)->prior = NULL; p = *start; /* start at top of list */ if(p){ while(p != i) p = p->next;// reach until the current line before which you ins ert if(p->prior) {// if this is a line anywhere after the first line p->prior->next = info; info->next = p; info->prior = p->prior; p->prior = info; goto last; } info->next = p; /* new first element */ info->prior = NULL; p->prior = info; *start = info; last: cnt++; old = info; } } } struct struct struct struct { line line line line *dell( *i, /* item to delete */ **start, /* first item */ **last) /* last item */

printf("\n? Line ''%s '' deleted\n\n",i->text); if(i->prior) i->prior->next = i->next; else { /* new first item */ if(!i->next && !i->prior){ *start = *last = NULL;// to delete the only line return NULL; } *start = i->next; i->next->prior = *start; if(*start) (*start)->prior = NULL; return i->next; } if(i->next){ i->next->prior = i->prior; return i->next; } else /* deleting last element */ *last = i->prior; if(*last) (*last)->next = NULL; return *last; }

void writef(char *filename)// save the lines to the file { int cnt1=0; struct line *info; FILE *fp; fp = fopen(filename, "w"); if(!fp) { printf("Cannot open file.\n"); exit(1); } info = start; while(info) { fwrite(info, sizeof(struct line), 1, fp); info = info->next; /* get next address */ cnt1++; } printf("\n%d Lines written\n\n",cnt1); fclose(fp); }

struct line *readf(char *filename)// load the lines from file { int cnt1=0; struct line *info; FILE *fp; fp = fopen(filename, "r"); if(!fp) { printf("Cannot open file.\n"); exit (1); } /* free any previously allocated memory */ while(start) { info = start->next; free (start); start = info; } /* reset top and bottom pointers */ start = last = NULL; while(!feof(fp)) { info = (struct line *) malloc(sizeof(struct line)); if(!info) { printf("Out of Memory"); return; } if(1 != fread(info, sizeof(struct line), 1, fp)) break; cnt1++; if(!start){ start = info; /* is first item in list */ start->next = NULL; start->prior = NULL; last = start; } else last->next = info; info->next = NULL; info->prior = last;

last = info; } printf("\n%d Lines Read\n\n",cnt1); fclose (fp); return start; } struct line *tell(struct line *i) { int c = 0; if(i){ printf(i->text); c++; } if(i) printf("\n\n"); if(i) if(i->next) i = i->next; return i; } void processc(char *str){ if(start) (start)->prior = NULL; else current = start; switch(str[0]) { case 'r': str+=2; current = readf(str); break; case 'w': str+=2; case 't': case 'i': case '-': case 'a': case 'd': case 'q': default:; } } writef(str); break; current = tell(current); break; current = insertl(current,&start,&last); break; current = prevl(current); break; current = telins(current); break; if(current->prior) current = current->prior; current = dell(current, &start, &last); break; exit(1);

/////////////////////////// /////////////////// #include <stdio.h> #include <stdlib.h> #include <string.h> char str[20]; void *readc() {

main.c

////////////////

char c = str[0]; printf("? "); gets(str); if(!str[0] && c == 't') str[0] = 't'; } int main(void) { printf("Text Editor v1.0\n\n"); while(1) { readc(); processc(str); }return 0; }

Você também pode gostar