1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* Copyright (C) 2010 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FortranFormat.h"
#include "Error.h"
FortranFormat::FortranFormat()
{
inputPos = -1;
endOfPattern = false;
}
void FortranFormat::SetInputFile(IFILE & file)
{
input = file;
inputPos = -1;
endOfPattern = false;
}
void FortranFormat::SetFormat(const String & formatString)
{
format = formatString;
inputPos = -1;
endOfPattern = false;
repeatCount = 0;
format.Clear();
// Remove blank spaces from format statement and extract
// the first bracketed expression
int level = 0;
for (int i = 0; i < formatString.Length(); i++)
{
if (formatString[i] == ' ' || formatString[i] == '\t' ||
formatString[i] == '\n' || formatString[i] == '\r')
continue;
if (formatString[i] == '(')
level++;
if (formatString[i] == ')')
level--;
format += formatString[i];
if (level == 0) break;
}
if (format[0] != '(' || format[format.Length() - 1] != ')')
error("Invalid FORTRAN format statement\n\n"
"The statement \"%s\" is not bracketed correctly.\n",
(const char *) formatString);
lastBracket = 1;
lastCount = 0;
formatPos = 1;
repeatCount = 0;
bracketStack.Clear();
bracketCounter.Clear();
bracketCount.Clear();
}
int FortranFormat::GetNextInteger()
{
GetNextField(buffer);
return buffer.AsInteger();
}
char FortranFormat::GetNextCharacter()
{
GetNextField(buffer);
return buffer[0];
}
void FortranFormat::GetNextField(String & field)
{
while (!ProcessToken(field))
;
}
bool FortranFormat::ProcessToken(String & field)
{
// This flag only gets set if we encounter the final bracket or a ':'
endOfPattern = false;
// Read input from file, if appropriate
if (inputPos == -1)
{
inputLine.ReadLine(input);
inputPos = 0;
}
// First read repeat count specifier
if (repeatCount == 0)
repeatCount = GetIntegerFromFormat();
// By default, the repeat count should be 1
if (repeatCount == 0)
repeatCount = 1;
int repeatPos = formatPos;
// Check if this is a new bracketed grouping
if (format[formatPos] == '(')
{
formatPos++;
bracketStack.Push(formatPos);
bracketCounter.Push(repeatCount);
bracketCount.Push(repeatCount);
repeatCount = 0;
return false;
}
// Check if this an 'X' field
if (format[formatPos] == 'X')
{
formatPos++;
// No width specifier allowed for these fields
RejectWidth('X');
// Skip appropriate number of characters
inputPos += repeatCount;
// Reset repeat count
repeatCount = 0;
FinishField();
return false;
}
// Check if this is a '/' (vertical tab field)
if (format[formatPos] == '/')
{
formatPos++;
// No width specifier allowed for these fields
RejectWidth('/');
// Skip the appropriate number of lines
while (repeatCount--)
inputLine.ReadLine(input);
inputPos = 0;
// Separators are optional, so we might already be at the next field
if (format[formatPos] == ',' || format[formatPos] || ')')
FinishField();
return false;
}
// Check that we haven't encountered a rare, but unsupported input type
if (format[formatPos] == 'Q' || format[formatPos] == 'P' || format[formatPos] == 'B')
{
formatPos++;
int problemStart = formatPos;
while (format[formatPos] != ',' && format[formatPos] != ')' && format[formatPos] != '/')
formatPos++;
error("Unsupported pattern in FORMAT statement\n\n"
"Statement \"%s\" includes unsupporterd pattern '%s'\n",
(const char *) format,
(const char *) format.SubStr(problemStart, formatPos - problemStart));
}
if (format[formatPos] == ':')
{
formatPos++;
if (format[formatPos] == ',' || format[formatPos] || ')')
FinishField();
repeatCount = 0;
endOfPattern = true;
return false;
}
// All the other types we recognize include a width specifier
// Identify the location of the type specifier
int typeStart = formatPos;
while (CharacterFollows())
formatPos++;
int typeLen = formatPos - typeStart;
// Retrieve the field width
int width = GetIntegerFromFormat();
if (width == 0)
error("Unrecognized FORMAT statement\n\n"
"Statement \"%s\" is missing a width specifier for a field of type '%s'\n",
(const char *) format, (const char *) format.SubStr(typeStart, typeLen));
// Check for horizontal tab character
if (format[typeStart] == 'T')
{
// Move left by a specified number of characters
if (format[typeStart + 1] == 'L')
inputPos = width > inputPos ? 0 : inputPos - width;
// Move right by a specified number of characters
else if (format[typeStart + 1] == 'R')
inputPos += width;
// Or simply set the appropriate horizontal position
else
inputPos = width;
repeatCount--;
if (repeatCount)
formatPos = repeatPos;
else
FinishField();
return false;
}
// Assume that if we got here, we are looking at a data field!
field.Copy(inputLine, inputPos, width);
field.Trim();
inputPos += width;
repeatCount--;
if (repeatCount)
formatPos = repeatPos;
else
FinishField();
return true;
}
int FortranFormat::GetIntegerFromFormat()
{
int result = 0;
while (DigitFollows())
result = result * 10 + (int)(format[formatPos++] - '0');
return result;
}
bool FortranFormat::DigitFollows()
{
return (format[formatPos] >= '0') && (format[formatPos] <= '9');
}
bool FortranFormat::CharacterFollows()
{
return (format[formatPos] >= 'A') && (format[formatPos] <= 'Z');
}
void FortranFormat::RejectWidth(char ch)
{
// No width allowed for field types 'X' and '\'
if (DigitFollows())
error("Unrecognized FORTRAN format statement\n\n"
"The statement \"%s\" includes width specifier for field of type '%c'.\n",
(const char *) format, ch);
}
void FortranFormat::FinishField(bool)
{
// Find the next field separator
while (format[formatPos] != ',' && format[formatPos] != ')')
{
if (format[formatPos] == '/')
return;
formatPos++;
}
// Skip commas
if (format[formatPos] == ',')
{
formatPos++;
return;
}
// If we found a bracket, then it is either the end of the statement
// (if bracketStack is empty) or we finish an internal grouping
if (bracketStack.Length())
{
// Retrieve information about this grouping
lastBracket = bracketStack.Pop();
lastCount = bracketCount.Pop();
int lastCounter = bracketCounter.Pop() - 1;
// Loop if required
if (lastCounter)
{
bracketStack.Push(lastBracket);
bracketCount.Push(lastCount);
bracketCounter.Push(lastCounter);
formatPos = lastBracket;
}
else
// Otherwise find the next separator
{
formatPos++;
FinishField();
return;
}
}
else
{
// If we finished the input line, then activate reset input counter
inputPos = -1;
endOfPattern = true;
// And re-use input tokens starting at the last bracket
formatPos = lastBracket;
if (lastBracket == 1)
return;
// With appropriate repeat counts
bracketStack.Push(lastBracket);
bracketCounter.Push(lastCount);
bracketCount.Push(lastCount);
}
}
void FortranFormat::Flush()
{
while (!endOfPattern)
ProcessToken(buffer);
inputPos = -1;
lastBracket = 1;
lastCount = 0;
formatPos = 1;
repeatCount = 0;
bracketStack.Clear();
bracketCounter.Clear();
bracketCount.Clear();
}
|