Memosa-FVM  0.2
SchemeReader.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "SchemeReader.h"
6 
7 SchemeReader::SchemeReader(const string& fileName) :
8  Reader(fileName)
9 {}
10 
12 {}
13 
14 char
16 {
17  char c;
18 
19  while ((c = getc(_fp)) != EOF)
20  if (isprint(c) && !isspace(c))
21  break;
22  return(c);
23 }
24 
25 
26 /* move _fp just past next opening paren */
27 int
29 {
30  char c;
31 
32  while ((c = getNextChar()))
33  if ((char) c == '(')
34  return 0;
35  else if (c == EOF)
36  return EOF;
37 
38  return EOF;
39 }
40 
41 /* Move _fp just past closing paren of current list */
42 void
44 {
45  char c;
46 
47  while ((c = getc(_fp)) != EOF)
48  switch (c)
49  {
50  case '(':
52  break;
53  case ')':
54  return;
55  }
56 }
57 
58 int
60 {
61  char c;
62  int parenLevel=0;
63  int buffSize=0;
64 
65 
66  while ((c = getc(_fp)) != EOF)
67  {
68 
69  if (c == '(')
70  parenLevel++;
71 
72  if (parenLevel > 0)
73  {
74  buffSize++;
75  }
76 
77  if (c == ')')
78  parenLevel--;
79 
80  if (parenLevel == 0 && buffSize > 0)
81  return buffSize;
82  }
83 
84  throw CException("EOF reached while reading list");
85 }
86 
87 void
89 {
90  char c;
91  int parenLevel=0;
92  int buffSize=0;
93 
94  while ((c = getc(_fp)) != EOF)
95  {
96  if (c == '(')
97  parenLevel++;
98 
99  if (parenLevel > 0)
100  {
101  buffer[buffSize] = c;
102  buffSize++;
103  }
104 
105  if (c == ')')
106  parenLevel--;
107 
108  if (parenLevel == 0 && buffSize > 0)
109  return;
110  }
111 
112  throw CException("EOF reached while reading list");
113 }
114 
115 void
117 {
118  char c;
119  int id;
120  moveToListOpen();
121  do
122  {
123  while ((c = getc(_fp)) != '\n')
124  if (EOF == c)
125  return;
126  }
127  while (1 != fscanf(_fp,"End of Binary Section %6d",&id));
128 }
129 
130 
131 int
133 {
134  if (moveToListOpen() == EOF)
135  return EOF;
136 
137  int id;
138  if (fscanf(_fp,"%d", &id) != 1)
139  {
140  cerr << "error reading id "<< endl;
141  }
142 
143  return id;
144 }
145 
146 
147 void
149 {
150  moveToListClose();
151 }
152 
153 int
155 {
156  // char c;
157  int id;
158  // moveToListOpen();
159  //moveToListClose();
160  //moveToListOpen();
161  do
162  {
163  getc(_fp);
164 // while ((c = getc(_fp)) != '\n')
165 // if (EOF == c)
166 // {
167 // cerr << "error closing binary section: expected " << currentId
168 // << " , found EOF " << endl;
169 
170 // return EOF;
171 // }
172  }
173  while (1 != fscanf(_fp,"End of Binary Section %d",&id));
174 
175  if (currentId != id)
176  cerr << "error closing binary section: expected " << currentId
177  << " , found " << id << endl;
178  return id;
179 }
180 
181 void
182 SchemeReader::readHeader(int& i1, int& i2, int& i3, int& i4, int& i5)
183 {
184  moveToListOpen();
185 
186  if (fscanf(_fp, "%x%x%x%x",&i1,&i2,&i3,&i4) != 4)
187  {
188  cerr << "error reading header" << endl;
189  // readerError("Error reading the mesh header", DBG_HERE);
190  }
191 
192  /* read shape, not always availabe */
193  if (fscanf(_fp,"%x",&i5) != 1)
194  i5 = -1;
195 
196  moveToListClose();
197 }
198 
199 int
200 SchemeReader::readInt(const bool isBinary)
201 {
202  int i;
203  if (isBinary ?
204  (1 != fread(&i, sizeof(int), 1, _fp)) :
205  (1 != fscanf(_fp,"%x",&i)))
206  cerr << "Error reading int " << endl;
207  return i;
208 }
209 
210 void
211 SchemeReader::skipInt(const int count, const bool isBinary)
212 {
213  int i;
214  for (int n=0; n<count; n++)
215  {
216  if (isBinary ?
217  (1 != fread(&i, sizeof(int), 1, _fp)) :
218  (1 != fscanf(_fp,"%x",&i)))
219  cerr << "Error reading int " << endl;
220  }
221 }
Definition: Reader.h:15
int readInt(const bool isBinary)
FILE * _fp
Definition: Reader.h:26
virtual ~SchemeReader()
void closeSection()
SchemeReader(const string &fileName)
Definition: SchemeReader.cpp:7
int readListLength()
void moveToListClose()
int getNextSection()
int moveToListOpen()
char getNextChar()
void readHeader(int &i1, int &i2, int &i3, int &i4, int &i5)
void moveToListCloseBinary()
void skipInt(const int n, const bool isBinary)
int closeSectionBinary(const int currentId)
void readList(char *buffer)