summaryrefslogtreecommitdiff
path: root/files.c
blob: dc91e3b0f34b8cc231da00b81043f518db1fcede (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#define _POSIX_C_SOURCE 200112L

#include <stdio.h>
#include <string.h>
#include "gramscii.h"


/** extern declarations **/

extern lineset_t screen; /* what is visualised */

extern int WIDTH, HEIGHT;

extern int force_new;
extern char modified; /* set to 1 if screen modified since last save */
extern char fname[256];

/*** File management ***/

void write_file(FILE *fc){
	FILE *fout;
	int i, ne;

	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;
	}
	ne = 0;
	for (i=0; i<HEIGHT; i++){
		if (strlen(screen.l[i].s)){/* remove trailing blank lines */
			/* put the empty lines preceeding the current non-empty one */
			while (ne--)
				fprintf(fout, "\n");
			fprintf(fout, "%s\n", screen.l[i].s);
			ne = 0;
		}
		else ne++;
	}
	fclose(fout);
	modified = 0;
	get_key(fc, "File saved.");
	redraw();
}

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.l[i].s, WIDTH+1, fin)) != NULL && i<HEIGHT){
			screen.l[i].lst = strlen(screen.l[i].s) - 2;
			screen.l[i].n = i;
			screen.l[i].s[strlen(screen.l[i].s)-1]='\0';
			i++;
		}
		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;
}

void read_file_at(FILE *fc, int xl, int yl){

	char nfname[512], tmp[512], *fptr, *tptr;
	FILE *fin;
	int i, j;
	char ftype;

	get_string(fc, "Read file: ", nfname, 511);
	fptr = nfname;
	while(*fptr && _isblank(*fptr))
		 fptr ++;
	if (*fptr == '!'){
		fin = popen(++fptr, "r");
		ftype = FPIPE;
	}
	else {
		fin = fopen(fptr, "r");
		ftype = FFILE;
	}
	if (fin != NULL){
		copy_lines_to_ring(0, HEIGHT-1, PRV_STATE);
		i = yl;
		while((fgets(tmp, WIDTH+1, fin)) != NULL && i<HEIGHT){
			j = xl;
			tptr = tmp;
			if (strlen(tmp))
				tmp[strlen(tmp) - 1] = '\0';
			ensure_line_length(& (screen.l[i]), xl + strlen(tmp) + 1);
			while (*tptr && j < WIDTH){
				set_xy(j, i, *tptr);
				j++;
				tptr ++;
			}
			i++;
		}
		if (ftype == FFILE)
			fclose(fin);
		else
			pclose(fin);
		modified = 1;
		redraw();
		copy_lines_to_ring(yl, i-1, NEW_STATE);
	}
}