feature: can now edit new documents using the command line

This commit is contained in:
Mazar Farran 2015-08-29 14:53:31 -07:00
parent 4de07233cd
commit e0d56b903c
2 changed files with 28 additions and 4 deletions

31
text.c
View File

@ -29,12 +29,19 @@ int main(int argc, char *argv[])
if(argc > 1) if(argc > 1)
{ {
load_file(&page, argv[1]); if(file_exists(argv[1]))
{
load_file(&page, argv[1]);
}
else
{
init_page(&page, argv[1], PAGE_SIZE);
page.numlines = 1;
}
} }
else // initialize else // initialize
{ {
init_page(&page, "untitled.txt", PAGE_SIZE); init_page(&page, "untitled.txt", PAGE_SIZE);
page.text[0].line[0] = '\0';
page.numlines = 1; page.numlines = 1;
} }
@ -131,9 +138,9 @@ int main(int argc, char *argv[])
} }
} }
} }
endnc: endwin(); endnc:
/* end curses */ /* end curses */
endwin();
dest_page(&page); dest_page(&page);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} // main } // main
@ -229,6 +236,13 @@ void load_file(PAGE *p, char *filename)
init_page(p, filename, size); init_page(p, filename, size);
if(fp == NULL) // file doesn't exist yet. don't bother reading
{
p->numlines = 1;
return;
}
for(line = 0; line < size && ch != EOF; line++) for(line = 0; line < size && ch != EOF; line++)
{ {
ch = fgetc(fp); ch = fgetc(fp);
@ -275,4 +289,13 @@ void save_file(PAGE *p)
fclose(fp); fclose(fp);
} // save_file } // save_file
int file_exists(char *filename)
{
FILE *fp = fopen(filename, "r");
int result = (fp == NULL);
if(result)
fclose(fp);
return !result;
}
/* saving and loading */ /* saving and loading */

1
text.h
View File

@ -15,6 +15,7 @@ void update_status(char *info);
int count_lines(FILE *fp); int count_lines(FILE *fp);
void load_file(PAGE *p, char* filename); void load_file(PAGE *p, char* filename);
void save_file(PAGE *p); void save_file(PAGE *p);
int file_exists(char *filename);
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);