libsurf
Programmer's Documentation

stepline.h (r6227/r5337)
1 
2 /* Copyright (C) 2015 David Eller <david@larosterna.com>
3  *
4  * Commercial License Usage
5  * Licensees holding valid commercial licenses may use this file in accordance
6  * with the terms contained in their respective non-exclusive license agreement.
7  * For further information contact david@larosterna.com .
8  *
9  * GNU General Public License Usage
10  * Alternatively, this file may be used under the terms of the GNU General
11  * Public License version 3.0 as published by the Free Software Foundation and
12  * appearing in the file gpl.txt included in the packaging of this file.
13  */
14 
15 #ifndef SURF_STEPFILELINE_H
16 #define SURF_STEPFILELINE_H
17 
18 #include "steplistrep.h"
19 #include "step.h"
20 
23 {
24 public:
25 
27  explicit StepFileLine(const char *s) : cursor(s) {}
28 
30  StepFileLine & operator++ () {++cursor; return *this;}
31 
33  const char *current() const {return cursor;}
34 
36  void move(const char *pos) {cursor = pos;}
37 
39  uint entityId();
40 
42  bool skipAttr();
43 
45  const char *entityType();
46 
48  uint entityType(std::string & etype);
49 
51  bool option() const {
52  assert(cursor);
53  return *cursor != '$';
54  }
55 
57  bool parseBool(bool & x);
58 
60  bool parseId(StepID & x);
61 
63  bool parseInt(int & x);
64 
66  bool parseFloat(double & x);
67 
69  bool parseString(std::string & attr);
70 
72  bool parseEnum(const char **beg, const char **end);
73 
75  bool parseSelect(const char **beg, const char **end);
76 
78  bool parseIdArray(StepIDArray & x);
79 
81  bool parseIntArray(StepIntArray & x);
82 
84  bool parseFloatArray(StepRealArray & x);
85 
87  bool parseStringArray(StepStringArray & x);
88 
90  bool parseIDMatrix(StepIDMatrix & x);
91 
94 
96  template <int N>
97  bool parseFloats(double val[]) {
98  StepListRep list = nextList();
99  bool ok = list.parseFloats<N>(val);
100  cursor = list.end();
101  return ok;
102  }
103 
105  template <int N>
106  bool parseInts(int val[]) {
107  StepListRep list = nextList();
108  bool ok = list.parseInts<N>(val);
109  cursor = list.end();
110  return ok;
111  }
112 
114  template <int N>
115  bool parseIds(StepID val[]) {
116  StepListRep list = nextList();
117  bool ok = list.parseIds<N>(val);
118  cursor = list.end();
119  return ok;
120  }
121 
122  // overloads for autogenerated code
123 
124  bool readAttr(bool & x) { return parseBool(x); }
125  bool readAttr(int & x) { return parseInt(x); }
126  bool readAttr(StepID & x) { return parseId(x); }
127  bool readAttr(double & x) { return parseFloat(x); }
128  bool readAttr(std::string & x) { return parseString(x); }
129  bool readAttr(StepIntArray & x) { return parseIntArray(x); }
130  bool readAttr(StepIDArray & x) { return parseIdArray(x); }
131  bool readAttr(StepStringArray & x) { return parseStringArray(x); }
132  bool readAttr(StepRealArray & x) { return parseFloatArray(x); }
133  bool readAttr(StepIDMatrix & x) { return parseIDMatrix(x); }
134  bool readAttr(StepRealMatrix & x) { return parseFloatMatrix(x); }
135 
136  template <int N>
137  bool readArrayAttr(int x[]) {return parseInts<N>(x);}
138 
139  template <int N>
140  bool readArrayAttr(StepID x[]) {return parseIds<N>(x);}
141 
142  template <int N>
143  bool readArrayAttr(double x[]) {return parseFloats<N>(x);}
144 
145  template <class EnumType>
146  bool readEnumArray(std::vector<EnumType> & ev) {
147  StepListRep list = nextList();
148  if (not list.valid())
149  return false;
150  const int n = list.nelements();
151  ev.resize(n);
152  cursor = list.begin() + 1;
153  bool ok = true;
154  for (int i=0; i<n; ++i) {
155  ok &= ev[i].read(*this);
156  }
157  cursor = list.end();
158  return ok;
159  }
160 
161  template <int N, class EnumType>
162  bool readEnumArray(EnumType ev[]) {
163  if (cursor == 0)
164  return false;
165  const char *s = strchr(cursor, '(');
166  if (s == 0)
167  return false;
168  cursor = s+1;
169  bool ok = true;
170  for (int i=0; i<N; ++i) {
171  ok &= ev[i].read(*this);
172  }
173  while (*cursor == ')' or *cursor == ',' or isspace(*cursor))
174  ++cursor;
175  return ok;
176  }
177 
178  template <class EnumType>
179  bool readSelectArray(std::vector<EnumType> & ev) {
180  return readEnumArray(ev);
181  }
182 
183  template <int N, class EnumType>
184  bool readSelectArray(EnumType ev[]) {
185  StepListRep list = nextList();
186  if (not list.valid())
187  return false;
188  const int ne = list.nelements();
189  const int m = std::min(N,ne);
190  cursor = list.begin() + 1;
191  bool ok = true;
192  for (int i=0; i<m; ++i) {
193  ok &= ev[i].read(*this);
194  }
195  cursor = list.end();
196  return ok;
197  }
198 
200  static void writeAttr(std::ostream & os, bool x) {
201  os << (x ? ".T." : ".F.");
202  }
203 
205  static void writeAttr(std::ostream & os, int x) {os << x;}
206 
208  static void writeAttr(std::ostream & os, StepID x, char cundef='*') {
209  if (x == 0)
210  os << cundef;
211  else
212  os << '#' << x;
213  }
214 
216  static void writeAttr(std::ostream & os, double x) {os << x;}
217 
219  static void writeAttr(std::ostream & os, const std::string & x) {
220  os << '\'' << x << '\'';
221  }
222 
224  static void writeAttr(std::ostream & os, const StepIntArray & x);
225 
227  static void writeAttr(std::ostream & os, const StepIDArray & x);
228 
230  static void writeAttr(std::ostream & os, const StepRealArray & x);
231 
233  static void writeAttr(std::ostream & os, const StepStringArray & x);
234 
236  static void writeAttr(std::ostream & os, const StepIDMatrix & x);
237 
239  static void writeAttr(std::ostream & os, const StepRealMatrix & x);
240 
241  template <int N>
242  static void writeArrayAttr(std::ostream & os, const int x[]) {
243  for (int i=0; i<N-1; ++i)
244  os << x[i] << ',';
245  os << x[N-1];
246  }
247 
248  template <int N>
249  static void writeArrayAttr(std::ostream & os, const StepID x[]) {
250  for (int i=0; i<N-1; ++i)
251  os << '#' << x[i] << ',';
252  os << '#' << x[N-1];
253  }
254 
255  template <int N>
256  static void writeArrayAttr(std::ostream & os, const double x[]) {
257  for (int i=0; i<N-1; ++i)
258  os << x[i] << ',';
259  os << x[N-1];
260  }
261 
262  template <class EnumType>
263  static void writeEnumArray(std::ostream & os,
264  const std::vector<EnumType> & ev)
265  {
266  os << '(';
267  const int n = ev.size();
268  for (int i=0; i<n-1; ++i) {
269  ev[i].write(os);
270  os << ',';
271  }
272  ev.back().write(os);
273  os << ')';
274  }
275 
276  template <int N, class EnumType>
277  static void writeEnumArray(std::ostream & os, const EnumType ev[])
278  {
279  os << '(';
280  for (int i=0; i<N-1; ++i) {
281  ev[i].write(os);
282  os << ',';
283  }
284  ev[N-1].write(os);
285  os << ')';
286  }
287 
288  template <class EnumType>
289  static void writeSelectArray(std::ostream & os,
290  const std::vector<EnumType> & ev)
291  {
292  os << '(';
293  const int n = ev.size();
294  int nvalid(0);
295  for (int i=0; i<n; ++i) {
296  if (ev[i].valid()) {
297  if (nvalid > 0)
298  os << ',';
299  ev[i].write(os);
300  ++nvalid;
301  }
302  }
303  os << ')';
304  }
305 
306  template <int N, class EnumType>
307  static void writeSelectArray(std::ostream & os, const EnumType ev[])
308  {
309  os << '(';
310  int nvalid(0);
311  for (int i=0; i<N; ++i) {
312  if (ev[i].valid()) {
313  if (nvalid > 0)
314  os << ',';
315  ev[i].write(os);
316  }
317  }
318  os << ')';
319  }
320 
324  if (list.valid())
325  cursor = list.end();
326  return list;
327  }
328 
329 private:
330 
334  if (list.valid())
335  cursor = list.begin();
336  return list;
337  }
338 
339 private:
340 
342  // const char *line;
343 
345  const char *cursor;
346 };
347 
348 #endif // StepFileLine_H
bool option() const
check whether an optional attribute is present
Definition: stepline.h:51
static void writeAttr(std::ostream &os, const std::string &x)
write attribute
Definition: stepline.h:219
static void writeAttr(std::ostream &os, int x)
write attribute
Definition: stepline.h:205
bool parseSelect(const char **beg, const char **end)
parse a select string
Definition: stepline.cpp:241
uint entityId()
retrieve entity ID and place cursor on content after &quot;=(&quot;
Definition: stepline.cpp:33
bool valid() const
determine whether list was identified
Definition: steplistrep.h:33
const char * current() const
current cursor location
Definition: stepline.h:33
A list in a STEP file line.
Definition: steplistrep.h:25
bool parseBool(bool &x)
parse a boolean value
Definition: stepline.cpp:153
bool parseIDMatrix(StepIDMatrix &x)
retrieve a matrix of IDs
Definition: stepline.cpp:313
static void writeAttr(std::ostream &os, StepID x, char cundef='*')
write attribute
Definition: stepline.h:208
bool parseIntArray(StepIntArray &x)
retrieve a list of IDs
Definition: stepline.cpp:280
StepFileLine(const char *s)
initialize with pointer to first character
Definition: stepline.h:27
bool parseInts(int val[]) const
extract list of integers
Definition: steplistrep.cpp:75
void move(const char *pos)
place cursor at pos
Definition: stepline.h:36
bool parseEnum(const char **beg, const char **end)
parse an enum string
Definition: stepline.cpp:218
uint nelements() const
number of list elements
Definition: steplistrep.h:48
bool parseFloats(double val[]) const
extract list of floating-point values, unknown length
Definition: steplistrep.cpp:95
static void writeAttr(std::ostream &os, double x)
write attribute
Definition: stepline.h:216
StepFileLine & operator++()
increment cursor
Definition: stepline.h:30
const char * entityType()
locate entity type
Definition: stepline.cpp:65
bool parseInts(int val[])
retrieve a fixed-size list of integer values
Definition: stepline.h:106
const char * begin() const
first character of the list &#39;(&#39;
Definition: steplistrep.h:36
static void writeAttr(std::ostream &os, bool x)
write attribute
Definition: stepline.h:200
bool parseId(StepID &x)
parse a single entity id
Definition: stepline.cpp:116
bool parseFloatMatrix(StepRealMatrix &x)
retrieve a matrix of real values
Definition: stepline.cpp:325
bool parseFloatArray(StepRealArray &x)
retrieve a list of real values
Definition: stepline.cpp:291
bool parseFloats(double val[])
retrieve a fixed-size list of real values
Definition: stepline.h:97
StepListRep nextList()
extract list
Definition: stepline.h:332
bool parseInt(int &x)
parse single integer value
Definition: stepline.cpp:137
bool parseString(std::string &attr)
parse a string attribute
Definition: stepline.cpp:186
bool parseIds(StepID val[]) const
extract list of entity ids
Definition: steplistrep.cpp:55
bool parseIdArray(StepIDArray &x)
retrieve a list of IDs
Definition: stepline.cpp:269
const char * cursor
start of the line
Definition: stepline.h:345
StepListRep parseList()
low-level access : extract a list, set cursor to its end
Definition: stepline.h:322
bool parseIds(StepID val[])
retrieve a fixed-size list of integer values
Definition: stepline.h:115
Single line in STEP file.
Definition: stepline.h:22
bool parseFloat(double &x)
parse single float value
Definition: stepline.cpp:170
bool parseStringArray(StepStringArray &x)
retrieve a list of strings
Definition: stepline.cpp:302
bool skipAttr()
skip one attribute
Definition: stepline.cpp:54
const char * end() const
last character of the list &#39;)&#39;
Definition: steplistrep.h:39
Generated on Wed Jan 19 2022 03:03:15 for libsurf by   doxygen 1.8.5