From 4de07233cd7b391dc100b5f6af259ef945589ccb Mon Sep 17 00:00:00 2001 From: Mazar Farran Date: Fri, 28 Aug 2015 21:53:20 -0700 Subject: [PATCH] updated readme, improved load and save --- README.md | 11 +++-------- text.c | 29 ++++++++++++++++------------- text.h | 6 +++--- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5961d6b..4f889df 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,6 @@ Probably the most well-developed program I made this summer. ##Current issues: -x-y coordinates in ncurses correspond to rows and columns in -the text document. This means tabs are an issue and text -wrapping is impossible. Solution: separate screen logic -from document logic. - -Can only save files under the name save.txt. Solution: create -a prompt window (from scratch...) that retuns a string to the -caller. (Done!) +* Code could be organized better +* Some arguments/variables don't do anything +* Need to add copy/paste/undo. diff --git a/text.c b/text.c index 49f2a05..6b8e21f 100644 --- a/text.c +++ b/text.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) if(argc > 1) { - load_file(argc, argv, &page); + load_file(&page, argv[1]); } else // initialize { @@ -70,13 +70,13 @@ int main(int argc, char *argv[]) print_page(&page, beg, end); break; case KEY_F(5): - save_file(argc, argv, &page); + save_file(&page); sprintf(status, "Saved as \'%s\'", page.filename); update_status(status); break; case KEY_F(6): prompt_string("Save As:", page.filename, NAME_LIMIT); - save_file(argc, argv, &page); + save_file(&page); sprintf(status, "Saved as \'%s\'", page.filename); print_page(&page, beg, end); update_status(status); @@ -204,27 +204,30 @@ void move_down(PAGE *p, int *x, int *y) } /* movement */ -int count_lines(int argc, char **argv) +int count_lines(FILE *fp) { - FILE *fp = fopen(argv[1], "r"); char ch = '\0'; int count = 0; while((ch = fgetc(fp)) != EOF) if( ch == '\n' ) count++; - - fclose(fp); + + fseek(fp, 0, SEEK_SET); // go to beginning of file return count; } // count_lines /* saving and loading */ -void load_file(int argc, char **argv, PAGE *p) +void load_file(PAGE *p, char *filename) { - FILE *fp = fopen(argv[1], "r"); - int size = count_lines(argc, argv) * 2; + FILE *fp = fopen(filename, "r"); + int size = count_lines(fp) * 2; char ch = '\0'; - int line;//, col; - init_page(p, argv[1], size); + int line; + + if(size < PAGE_SIZE) + size = PAGE_SIZE; + + init_page(p, filename, size); for(line = 0; line < size && ch != EOF; line++) { @@ -253,7 +256,7 @@ void load_file(int argc, char **argv, PAGE *p) } // load_file -void save_file(int argc, char **argv, PAGE *p) +void save_file(PAGE *p) { FILE *fp = fopen(p->filename, "w"); int line, col; diff --git a/text.h b/text.h index adea391..42767a2 100644 --- a/text.h +++ b/text.h @@ -12,9 +12,9 @@ void update_status(char *info); -int count_lines(int argc, char **argv); -void load_file(int argc, char **argv, PAGE *p); -void save_file(int argc, char **argv, PAGE *p); +int count_lines(FILE *fp); +void load_file(PAGE *p, char* filename); +void save_file(PAGE *p); void move_left(int *x, int *y); void move_right(PAGE *p, int *x, int *y);