18 #include "FortranFormat.h" 21 FortranFormat::FortranFormat()
27 void FortranFormat::SetInputFile(
IFILE & file)
34 void FortranFormat::SetFormat(
const String & formatString)
36 format = formatString;
48 for (
int i = 0; i < formatString.Length(); i++)
50 if (formatString[i] ==
' ' || formatString[i] ==
'\t' ||
51 formatString[i] ==
'\n' || formatString[i] ==
'\r')
54 if (formatString[i] ==
'(')
57 if (formatString[i] ==
')')
60 format += formatString[i];
62 if (level == 0)
break;
65 if (format[0] !=
'(' || format[format.Length() - 1] !=
')')
66 error(
"Invalid FORTRAN format statement\n\n" 67 "The statement \"%s\" is not bracketed correctly.\n",
68 (
const char *) formatString);
77 bracketCounter.Clear();
81 int FortranFormat::GetNextInteger()
85 return buffer.AsInteger();
88 char FortranFormat::GetNextCharacter()
95 void FortranFormat::GetNextField(
String & field)
97 while (!ProcessToken(field))
101 bool FortranFormat::ProcessToken(
String & field)
104 endOfPattern =
false;
109 inputLine.ReadLine(input);
114 if (repeatCount == 0)
115 repeatCount = GetIntegerFromFormat();
118 if (repeatCount == 0)
121 int repeatPos = formatPos;
124 if (format[formatPos] ==
'(')
128 bracketStack.Push(formatPos);
129 bracketCounter.Push(repeatCount);
130 bracketCount.Push(repeatCount);
138 if (format[formatPos] ==
'X')
146 inputPos += repeatCount;
157 if (format[formatPos] ==
'/')
165 while (repeatCount--)
166 inputLine.ReadLine(input);
171 if (format[formatPos] ==
',' || format[formatPos] ||
')')
178 if (format[formatPos] ==
'Q' || format[formatPos] ==
'P' || format[formatPos] ==
'B')
182 int problemStart = formatPos;
184 while (format[formatPos] !=
',' && format[formatPos] !=
')' && format[formatPos] !=
'/')
187 error(
"Unsupported pattern in FORMAT statement\n\n" 188 "Statement \"%s\" includes unsupporterd pattern '%s'\n",
189 (
const char *) format,
190 (
const char *) format.SubStr(problemStart, formatPos - problemStart));
193 if (format[formatPos] ==
':')
197 if (format[formatPos] ==
',' || format[formatPos] ||
')')
210 int typeStart = formatPos;
212 while (CharacterFollows())
215 int typeLen = formatPos - typeStart;
218 int width = GetIntegerFromFormat();
221 error(
"Unrecognized FORMAT statement\n\n" 222 "Statement \"%s\" is missing a width specifier for a field of type '%s'\n",
223 (
const char *) format, (
const char *) format.SubStr(typeStart, typeLen));
226 if (format[typeStart] ==
'T')
229 if (format[typeStart + 1] ==
'L')
230 inputPos = width > inputPos ? 0 : inputPos - width;
232 else if (format[typeStart + 1] ==
'R')
241 formatPos = repeatPos;
249 field.Copy(inputLine, inputPos, width);
257 formatPos = repeatPos;
264 int FortranFormat::GetIntegerFromFormat()
268 while (DigitFollows())
269 result = result * 10 + (int)(format[formatPos++] -
'0');
274 bool FortranFormat::DigitFollows()
276 return (format[formatPos] >=
'0') && (format[formatPos] <=
'9');
279 bool FortranFormat::CharacterFollows()
281 return (format[formatPos] >=
'A') && (format[formatPos] <=
'Z');
284 void FortranFormat::RejectWidth(
char ch)
288 error(
"Unrecognized FORTRAN format statement\n\n" 289 "The statement \"%s\" includes width specifier for field of type '%c'.\n",
290 (
const char *) format, ch);
293 void FortranFormat::FinishField(
bool)
296 while (format[formatPos] !=
',' && format[formatPos] !=
')')
298 if (format[formatPos] ==
'/')
305 if (format[formatPos] ==
',')
313 if (bracketStack.Length())
316 lastBracket = bracketStack.Pop();
317 lastCount = bracketCount.Pop();
318 int lastCounter = bracketCounter.Pop() - 1;
323 bracketStack.Push(lastBracket);
324 bracketCount.Push(lastCount);
325 bracketCounter.Push(lastCounter);
327 formatPos = lastBracket;
344 formatPos = lastBracket;
346 if (lastBracket == 1)
350 bracketStack.Push(lastBracket);
351 bracketCounter.Push(lastCount);
352 bracketCount.Push(lastCount);
356 void FortranFormat::Flush()
358 while (!endOfPattern)
359 ProcessToken(buffer);
369 bracketStack.Clear();
370 bracketCounter.Clear();
371 bracketCount.Clear();