From 4f8851bafd03b2a15e4204ea4df3f9cad395b93b Mon Sep 17 00:00:00 2001 From: Mazar Farran Date: Thu, 25 Sep 2014 00:05:20 -0700 Subject: [PATCH] Refactored: created move functions, separate files for line and page structs, and added a makefile --- line.c | 37 +++++++++++++ line.h | 18 +++++++ makefile | 19 +++++++ page.c | 71 +++++++++++++++++++++++++ page.h | 24 +++++++++ text.c | 154 ++++++++++++++++++------------------------------------- text.h | 36 ++++--------- 7 files changed, 231 insertions(+), 128 deletions(-) create mode 100644 line.c create mode 100644 line.h create mode 100644 makefile create mode 100644 page.c create mode 100644 page.h diff --git a/line.c b/line.c new file mode 100644 index 0000000..81690c0 --- /dev/null +++ b/line.c @@ -0,0 +1,37 @@ +#include "line.h" + +// Insert char into string. +void insert_char(LINE *s, char c, int index) +{ + int i; + + if(strlen(s->line) >= s->size - 2) expand(s); + + for(i = strlen(s->line); i >= index; i--) + s->line[i + 1] = s->line[i]; + + s->line[index] = c; +} // insert + + + +void remove_char(LINE *s, int index) +{ + int i; + int len = strlen(s->line); + for(i = index; i < len; i++) + s->line[i] = s->line[i + 1]; +} // remove_char + + + +// expands size of line +void expand(LINE *s) +{ + int new_size = s->size * 2; + char *temp = (char *)malloc(new_size * sizeof(char)); + strcpy(temp, s->line); + free(s->line); + s->line = temp; + s->size = new_size; +} // expand diff --git a/line.h b/line.h new file mode 100644 index 0000000..8bfa9bb --- /dev/null +++ b/line.h @@ -0,0 +1,18 @@ +#ifndef LINE_H +#define LINE_H +/* LINE struct and functions */ + +#include +#include + +typedef struct +{ + char *line; + int size; // size of array, not string +} LINE; + +void insert_char(LINE *s, char c, int index); // inserts char to string +void remove_char(LINE *s, int index); +void expand(LINE *s); // doubles the size of the line + +#endif diff --git a/makefile b/makefile new file mode 100644 index 0000000..fd52ff2 --- /dev/null +++ b/makefile @@ -0,0 +1,19 @@ +# makefile for text.c + +CXX=gcc +CFLAGS=-Wall -g +OBJS=line.o page.o +LIBS=-lncurses + +text: text.c text.h $(OBJS) + $(CXX) $(CFLAGS) -o text text.c $(OBJS) $(LIBS) + +line.o: line.c line.h + $(CXX) $(CFLAGS) -c line.c + +page.o: page.c page.h line.h + $(CXX) $(CFLAGS) -c page.c + +clean: + rm -f $(OBJS) text + diff --git a/page.c b/page.c new file mode 100644 index 0000000..7369a62 --- /dev/null +++ b/page.c @@ -0,0 +1,71 @@ +#include "page.h" + +void init_page(PAGE *p) +{ + p->text = (LINE *)malloc(PAGE_SIZE * sizeof(LINE)); + + int i; + for(i = 0; i < PAGE_SIZE; i++) + { + p->text[i].line = (char *)malloc(LINE_SIZE * sizeof(char)); + p->text[i].size = LINE_SIZE; + } + p->numlines = 0; +} // init_page + +void dest_page(PAGE *p) +{ + int i; + for(i = 0; i < p->numlines; i++) + { + free(p->text[i].line); + } + free(p->text); +} // dest_page + + + +// WARNING: Will not expand once the limit of 500 lines reached +void insert_line(PAGE *p, int index) +{ + LINE newline; + newline.line = (char *)malloc(LINE_SIZE * sizeof(char)); + newline.line[0] = '\0'; + + int i; + + for(i = p->numlines - 1; i >= index; i--) + p->text[i + 1] = p->text[i]; + + p->text[index] = newline; +} // insert_line + + + +void remove_line(PAGE *p, int index) +{ + if( p->numlines > 0 ) + { + free(p->text[index].line); + + int i; + for(i = index; i < p->numlines - 1; i++) + { + p->text[i] = p->text[i + 1]; + } + (p->numlines)--; + } +} // remove_line + +void print_page(const PAGE *p, int start, int end) +{ + int i; + for(i = start; i < p->numlines && i < end; i++) + { + move(i, 1); + clrtoeol(); + printw("%s", p->text[i].line); + } + refresh(); +} + diff --git a/page.h b/page.h new file mode 100644 index 0000000..ed99627 --- /dev/null +++ b/page.h @@ -0,0 +1,24 @@ +#ifndef PAGE_H +#define PAGE_H +/* PAGE struct definition and related functions */ + +#include // might have to move this +#include "line.h" + +#define PAGE_SIZE 500 /* Max number of lines. Currently not expandable. */ +#define LINE_SIZE 128 /* Max characters in a line. Expandable */ +#define WIN_SIZE LINES - 2 /* Size of window, making room for bottom prompt */ + +typedef struct +{ + LINE *text; // lines of text + int numlines; +} PAGE; + +void init_page(PAGE *p); +void dest_page(PAGE *p); +void insert_line(PAGE *p, int index); +void remove_line(PAGE *p, int index); +void print_page(const PAGE *p, int start, int end); + +#endif diff --git a/text.c b/text.c index 5526a05..e3c21bd 100644 --- a/text.c +++ b/text.c @@ -7,6 +7,8 @@ * -Saving * -Loading **/ + + // TODO: Create move_up, down, left, right functions int main(int argc, char *argv[]) { @@ -17,7 +19,12 @@ int main(int argc, char *argv[]) { load_file(argc, argv, &page); } - + else // initialize + { + page.text[0].line[0] = '\0'; + page.numlines = 1; + } + /* curses interface */ initscr(); noecho(); @@ -31,6 +38,7 @@ int main(int argc, char *argv[]) int y, x; getyx(stdscr, y, x); + //int yoffset = 0; // offset to account for screen scroll while(true) { @@ -40,25 +48,20 @@ int main(int argc, char *argv[]) case KEY_F(4): goto end; break; + case KEY_F(5): + save_file(argc, argv, &page); + break; case KEY_UP: - if( y > 0 ) --y; - if( x > strlen(page.text[y].line) + 1 ) // cursor adjusts to smaller - x = strlen(page.text[y].line) + 1; // lines - move(y, x); + move_up(&page, &x, &y); break; case KEY_DOWN: - if( y < WIN_SIZE ) ++y; - if( x > strlen(page.text[y].line) + 1 ) - x = strlen(page.text[y].line) + 1; - move(y, x); + move_down(&page, &x, &y); break; case KEY_LEFT: - if(x - 1 > 0) - move(y, --x); + move_left(&x, &y); break; case KEY_RIGHT: - if(x <= strlen(page.text[y].line)) - move(y, ++x); + move_right(&page, &x, &y); break; case KEY_DC: case 127: // backspace key... @@ -66,26 +69,27 @@ int main(int argc, char *argv[]) if(page.text[y].line[x - 2] == '\0') { remove_line(&page, y); - if( y > 0 ) --y; - if( x > strlen(page.text[y].line) + 1 ) - x = strlen(page.text[y].line) + 1; + move_up(&page, &x, &y); } else { remove_char(&page.text[y], x - 2); // why 2? - if( x > 0 ) x--; + move_left(&x, &y); } print_page(&page, 0, WIN_SIZE); move(y, x); - refresh(); + break; + case '\n': // newline + insert_line(&page, y + 1); + print_page(&page, 0, WIN_SIZE); + move_down(&page, &x, &y); break; default: // all other chars if( isprint(ch) ) { - insert(&page.text[y], ch, x - 1); + insert_char(&page.text[y], ch, x - 1); print_page(&page, 0, WIN_SIZE); - move(y, ++x); - refresh(); + move_right(&page, &x, &y); } } } @@ -96,6 +100,31 @@ end: endwin(); return EXIT_SUCCESS; } // main +void move_left(int *x, int *y) +{ + if(*x - 1 > 0) move(*y, --(*x)); +} + + +void move_right(PAGE *p, int *x, int *y) +{ + if(*x <= strlen(p->text[*y].line)) move(*y, ++(*x)); +} + +void move_up(PAGE *p, int *x, int *y) +{ + if( *y > 0 ) --(*y); + if( *x > strlen(p->text[*y].line) + 1 ) // cursor adjusts + *x = strlen(p->text[*y].line) + 1; // to smaller lines + move(*y, *x); +} +void move_down(PAGE *p, int *x, int *y) +{ + if( *y < WIN_SIZE ) ++(*y); + if( *x > strlen(p->text[*y].line) + 1 ) + *x = strlen(p->text[*y].line) + 1; + move(*y, *x); +}; void load_file(int argc, char **argv, PAGE *p) { @@ -128,6 +157,7 @@ void save_file(int argc, char **argv, PAGE *p) while(p->text[line].line[col] != '\0') { fputc(p->text[line].line[col], fp); + col++; } fputc('\n', fp); } @@ -136,85 +166,3 @@ void save_file(int argc, char **argv, PAGE *p) fclose(fp); } // save_file - -void init_page(PAGE *p) -{ - p->text = (LINE *)malloc(PAGE_SIZE * sizeof(LINE)); - - int i; - for(i = 0; i < PAGE_SIZE; i++) - { - p->text[i].line = (char *)malloc(LINE_SIZE * sizeof(char)); - p->text[i].size = LINE_SIZE; - } - p->numlines = 0; -} // init_page - -void dest_page(PAGE *p) -{ - int i; - for(i = 0; i < p->numlines; i++) - { - free(p->text[i].line); - } - free(p->text); -} // dest_page - -// Insert char into string. -void insert(LINE *s, char c, int index) -{ - int i; - - if(strlen(s->line) >= s->size - 2) expand(s); - - for(i = strlen(s->line); i >= index; i--) - s->line[i + 1] = s->line[i]; - - s->line[index] = c; -} // insert - -// removes a char from line, if it's the last char '\0' -// then it calls remove_line -void remove_char(LINE *s, int index) -{ - //if(s->line[index] != '\0') - //{ - int i; - int len = strlen(s->line); - for(i = index; i < len; i++) - s->line[i] = s->line[i + 1]; - //} -} // remove_char - -// expands size of line -void expand(LINE *s) -{ - int new_size = s->size * 2; - char *temp = (char *)malloc(new_size * sizeof(char)); - strcpy(temp, s->line); - free(s->line); - s->line = temp; - s->size = new_size; -} // expand - -void remove_line(PAGE *p, int index) -{ - free(p->text[index].line); - int i; - for(i = index; i < p->numlines - 1; i++) - { - p->text[i] = p->text[i + 1]; - } - (p->numlines)--; -} // remove_line - -void print_page(const PAGE *p, int start, int end) -{ - int i; - for(i = start; i < p->numlines && i < end; i++) - { - move(i, 1); - clrtoeol(); - printw("%s ", p->text[i].line); - } -} diff --git a/text.h b/text.h index 748d403..630ebba 100644 --- a/text.h +++ b/text.h @@ -2,35 +2,21 @@ #define TEXT_H #include -#include +#include // declared in line.h #include -#include -#include // -lncurses +#include // declared in line.h +#include // -lncurses. declared in page.h -#define PAGE_SIZE 500 -#define LINE_SIZE 128 -#define WIN_SIZE LINES - 2 - -typedef struct -{ - char *line; - int size; -} LINE; - -typedef struct -{ - LINE *text; // lines of text - int numlines; -} PAGE; +#include "page.h" void load_file(int argc, char **argv, PAGE *p); void save_file(int argc, char **argv, PAGE *p); -void init_page(PAGE *p); -void dest_page(PAGE *p); -void insert(LINE *s, char c, int index); // inserts to string -void remove_char(LINE *s, int index); -void remove_line(PAGE *p, int index); -void expand(LINE *s); -void print_page(const PAGE *p, int start, int end); + +void move_left(int *x, int *y); +void move_right(PAGE *p, int *x, int *y); +void move_up(PAGE *p, int *x, int *y); +void move_down(PAGE *p, int *x, int *y); + #endif +