/*
	e screen editor

	(C) G. Nigel Gilbert, MICROLOGY, 1981

	August-December 1981

	FILE: e5

	FUNCTIONS: readfile, writefile, exists, scans, retag, format

	PURPOSE: read and write files

*/

#include "e.h"

readfile(name)	/* read file 'name' and insert it after cursor*/
char *name;
{
	int c, i, j, k, line, first, res;
	char textb[LLIM];

	if (fopen(name,fbuf) == FAIL) {
		error("Can't find file");
		name[0]='\0'; 
		return(FAIL);
		}
	first=YES;
	i=charn; 
	line=cline;
	for (j=charn, k=0; (textb[k]=text[j]); j++, k++);
	do {
		while (i < LLIM && (c=getc(fbuf)) != FAIL && c != '\n'
		&& c != ENDFILE) text[i++]=c;
		if (text[i-1] == '\r') i--;
		text[i]='\0';
		if (c == ENDFILE) for (k=0; (text[i]=textb[k]) && i < LLIM; i++, k++);
		if (first) {
			altered=YES;
			puttext();
			first=NO;
			res=YES;
			}
		else {
			res=inject(line,text);
			line++;
			}
		i=0;
		} while (c != FAIL && res != FAIL && c != ENDFILE);
	fclose(fbuf);
	gettext(cline);
}

writefile(from,to,name,nametoprint,exiting)
int from, to, exiting;
char *name, *nametoprint;
{
	int l, copying, copysecstart,copybytestart;
	char c, *t, *getline();

	puttext();
	if (name[0] <= ' ') {
		error("Bad name");
		return FAIL;
		}
	if (fcreat(name,fbuf) == FAIL) {
		error("Can't write file - directory full?");
		return(FAIL);
		}
	putmess("Saving "); 
	puts(nametoprint);
	copying=NO;
	if (exiting) to=lastread;
	else if (to == lastl) to=loc(lastl,0);
	for (l=from; l <= to; ) {
		t=getline(l++);
		while (*t) if(putc(*t++,fbuf) == FAIL) goto diskfull;
		if (l <= to ) {
			if (putc('\r',fbuf) == FAIL) goto diskfull;
			if (putc('\n',fbuf) == FAIL) goto diskfull;
			}
		}
	if (exiting && (lastl == UNKNOWN) || !goteof) {
		copysecstart=tell(textbuf->_fd) -
		(textbuf->_nleft + SECSIZ)/SECSIZ;
		copybytestart=SECSIZ-(textbuf->_nleft + SECSIZ)%SECSIZ;
		if (putc('\r',fbuf) == FAIL) goto diskfull;
		if (putc('\n',fbuf) == FAIL) goto diskfull;
		copying=YES;
		while ( (c=getc(textbuf)) != ENDFILE && c != FAIL)
			if (putc(c,fbuf) == FAIL) goto diskfull;
		}
	if (putc(ENDFILE,fbuf) == FAIL)goto diskfull;
	if (fflush(fbuf) == FAIL) goto diskfull;
	if (fclose(fbuf) == FAIL) {
		error("Can't close file");
		goto reposition;
		}
	if (copying) fclose(textbuf);
	return YES;
diskfull:
	error("Disk full");
reposition:
	if (copying) {
		fclose(fbuf); 
		funlink(name);
		seek(textbuf->_fd,copysecstart,0);
		textbuf->_nleft=0;
		while (copybytestart--) getc(textbuf);
		}
	return FAIL;
}

exists(name)
char *name;
{
	char c;

	c='y';
	if (checkexists(name)) {
		putmess("OK to replace "); 
		puts(name); 
		puts(" ? ");
		putch((c=getlow()));
		putret(); 
		}
	return c == 'y';
}

checkexists(name)	/*return YES if file 'name' exists, else NO */
char *name;
{
	int fd;
	
	if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO;
	close(fd);
	return YES;
}

scans(answer,maxlen)
char *answer;
int maxlen;
{
	char c;
	int n, i;

	maxlen--;
	n=0;
	while (n < maxlen) {
		switch((c=getscankey())) {
		case LEFTKEY	:
		case DELLEFT	:
			if (n) {
				putch(BACKSP); 
				putch(' ');
				putch(BACKSP);
				n--; 
				answer--;
				}
			break;
		case CR		:
		case ESCKEY	:
			n=maxlen; 
			break;
		case RETRIEVE	:
			if (n == 0) {
				while (*answer) {
					dispch(*answer++);
					n++;
					}
				break;
				}
		default		:   
			dispch(c); 
			*answer++=c; 
			n++;
			break;
			}
		}
	*answer='\0';
	putret();
	return c;
}

retag(name,tag)	/*puts a new suffix on a file name*/
char *name, *tag;
{
	for (; *name && *name != '.'; name++);
	if (!*name)*name='.';
	for (++name; (*name=*tag); name++, tag++);
}

format(name)
char *name;
{
	char tempname[FILELEN], *n, *tn;

	if (!*name) return;
	if (*(name+1) == ':') strcpy(tempname,name);
	else {
		tempname[0]=curdsk+'A';
		tempname[1]=':';
		name[12]='\0';
		strcpy(&tempname[2],name);
		}
	for (n=name,tn=tempname; (*n=toupper(*tn++)); n++) if (*n == '.') break;
	while (*n++) *n=tolower(*tn++);
}
) == FAIL) goto diskfull;
		}
	if (putc(ENDFILE,fbuf) == FAIL)goto diskfull