1 /* Loglan82 Compiler&Interpreter
2 Copyright (C) 1993 Institute of Informatics, University of Warsaw
3 Copyright (C) 1993, 1994 LITA, Pau
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 contacts: Andrzej.Salwicki@univ-pau.fr
22 LITA Departement d'Informatique
24 Avenue de l'Universite
26 tel. ++33 59923154 fax. ++33 59841696
28 =======================================================================
39 /* File I/O routines */
43 * Load parameters of current file
44 * @parameter expected status of file
45 * @parameter file type
46 * @parameter file object address
47 * @parameter file stream pointer
49 void loadfile(word status, word *ftype, word *am, FILE **fp)
54 loadvirt(virt, currfile);
55 /* file object exists */
56 if (member(&virt, am)) {
59 if (status != s && status != UNKNOWN)
61 *ftype = M[*am + FTYPE];
62 *fp = MF(*am + FFILE);
64 /* file not opened yet */
72 * @parameter TRUE iff file is temporary
73 * @parameter file type
74 * @parameter file name
75 * @parameter output virtual address
76 * @parameter output physical address
78 void genfileobj(bool ftemp, word ftyp, char *fnam, virtaddr *virt, word *am)
82 /* generate file object */
83 request((word) APFILE, &t1, am);
85 virt->mark = M[t1 + 1];
86 M[*am + PROTNUM] = FILEOBJECT;
87 M[*am + FSTAT] = UNKNOWN;
88 M[*am + FTEMP] = lbool(ftemp);
89 M[*am + FTYPE] = ftyp;
90 MN(*am + FNAME) = fnam;
93 /* Prepare file for reading */
98 /* first close file if opened */
99 if (M[am + FSTAT] != UNKNOWN)
100 if (fclose(MF(am + FFILE)))
103 switch ((int) M[am + FTYPE]) {
104 /* open text file for reading */
106 fp = fopen(MN(am + FNAME), "r");
107 M[am + FSTAT] = READING;
110 /* open binary file for reading */
114 fp = fopen(MN(am + FNAME), BINARYREAD);
115 M[am + FSTAT] = READING;
118 /* open existing file for update */
120 fp = fopen(MN(am + FNAME), DIRECTOLD);
121 M[am + FSTAT] = UPDATING;
125 M[am + FSTAT] = UNKNOWN;
128 /* store stream pointer */
133 * Prepare file for writing
135 void rewrite(word am)
139 /* first close file if opened */
140 if (M[ am+FSTAT ] != UNKNOWN)
141 if (fclose(MF(am + FFILE)))
144 switch ((int) M[am + FTYPE]) {
145 /* open text file for writing */
147 fp = fopen(MN(am + FNAME), "w");
148 M[am + FSTAT] = WRITING;
151 /* open binary file for writing */
155 fp = fopen(MN(am + FNAME), BINARYWRITE);
156 M[am + FSTAT] = WRITING;
159 /* create new file for update */
161 fp = fopen(MN(am+FNAME), DIRECTNEW);
162 M[am + FSTAT] = UPDATING;
167 M[am + FSTAT] = UNKNOWN;
170 /* store stream pointer */
177 void delete(virtaddr *virt)
181 if (member(virt, &am)) {
182 /* first close file if opened */
183 if (M[am + FSTAT ] != UNKNOWN)
184 if (fclose(MF(am+FFILE))) errsignal(RTEIOERR);
187 if (unlink(MN(am+FNAME)))
189 /* free memory used by file name */
191 /* and kill file object */
199 * Generate temporary file name
204 static int tempcnt = 0;
210 sprintf(cp, "LOG%05d", tempcnt++);
215 * Test for end of file
217 bool testeof(FILE *fp)
227 * Test for end of line
229 bool testeoln(FILE *fp)
239 * Skip to end of line
241 void readln(FILE *fp)
249 while (ch != '\n' && ch != EOF) {
259 word readint(FILE *fp)
266 while(c < '0' || c > '9') {
281 i = 10*i + (c - '0');
284 } while(c >= '0' && c <= '9');
301 double readreal(FILE *fp)
305 if (fscanf(fp, "%lf", &r) != 1)
314 void writeint(word n, word field, FILE *fp)
316 static char format[32];
318 sprintf(format,"%*ld",(int)field, (long)n);
322 else if (fprintf(fp, "%*ld", (int)field, (long) n) == 0)
329 void writereal(double r, word field1, word field2, FILE *fp)
333 sprintf(format, "%*.*lf", (int) field1, (int) field2,r);
336 else if (fprintf(fp,"%*.*lf", (int)field1, (int)field2, r) == 0)
343 void writestring(word offset, word field, FILE *fp)
350 addr = strings + offset;
352 /* pointer to first char of string */
353 cp = (char *) &M[ addr+1 ];
355 for(i = 0; i < len; i++)
360 while (len-- > 0 && field-- != 0) {
361 if (putc(*cp++, fp) == EOF)
368 * Perform direct access read/write
369 * @parameter buffer array
370 * @parameter number of bytes to transfer
371 * @parameter fread() or fwrite()
372 * @parameter stream pointer
374 #ifndef NO_PROTOTYPES
375 word directio(virtaddr *buf, word len, int (*action)(char *,int, int, FILE *),
378 word directio(virtaddr *buf, word len, int (*action)(), FILE *fp)
385 if (member(buf, &am)) {
386 /* seek to current position required*/
387 if (fseek(fp, 0L, 1))
391 len = min(len, (M[ am ]-3)*sizeof(word));
392 /* number of bytes transfered */
394 /* address in memory for transfer */
396 /* transfer full blocks */
397 while (len >= IOBLOCK) {
398 n = (*action)((char *) &M[t1], 1, IOBLOCK, fp);
403 t1 += IOBLOCK / sizeof(word);
405 /* transfer last unfilled block */
407 n = (*action)((char *) &M[t1], 1, (int) len, fp);