summaryrefslogtreecommitdiff
path: root/files.c
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2019-07-27 06:57:12 +0100
committerKatolaZ <katolaz@freaknet.org>2019-07-27 06:57:12 +0100
commit8e3d23b921d9bbcb7c53017bacff8a3050a34b55 (patch)
treefc26ae9768e6c72827b03e973dbf0479b0a0431d /files.c
parent3d53fcefe3ca3e6f25d1731c2caa0c1c0e676453 (diff)
reorganise code
Diffstat (limited to 'files.c')
-rw-r--r--files.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/files.c b/files.c
new file mode 100644
index 0000000..811a9bc
--- /dev/null
+++ b/files.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <string.h>
+#include "gramscii.h"
+
+
+/*** File management ***/
+
+void write_file(FILE *fc){
+ FILE *fout;
+ int i;
+
+ if (!fname[0] || force_new){
+ get_string(fc, "Write to: ", fname, 255);
+ if ((fout=fopen(fname, "r"))!=NULL){
+ if (!is_yes(get_key(fc,"File exists. Overwrite [y/n]?")) ){
+ fclose(fout);
+ return;
+ }
+ fclose(fout);
+ }
+ }
+ if((fout=fopen(fname, "w"))==NULL){
+ get_key(fc, "Error opening file.");
+ return;
+ }
+ for (i=0; i<HEIGHT; i++){
+ fprintf(fout, "%s\n", screen[i].s);
+ }
+ fclose(fout);
+ modified = 0;
+ get_key(fc, "File saved.");
+}
+
+void check_modified(FILE *fc){
+
+ if (modified){
+ if (!is_yes(get_key(fc, "Unsaved changes. Write to file [y/n]?")) ){
+ return;
+ }
+ write_file(fc);
+ }
+}
+
+void load_file(FILE *fc){
+
+ char newfname[256];
+ FILE *fin;
+ int i;
+
+ get_string(fc, "Load file: ", newfname, 255);
+ if ((fin=fopen(newfname, "r")) != NULL){
+ i = 0;
+ while((fgets(screen[i].s, WIDTH+2, fin)) != NULL && i<HEIGHT)
+ screen[i++].s[WIDTH-1]='\0';
+ for(;i<HEIGHT; i++){
+ erase_line(screen[i].s);
+ }
+ fclose(fin);
+ }
+ strcpy(fname, newfname);
+ modified=0;
+ redraw();
+}
+
+void new_file(FILE *fc){
+ check_modified(fc);
+ erase_screen();
+ go_to(HOME);
+ redraw();
+ fname[0] = '\0';
+ modified=0;
+}
+