libStatGen Software  1
FortranFormat.h
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __FORTRAN_FORMAT__
19 #define __FORTRAN_FORMAT__
20 
21 #include "StringBasics.h"
22 #include "IntArray.h"
23 
25 {
26 public:
27  // This class reads a user specified input file, one line at a time,
28  // and returns individual fields according to a user specified format
29  // statement
30  FortranFormat();
31 
32  // Set the fortran format statement
33  void SetFormat(const String & formatString);
34 
35  // Set the input file
36  void SetInputFile(IFILE & file);
37 
38  // Read one field from input file
39  void GetNextField(String & field);
40  int GetNextInteger();
41  char GetNextCharacter();
42 
43  // Process a token in format statement and return true
44  // if token corresponds to input field. Return false if
45  // token led to processing of white-space or input line
46  // positioning
47  bool ProcessToken(String & field);
48 
49  // Flush the pattern -- this finishes processing the current
50  // pattern and ensures that all trailing new-lines, etc. are
51  // handled correctly
52  void Flush();
53 
54 private:
55  // The input line and current position along it
56  String inputLine;
57  int inputPos;
58 
59  // The Fortran format statement and current position along it
60  String format;
61  int formatPos;
62 
63  // The position of the pattern we are repeating, if any
64  int repeatCount;
65 
66  // Returns an integer from the current format statement, if any
67  int GetIntegerFromFormat();
68 
69  // These functions check the next character in format string
70  bool DigitFollows();
71  bool CharacterFollows();
72 
73  // This function finish the input field
74  void FinishField(bool haveSlash = false);
75 
76  // Reject width were appropriate
77  void RejectWidth(char type);
78 
79  // The input file
80  IFILE input;
81 
82  // Stacks to keep track of nested parenthesis
83  IntArray bracketStack;
84  IntArray bracketCount;
85  IntArray bracketCounter;
86 
87  int lastBracket;
88  int lastCount;
89 
90  // Buffer for reading fields
91  String buffer;
92 
93  // Flag that indicates whether we have reached end-of-pattern
94  bool endOfPattern;
95 };
96 
97 #endif
98 
99 
String
Definition: StringBasics.h:39
FortranFormat
Definition: FortranFormat.h:25
InputFile
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37
IntArray
Definition: IntArray.h:24