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