summaryrefslogtreecommitdiff
path: root/files.c
blob: c007eacdc5ac7ba3edfd3b848794cc4c9adf687e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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(i);
		}
		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;
}