updated readme, improved load and save

This commit is contained in:
Mazar Farran 2015-08-28 21:53:20 -07:00
parent 99b2eac924
commit 4de07233cd
3 changed files with 22 additions and 24 deletions

View File

@ -18,11 +18,6 @@ Probably the most well-developed program I made this summer.
##Current issues: ##Current issues:
x-y coordinates in ncurses correspond to rows and columns in * Code could be organized better
the text document. This means tabs are an issue and text * Some arguments/variables don't do anything
wrapping is impossible. Solution: separate screen logic * Need to add copy/paste/undo.
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!)

29
text.c
View File

@ -29,7 +29,7 @@ int main(int argc, char *argv[])
if(argc > 1) if(argc > 1)
{ {
load_file(argc, argv, &page); load_file(&page, argv[1]);
} }
else // initialize else // initialize
{ {
@ -70,13 +70,13 @@ int main(int argc, char *argv[])
print_page(&page, beg, end); print_page(&page, beg, end);
break; break;
case KEY_F(5): case KEY_F(5):
save_file(argc, argv, &page); save_file(&page);
sprintf(status, "Saved as \'%s\'", page.filename); sprintf(status, "Saved as \'%s\'", page.filename);
update_status(status); update_status(status);
break; break;
case KEY_F(6): case KEY_F(6):
prompt_string("Save As:", page.filename, NAME_LIMIT); prompt_string("Save As:", page.filename, NAME_LIMIT);
save_file(argc, argv, &page); save_file(&page);
sprintf(status, "Saved as \'%s\'", page.filename); sprintf(status, "Saved as \'%s\'", page.filename);
print_page(&page, beg, end); print_page(&page, beg, end);
update_status(status); update_status(status);
@ -204,27 +204,30 @@ void move_down(PAGE *p, int *x, int *y)
} }
/* movement */ /* movement */
int count_lines(int argc, char **argv) int count_lines(FILE *fp)
{ {
FILE *fp = fopen(argv[1], "r");
char ch = '\0'; char ch = '\0';
int count = 0; int count = 0;
while((ch = fgetc(fp)) != EOF) while((ch = fgetc(fp)) != EOF)
if( ch == '\n' ) if( ch == '\n' )
count++; count++;
fclose(fp); fseek(fp, 0, SEEK_SET); // go to beginning of file
return count; return count;
} // count_lines } // count_lines
/* saving and loading */ /* 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"); FILE *fp = fopen(filename, "r");
int size = count_lines(argc, argv) * 2; int size = count_lines(fp) * 2;
char ch = '\0'; char ch = '\0';
int line;//, col; int line;
init_page(p, argv[1], size);
if(size < PAGE_SIZE)
size = PAGE_SIZE;
init_page(p, filename, size);
for(line = 0; line < size && ch != EOF; line++) for(line = 0; line < size && ch != EOF; line++)
{ {
@ -253,7 +256,7 @@ void load_file(int argc, char **argv, PAGE *p)
} // load_file } // load_file
void save_file(int argc, char **argv, PAGE *p) void save_file(PAGE *p)
{ {
FILE *fp = fopen(p->filename, "w"); FILE *fp = fopen(p->filename, "w");
int line, col; int line, col;

6
text.h
View File

@ -12,9 +12,9 @@
void update_status(char *info); void update_status(char *info);
int count_lines(int argc, char **argv); int count_lines(FILE *fp);
void load_file(int argc, char **argv, PAGE *p); void load_file(PAGE *p, char* filename);
void save_file(int argc, char **argv, PAGE *p); void save_file(PAGE *p);
void move_left(int *x, int *y); void move_left(int *x, int *y);
void move_right(PAGE *p, int *x, int *y); void move_right(PAGE *p, int *x, int *y);