home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_100
/
158_01
/
qe5
< prev
next >
Wrap
Text File
|
1987-10-12
|
5KB
|
231 lines
/* VERSION 0002 (DATE: 23.VII.85) (TIME: 3:35PM) */
/*
e (qe) screen editor
(C) G. Nigel Gilbert, MICROLOGY, 1981
August-December 1981
Modified: To QE from e (ver 4.6a) by J.W. Haefner -- Oct 84-Mar 85
FILE: qe5
FUNCTIONS: readfile, writefile, exists, scans, retag, format
PURPOSE: read and write files
*/
#include "qe.h"
#define ESC 0x1b
#define GS 0x1d
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);
}
if (expert) xprtmess("Saving ");
else 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);
textbuf->_nleft=0;
while (copybytestart--) getc(textbuf);
}
return FAIL;
}
exists(name)
char *name;
{
char c;
c='y';
if (checkexists(name)) {
if (expert) xprtmess("Replace ");
else 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 BACKSP :
case DELLEFT_P :
if (n) {
putch(BACKSP);
putch(' ');
putch(BACKSP);
n--;
answer--;
}
break;
case ESCKEY_P :
case CR_P :
n=maxlen;
break;
case LEFTKEY_P :
c = tran[LEFTKEY];
goto passthru;
break;
case RETRIEVE_P :
/*if (n == 0) {*/
if (*answer) {
while (*answer) {
dispch(*answer++);
n++;
}
break;
}
else {
c = tran[RETRIEVE];
goto passthru;
}
case GS : /* kludge to permit ESC in find/alter */
c=ESC;
passthru: default :
dispch(c);
*answer++=c;
n++;
break;
}
}
*answer='\0';
putret();
return (c & ~PARBIT); /* maintain compatibility with other calls*/
}
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++);
}
if (copying) {
fclose(fbuf);
funlink(name);
textb