#include "ded.h" #include "char.h" #include "file.h" /************************************************************************ * * * copyright Richard Bornat 1981 * * * ************************************************************************/ /* file handler */ int f_tmp = -1; int f_write; char f_cache[512]; #define fpLIM &f_cache[511] int f_cblock; /* block offset of file cache */ int f_nblock; /* where next write will go */ int tmp_block, tmp_offset; /* where the end of the temporary file is */ /* create the temporary file */ void maketemp(tname) register char *tname; { if (f_tmp == -1) /* not yet opened */ { if ((f_tmp = creat(tname, 0666)) == -1) quit("cannot create temporary file %s", tname); close(f_tmp); if ((f_tmp = open(tname, 2)) == -1) quit("cannot re-open temporary file %s", tname); } maxl = -1; tmp_block = tmp_offset = 0; tmp_changed = false; { register int i; for (i=0; i=fline) { *linebuf = i+'a'+0200; /* bright tags */ break; } /* one more space then a terminator */ linebuf++; *linebuf++ = ' '; *linebuf = 0; } getline(fline,linebuf) register int fline; register char *linebuf; { if (topl<=fline && fline<=topl+LASTINROW) { linetag(fline, linebuf); linebuf += MARGIN; copyrow(fline-topl,MARGIN,linebuf); } else getfline(fline,linebuf); } /* gets a line from disk into store */ getfline(fline,linebuf) int fline; register char *linebuf; { register char *fp; register unsigned lo = l_offset[fline]; int block, offset; block = (lo>>7)&0777; offset = (lo<<2)&0777; linetag(fline, linebuf); linebuf += MARGIN; if (lo == (unsigned)-1 || fline>maxl) *linebuf = 0; else { getblock(block, true); fp = f_cache+offset; while ((*linebuf++ = *fp++) != 0) if (fp > fpLIM) { getblock(++block, true); fp = f_cache; } *linebuf = 0; } return; } /* writes the line in buf * onto the end of the file */ void addline(fline,linebuf) int fline; register char *linebuf; { register char *fp; int block, offset; register char c; if (linebuf==0 || *linebuf==0) l_offset[fline] = -1; else { block = tmp_block; offset = tmp_offset; getblock(block, offset!=0); f_write++; fp = f_cache + offset; do { *fp++ = c = *linebuf++; if (fp > fpLIM) { getblock(++block,false); f_write++; fp = f_cache; } } while (c != 0); if ((l_offset[fline] = (tmp_block<<7) | (tmp_offset>>2)) == (unsigned)-1 || tmp_block>0777) editerror("[addline] file too big"); while ((tmp_offset = (fp-f_cache)) & 03) fp++; if (tmp_offset>=512) { tmp_offset -= 512; block++; } tmp_block = block; } } emptyline(fline) register int fline; { return(onscreen(fline) ? emptyrow(fline-topl) : l_offset[fline] == (unsigned)-1); } /* get a block from the tmp file into the cache buffer */ seekit(block) register int block; { if (block!=f_nblock) #ifdef V7 lseek(f_tmp, ((long) block)*512, 0); #else seek(f_tmp, block, 3); #endif } getblock(block,there) register int block; int there; { register int oblock = f_cblock; if (oblock != block) { /* write out old block if necessary */ if (f_write) { seekit(oblock); if (write(f_tmp, f_cache, 512) != 512) editerror("tmp write failed[getblock] - out of space?"); else f_nblock = oblock+1; } /* read in new block if necessary */ if (there) { register int count; seekit(block); if ((count=read(f_tmp, f_cache, 512))<=0) editerror("tmp read failed[getblock] - what goes on?"); else if (count==512) f_nblock=block+1; else f_nblock = -1; } /* record new position */ f_cblock=block; f_write = 0; } }