| 1 |
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ |
| 2 |
/* From input file "multfile.pas" */ |
| 3 |
|
| 4 |
|
| 5 |
#include "p2c.h" |
| 6 |
|
| 7 |
|
| 8 |
#define MULTFILE_G |
| 9 |
#include "multfile.h" |
| 10 |
|
| 11 |
|
| 12 |
Char nextData[256] = ""; |
| 13 |
|
| 14 |
|
| 15 |
typedef struct filenode { |
| 16 |
Char name[256]; |
| 17 |
FILE *actualfile; |
| 18 |
struct filenode *prev; |
| 19 |
short lineno; |
| 20 |
Char actualfile_NAME[_FNSIZE]; |
| 21 |
} filenode; |
| 22 |
|
| 23 |
|
| 24 |
Static filenode *current = NULL; |
| 25 |
Static short last_valid_line_no = 0; |
| 26 |
Static boolean inputerror = false; |
| 27 |
Static short reportitem = reportnewfile + reportoldfile + reporterror + |
| 28 |
reportrecursive; |
| 29 |
|
| 30 |
|
| 31 |
void report(short items) |
| 32 |
{ |
| 33 |
reportitem = items; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
Static boolean recursive(Char *filename) |
| 38 |
{ |
| 39 |
boolean Result; |
| 40 |
filenode *previous; |
| 41 |
|
| 42 |
if (current == NULL) |
| 43 |
return false; |
| 44 |
previous = current; |
| 45 |
/* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */ |
| 46 |
while (previous != NULL) { |
| 47 |
if (!strcmp(filename, previous->name)) |
| 48 |
return true; |
| 49 |
previous = previous->prev; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
void pushFile(Char *filename) |
| 56 |
{ |
| 57 |
filenode *newnode; |
| 58 |
Char STR3[256]; |
| 59 |
|
| 60 |
if (recursive(filename)) { |
| 61 |
printf("===! Ignoring recursive include of file %s\n", filename); |
| 62 |
return; |
| 63 |
} |
| 64 |
newnode = Malloc(sizeof(filenode)); |
| 65 |
newnode->actualfile = NULL; |
| 66 |
strcpy(newnode->name, filename); |
| 67 |
newnode->prev = current; |
| 68 |
newnode->lineno = 0; |
| 69 |
strcpy(newnode->actualfile_NAME, filename); |
| 70 |
if (newnode->actualfile != NULL) |
| 71 |
newnode->actualfile = freopen(newnode->actualfile_NAME, "r", |
| 72 |
newnode->actualfile); |
| 73 |
else |
| 74 |
newnode->actualfile = fopen(newnode->actualfile_NAME, "r"); |
| 75 |
_SETIO(newnode->actualfile != NULL, FileNotFound); |
| 76 |
inputerror = (P_ioresult != 0); |
| 77 |
if (inputerror) |
| 78 |
Free(newnode); |
| 79 |
else |
| 80 |
current = newnode; |
| 81 |
if (!inputerror && (reportitem & reportnewfile) > 0) |
| 82 |
printf("==>> Input from file %s\n", currentFilename(STR3)); |
| 83 |
if (inputerror && (reportitem & reporterror) > 0) |
| 84 |
printf("==!! Could not open file %s\n", filename); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
void popFile(void) |
| 89 |
{ |
| 90 |
filenode *previous; |
| 91 |
Char STR2[256]; |
| 92 |
Char STR3[256]; |
| 93 |
|
| 94 |
if (current == NULL) |
| 95 |
return; |
| 96 |
if ((reportitem & reportclose) > 0) |
| 97 |
printf("==>> Closing file %s at line number %d\n", |
| 98 |
currentFilename(STR2), currentLineNo()); |
| 99 |
if (current->actualfile != NULL) |
| 100 |
fclose(current->actualfile); |
| 101 |
current->actualfile = NULL; |
| 102 |
previous = current->prev; |
| 103 |
Free(current); |
| 104 |
current = previous; |
| 105 |
if (current != NULL && (reportitem & reportoldfile) > 0) |
| 106 |
printf("==>> Resuming input from file %s at line number %d\n", |
| 107 |
currentFilename(STR3), currentLineNo()); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void closeAll(void) |
| 112 |
{ |
| 113 |
do { |
| 114 |
popFile(); |
| 115 |
} while (current != NULL); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
boolean eofCurrent(void) |
| 120 |
{ |
| 121 |
return P_eof(current->actualfile); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
Char *readLine(Char *Result) |
| 126 |
{ |
| 127 |
Char s[256]; |
| 128 |
Char *TEMP; |
| 129 |
Char STR2[256]; |
| 130 |
|
| 131 |
if (*nextData != '\0') { |
| 132 |
strcpy(Result, nextData); |
| 133 |
*nextData = '\0'; |
| 134 |
return Result; |
| 135 |
} |
| 136 |
if (eofAll()) |
| 137 |
return strcpy(Result, ""); |
| 138 |
_SETIO(fgets(s, 256, current->actualfile) != NULL, EndOfFile); |
| 139 |
TEMP = strchr(s, '\n'); |
| 140 |
if (TEMP != NULL) |
| 141 |
*TEMP = 0; |
| 142 |
strcpy(Result, s); |
| 143 |
inputerror = (P_ioresult != 0); |
| 144 |
if (!inputerror) { |
| 145 |
current->lineno++; |
| 146 |
last_valid_line_no = current->lineno; |
| 147 |
} |
| 148 |
if (inputerror && (reportitem & reporterror) > 0) |
| 149 |
printf("==!! Could not read from file %s\n", currentFilename(STR2)); |
| 150 |
return Result; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
boolean isEmpty(Char *s) |
| 155 |
{ |
| 156 |
short i, FORLIM; |
| 157 |
|
| 158 |
if (*s == '\0') |
| 159 |
return true; |
| 160 |
FORLIM = strlen(s); |
| 161 |
for (i = 0; i <= FORLIM - 1; i++) { |
| 162 |
if (s[i] != ' ') |
| 163 |
return false; |
| 164 |
} |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
Char *readData(Char *Result) |
| 170 |
{ |
| 171 |
Char s[256]; |
| 172 |
|
| 173 |
if (!isEmpty(nextData)) { |
| 174 |
strcpy(Result, nextData); |
| 175 |
*nextData = '\0'; |
| 176 |
return Result; |
| 177 |
} |
| 178 |
while (!eofAll()) { |
| 179 |
readLine(s); |
| 180 |
if (!isEmpty(s)) |
| 181 |
return strcpy(Result, s); |
| 182 |
} |
| 183 |
return strcpy(Result, ""); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
void skipBlanks(void) |
| 188 |
{ |
| 189 |
while (*nextData == '\0') { |
| 190 |
readData(nextData); |
| 191 |
if (eofAll()) |
| 192 |
return; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
boolean eofAll(void) |
| 198 |
{ |
| 199 |
boolean Result; |
| 200 |
|
| 201 |
/* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */ |
| 202 |
if (current == NULL) |
| 203 |
return true; |
| 204 |
if (eofCurrent()) { |
| 205 |
popFile(); |
| 206 |
return (eofAll()); |
| 207 |
} |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
short currentLineNo(void) |
| 213 |
{ |
| 214 |
return last_valid_line_no; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
Char *currentFilename(Char *Result) |
| 219 |
{ |
| 220 |
if (current == NULL) |
| 221 |
return strcpy(Result, "No file open yet"); |
| 222 |
else |
| 223 |
return strcpy(Result, current->name); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
boolean fileError(void) |
| 228 |
{ |
| 229 |
return inputerror; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
/* End. */ |