Commit | Line | Data |
---|---|---|
f90fa2f5 | 1 | /* |
3f0c29e0 | 2 | char id_sfe[] = "@(#)sfe.c 1.4"; |
f90fa2f5 DW |
3 | * |
4 | * sequential formatted external routines | |
5 | */ | |
6 | ||
7 | #include "fio.h" | |
8 | ||
9 | /* | |
10 | * read sequential formatted external | |
11 | */ | |
12 | ||
13 | extern int rd_ed(),rd_ned(); | |
14 | int x_rnew(),x_getc(),x_tab(); | |
15 | ||
3f0c29e0 DW |
16 | char rsfe[] = "read sfe"; |
17 | char wsfe[] = "write sfe"; | |
18 | ||
f90fa2f5 DW |
19 | s_rsfe(a) cilist *a; /* start */ |
20 | { int n; | |
21 | reading = YES; | |
22 | if(n=c_sfe(a,READ)) return(n); | |
3f0c29e0 | 23 | if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rsfe) |
f90fa2f5 DW |
24 | getn= x_getc; |
25 | doed= rd_ed; | |
26 | doned= rd_ned; | |
27 | donewrec = dorevert = doend = x_rnew; | |
28 | dotab = x_tab; | |
3f0c29e0 | 29 | if(pars_f(fmtbuf)) err(errflag,F_ERFMT,rsfe) |
f90fa2f5 DW |
30 | fmt_bg(); |
31 | return(OK); | |
32 | } | |
33 | ||
34 | x_rnew() /* find next record */ | |
35 | { int ch; | |
36 | if(!curunit->uend) | |
37 | while((ch=getc(cf))!='\n' && ch!=EOF); | |
ee64299d DW |
38 | if(recpos==0 && feof(cf)) |
39 | { curunit->uend = YES; | |
40 | return(EOF); | |
41 | } | |
f90fa2f5 DW |
42 | cursor=recpos=reclen=0; |
43 | return(OK); | |
44 | } | |
45 | ||
46 | x_getc() | |
47 | { int ch; | |
48 | if(curunit->uend) return(EOF); | |
49 | if((ch=getc(cf))!=EOF && ch!='\n') | |
50 | { recpos++; | |
51 | return(ch); | |
52 | } | |
53 | if(ch=='\n') | |
54 | { ungetc(ch,cf); | |
55 | return(ch); | |
56 | } | |
57 | if(feof(cf)) curunit->uend = YES; | |
58 | return(EOF); | |
59 | } | |
60 | ||
61 | e_rsfe() | |
62 | { int n; | |
63 | n=en_fio(); | |
64 | fmtbuf=NULL; | |
65 | return(n); | |
66 | } | |
67 | ||
68 | c_sfe(a,flag) cilist *a; /* check */ | |
69 | { unit *p; | |
70 | int n; | |
71 | external=sequential=formatted=FORMATTED; | |
72 | fmtbuf=a->cifmt; | |
73 | lfname = NULL; | |
74 | elist = NO; | |
75 | errflag = a->cierr; | |
76 | endflag = a->ciend; | |
77 | lunit = a->ciunit; | |
3f0c29e0 | 78 | if(not_legal(lunit)) err(errflag,F_ERUNIT,rsfe+5); |
f90fa2f5 DW |
79 | curunit = p = &units[lunit]; |
80 | if(!p->ufd && (n=fk_open(flag,SEQ,FMT,(ftnint)lunit)) ) | |
3f0c29e0 | 81 | err(errflag,n,rsfe+5) |
f90fa2f5 DW |
82 | cf = curunit->ufd; |
83 | elist = YES; | |
84 | lfname = curunit->ufnm; | |
3f0c29e0 DW |
85 | if(!p->ufmt) err(errflag,F_ERNOFIO,rsfe+5) |
86 | if(p->url) err(errflag,F_ERNOSIO,rsfe+5) | |
f90fa2f5 DW |
87 | cursor=recpos=scale=reclen=0; |
88 | radix = 10; | |
89 | signit = YES; | |
90 | cblank = curunit->ublnk; | |
91 | cplus = NO; | |
92 | return(OK); | |
93 | } | |
94 | ||
95 | /* | |
96 | * write sequential formatted external | |
97 | */ | |
98 | ||
99 | extern int w_ed(),w_ned(); | |
100 | int x_putc(),pr_put(),x_wend(),x_wnew(); | |
101 | ioflag new; | |
102 | ||
103 | s_wsfe(a) cilist *a; /*start*/ | |
104 | { int n; | |
105 | reading = NO; | |
106 | if(n=c_sfe(a,WRITE)) return(n); | |
3f0c29e0 | 107 | if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wsfe) |
f90fa2f5 DW |
108 | curunit->uend = NO; |
109 | if (curunit->uprnt) putn = pr_put; | |
110 | else putn = x_putc; | |
111 | new = YES; | |
112 | doed= w_ed; | |
113 | doned= w_ned; | |
114 | doend = x_wend; | |
115 | dorevert = donewrec = x_wnew; | |
116 | dotab = x_tab; | |
3f0c29e0 | 117 | if(pars_f(fmtbuf)) err(errflag,F_ERFMT,wsfe) |
f90fa2f5 DW |
118 | fmt_bg(); |
119 | return(OK); | |
120 | } | |
121 | ||
122 | x_putc(c) | |
123 | { | |
124 | if(c=='\n') recpos = reclen = cursor = 0; | |
125 | else recpos++; | |
126 | if (c) putc(c,cf); | |
127 | return(OK); | |
128 | } | |
129 | ||
130 | pr_put(c) | |
131 | { | |
132 | if(c=='\n') | |
133 | { new = YES; | |
134 | recpos = reclen = cursor = 0; | |
135 | } | |
136 | else if(new) | |
137 | { new = NO; | |
138 | if(c=='0') c = '\n'; | |
139 | else if(c=='1') c = '\f'; | |
140 | else return(OK); | |
141 | } | |
142 | else recpos++; | |
143 | if (c) putc(c,cf); | |
144 | return(OK); | |
145 | } | |
146 | ||
147 | x_tab() | |
148 | { int n; | |
149 | if(reclen < recpos) reclen = recpos; | |
150 | if(curunit->useek) | |
43666f58 | 151 | { if((recpos+cursor) < 0) return(F_ERBREC); |
f90fa2f5 DW |
152 | n = reclen - recpos; /* distance to eor, n>=0 */ |
153 | if((cursor-n) > 0) | |
154 | { fseek(cf,(long)n,1); /* find current eor */ | |
155 | recpos = reclen; | |
156 | cursor -= n; | |
157 | } | |
158 | else | |
159 | { fseek(cf,(long)cursor,1); /* do not pass go */ | |
160 | recpos += cursor; | |
161 | return(cursor=0); | |
162 | } | |
163 | } | |
164 | else | |
43666f58 | 165 | if(cursor < 0) return(F_ERSEEK); /* can't go back */ |
f90fa2f5 DW |
166 | while(cursor--) |
167 | { if(reading) | |
168 | { n = (*getn)(); | |
169 | if(n=='\n') | |
170 | { (*ungetn)(n,cf); | |
43666f58 | 171 | return(F_EREREC); |
f90fa2f5 DW |
172 | } |
173 | if(n==EOF) return(EOF); | |
174 | } | |
175 | else (*putn)(' '); /* fill in the empty record */ | |
176 | } | |
177 | return(cursor=0); | |
178 | } | |
179 | ||
180 | x_wnew() | |
181 | { | |
182 | if(reclen>recpos) fseek(cf,(long)(reclen-recpos),1); | |
183 | return((*putn)('\n')); | |
184 | } | |
185 | ||
186 | x_wend(last) char last; | |
187 | { | |
188 | if(reclen>recpos) fseek(cf,(long)(reclen-recpos),1); | |
189 | return((*putn)(last)); | |
190 | } | |
191 | ||
192 | /* | |
193 | /*xw_rev() | |
194 | /*{ | |
195 | /* if(workdone) x_wSL(); | |
196 | /* return(workdone=0); | |
197 | /*} | |
198 | /* | |
199 | */ | |
200 | e_wsfe() | |
201 | { return(e_rsfe()); } |