1 |
#ifndef TXTPRT_IMPORT |
2 |
#define TXTPRT_IMPORT |
3 |
|
4 |
#include <iostream> |
5 |
#include <fstream> |
6 |
#include <string> |
7 |
#include <cstdio> |
8 |
|
9 |
#include "entity.h" |
10 |
|
11 |
struct txtprt_i_exception |
12 |
{ |
13 |
txtprt_i_exception (std::string m) |
14 |
: msg(m) |
15 |
{ |
16 |
} |
17 |
|
18 |
std::string msg; |
19 |
}; |
20 |
|
21 |
class txtprt_parser |
22 |
{ |
23 |
std::ifstream ifs; |
24 |
int cur_line; |
25 |
std::string line_data; |
26 |
|
27 |
public: |
28 |
txtprt_parser () |
29 |
{ |
30 |
} |
31 |
|
32 |
geometry *read (std::string filename); |
33 |
inline void match_char (char c) |
34 |
{ |
35 |
char o; |
36 |
match_achar (o); |
37 |
if (o != c) |
38 |
throw txtprt_i_exception ((std::string) "Fileformat invalid, expected '" |
39 |
+ c + "' got: '" + o + "' -> '" + |
40 |
line_data.substr (0, 10) + "..."); |
41 |
} |
42 |
|
43 |
inline void match_achar (char &c) |
44 |
{ |
45 |
std::string s = line_data.substr (0, 1); |
46 |
c = s[0]; |
47 |
line_data.erase (0, 1); |
48 |
} |
49 |
|
50 |
inline void match_space () |
51 |
{ |
52 |
match_char (' '); |
53 |
} |
54 |
|
55 |
inline void match_nl () |
56 |
{ |
57 |
match_char ('\n'); |
58 |
} |
59 |
|
60 |
inline void match_number (double &d) |
61 |
{ |
62 |
size_t p = line_data.find_first_not_of ("0123456789.-+e"); |
63 |
std::string tmp = line_data.substr (0, p); |
64 |
line_data.erase (0, p); |
65 |
d = strtod (tmp.c_str (), NULL); |
66 |
} |
67 |
|
68 |
inline void match_section (char c, std::string §) |
69 |
{ // c - section char, sect - name of section |
70 |
match_char (c); |
71 |
match_space (); |
72 |
size_t p = line_data.find_first_of ("\n"); |
73 |
sect = line_data.substr (0, p); |
74 |
line_data.erase (0, p); |
75 |
match_nl (); |
76 |
} |
77 |
|
78 |
inline void match_section (char c, double &num) |
79 |
{ // c - section char, sect - name of section |
80 |
match_char (c); |
81 |
match_space (); |
82 |
match_number (num); |
83 |
match_nl (); |
84 |
} |
85 |
|
86 |
inline void match_asection (char &c, std::string §) |
87 |
{ // c - section char, sect - name of section |
88 |
match_achar (c); |
89 |
match_space (); |
90 |
size_t p = line_data.find_first_of ("\n"); |
91 |
sect = line_data.substr (0, p); |
92 |
line_data.erase (0, p); |
93 |
match_nl (); |
94 |
} |
95 |
|
96 |
inline void match_asection (char &c, double &num) |
97 |
{ // c - section char, sect - name of section |
98 |
match_achar (c); |
99 |
match_space (); |
100 |
match_number (num); |
101 |
match_nl (); |
102 |
} |
103 |
}; |
104 |
|
105 |
#endif |
106 |
|