12486Sdlw /*
2*20984Slibs char id_dfe[] = "@(#)dfe.c 1.6";
32486Sdlw *
42486Sdlw * direct formatted external i/o
52486Sdlw */
62486Sdlw
72486Sdlw #include "fio.h"
82486Sdlw
92486Sdlw extern int rd_ed(),rd_ned(),w_ed(),w_ned();
102486Sdlw int y_getc(),y_putc(),y_rnew(),y_wnew(),y_tab();
112486Sdlw
12*20984Slibs LOCAL char rdfe[] = "read dfe";
13*20984Slibs LOCAL char wdfe[] = "write dfe";
142486Sdlw
s_rdfe(a)152486Sdlw s_rdfe(a) cilist *a;
162486Sdlw {
172486Sdlw int n;
182486Sdlw reading = YES;
192486Sdlw if(n=c_dfe(a,READ)) return(n);
204113Sdlw if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rdfe)
212486Sdlw getn = y_getc;
222486Sdlw doed = rd_ed;
232486Sdlw doned = rd_ned;
242486Sdlw dotab = y_tab;
252486Sdlw dorevert = doend = donewrec = y_rnew;
2617967Slibs if(pars_f()) err(errflag,F_ERFMT,rdfe)
272486Sdlw fmt_bg();
282486Sdlw return(OK);
292486Sdlw }
302486Sdlw
s_wdfe(a)312486Sdlw s_wdfe(a) cilist *a;
322486Sdlw {
332486Sdlw int n;
342486Sdlw reading = NO;
352486Sdlw if(n=c_dfe(a,WRITE)) return(n);
362486Sdlw curunit->uend = NO;
374113Sdlw if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wdfe)
382486Sdlw putn = y_putc;
392486Sdlw doed = w_ed;
402486Sdlw doned = w_ned;
412486Sdlw dotab = y_tab;
422486Sdlw dorevert = doend = donewrec = y_wnew;
4317967Slibs if(pars_f()) err(errflag,F_ERFMT,wdfe)
442486Sdlw fmt_bg();
452486Sdlw return(OK);
462486Sdlw }
472486Sdlw
e_rdfe()482486Sdlw e_rdfe()
492486Sdlw {
502486Sdlw en_fio();
512486Sdlw return(OK);
522486Sdlw }
532486Sdlw
e_wdfe()542486Sdlw e_wdfe()
552486Sdlw {
562486Sdlw en_fio();
572486Sdlw return(OK);
582486Sdlw }
592486Sdlw
60*20984Slibs LOCAL
c_dfe(a,flag)612486Sdlw c_dfe(a,flag) cilist *a;
622486Sdlw { int n;
632486Sdlw sequential = NO;
642486Sdlw external = formatted = FORMATTED;
652486Sdlw lfname = NULL;
662486Sdlw elist = NO;
672486Sdlw cursor=scale=recpos=reclen=0;
682486Sdlw radix = 10;
692486Sdlw signit = YES;
702486Sdlw fmtbuf = a->cifmt;
712486Sdlw errflag = a->cierr;
722486Sdlw endflag = a->ciend;
732486Sdlw lunit = a->ciunit;
744113Sdlw if(not_legal(lunit)) err(errflag,F_ERUNIT,rdfe+5);
752486Sdlw curunit = &units[lunit];
762486Sdlw if(!curunit->ufd && (n=fk_open(flag,DIR,FMT,(ftnint)lunit)))
774113Sdlw err(errflag,n,rdfe+5)
782486Sdlw cf = curunit->ufd;
792486Sdlw elist = YES;
802486Sdlw lfname = curunit->ufnm;
814113Sdlw if(!curunit->ufmt) err(errflag,F_ERNOFIO,rdfe+5)
824113Sdlw if(!curunit->useek || !curunit->url) err(errflag,F_ERNODIO,rdfe+5)
832486Sdlw recnum = a->cirec - 1;
842486Sdlw fseek(cf, (long)curunit->url * recnum, 0);
852486Sdlw cblank = curunit->ublnk;
862486Sdlw cplus = NO;
872486Sdlw return(OK);
882486Sdlw }
892486Sdlw
90*20984Slibs LOCAL
y_getc()912486Sdlw y_getc()
922486Sdlw {
932486Sdlw int ch;
942486Sdlw if(curunit->uend) return(EOF);
952486Sdlw if(curunit->url==1 || recpos++ < curunit->url)
962486Sdlw {
972486Sdlw if((ch=getc(cf))!=EOF)
982486Sdlw {
992486Sdlw return(ch);
1002486Sdlw }
1012486Sdlw if(feof(cf))
1022486Sdlw {
1032486Sdlw curunit->uend = YES;
1042486Sdlw return(EOF);
1052486Sdlw }
1062486Sdlw err(errflag,errno,rdfe);
1072486Sdlw }
1082486Sdlw else return(' ');
1092486Sdlw }
1102486Sdlw
111*20984Slibs LOCAL
y_putc(c)1122486Sdlw y_putc(c)
1132486Sdlw {
1142584Sdlw if(curunit->url!=1 && recpos++ >= curunit->url) err(errflag,F_EREREC,wdfe)
1152486Sdlw putc(c,cf);
1162486Sdlw return(OK);
1172486Sdlw }
1182486Sdlw
119*20984Slibs LOCAL
y_tab()1202486Sdlw y_tab()
1212486Sdlw { int n;
1222486Sdlw if(curunit->url==1)
1232486Sdlw {
12412046Sdlw if(cursor < 0 && -cursor > ftell(cf)) rewind(cf);
12512046Sdlw else fseek(cf,(long)cursor,1);
12612046Sdlw return(cursor=0);
1272486Sdlw }
1282486Sdlw else
1292486Sdlw { if(reclen < recpos) reclen = recpos;
13012046Sdlw if((recpos + cursor) < 0) cursor = -recpos; /* BOR */
1312486Sdlw n = reclen - recpos; /* n >= 0 */
1322486Sdlw if(!reading && (cursor-n) > 0)
1332486Sdlw { recpos = reclen;
1342486Sdlw cursor -= n;
1352486Sdlw fseek(cf,(long)n,1);
1362486Sdlw while(cursor--) if(n=(*putn)(' ')) return(n);
1372486Sdlw return(cursor=0);
1382486Sdlw }
1392486Sdlw recpos += cursor;
1404113Sdlw if(recpos >= curunit->url) err(errflag,F_EREREC,rdfe+5)
1412486Sdlw }
1422486Sdlw fseek(cf,(long)cursor,1);
1432486Sdlw return(cursor=0);
1442486Sdlw }
1452486Sdlw
1462486Sdlw /*
1472486Sdlw /*y_rev()
1482486Sdlw /*{ /*what about work done?*/
1492486Sdlw /* if(curunit->url==1) return(0);
1502486Sdlw /* while(recpos<curunit->url) (*putn)(' ');
1512486Sdlw /* recpos=0;
1522486Sdlw /* return(0);
1532486Sdlw /*}
1542486Sdlw /*
1552486Sdlw /*y_err()
1562486Sdlw /*{
1574113Sdlw /* err(errflag, F_EREREC, rdfe+5);
1582486Sdlw /*}
1592486Sdlw */
1602486Sdlw
161*20984Slibs LOCAL
y_rnew()1622486Sdlw y_rnew()
1632486Sdlw { if(curunit->url != 1)
1642486Sdlw { fseek(cf,(long)curunit->url*(++recnum),0);
1652486Sdlw recpos = reclen = cursor = 0;
1662486Sdlw }
1672486Sdlw return(OK);
1682486Sdlw }
1692486Sdlw
170*20984Slibs LOCAL
y_wnew()1712486Sdlw y_wnew()
1722486Sdlw { if(curunit->url != 1)
1732486Sdlw { if(reclen > recpos)
1742486Sdlw { fseek(cf,(long)(reclen-recpos),1);
1752486Sdlw recpos = reclen;
1762486Sdlw }
1772486Sdlw while(recpos < curunit->url) (*putn)(' ');
1782486Sdlw recnum++;
1792486Sdlw recpos = reclen = cursor = 0;
1802486Sdlw }
1812486Sdlw return(OK);
1822486Sdlw }
1832486Sdlw
184*20984Slibs LOCAL
y_rend()1852486Sdlw y_rend()
1862486Sdlw {
1872486Sdlw return(OK);
1882486Sdlw }
1892486Sdlw
190*20984Slibs LOCAL
y_wend()1912486Sdlw y_wend()
1922486Sdlw {
1932486Sdlw return(y_wnew());
1942486Sdlw }
195