1 |
#ifndef TXTPRT_IMPORT |
2 |
#define TXTPRT_IMPORT |
3 |
#include <iostream> |
4 |
#include <fstream> |
5 |
#include <string> |
6 |
#include <cstdio> |
7 |
|
8 |
#include "entity.h" |
9 |
|
10 |
struct txtprt_i_exception { |
11 |
txtprt_i_exception (std::string m) : msg (m) { } |
12 |
std::string msg; |
13 |
}; |
14 |
|
15 |
class txtprt_parser { |
16 |
std::ifstream ifs; |
17 |
int cur_line; |
18 |
std::string line_data; |
19 |
|
20 |
public: |
21 |
txtprt_parser () {} |
22 |
|
23 |
entity_base* read (std::string filename); |
24 |
inline void match_char (char c) { |
25 |
char o; |
26 |
match_achar (o); |
27 |
if (o != c) |
28 |
throw txtprt_i_exception ((std::string)"Fileformat invalid, expected '" + c + "' got: '" + o + "' -> '" + line_data.substr (0, 10) + "..."); |
29 |
} |
30 |
inline void match_achar (char &c) { |
31 |
std::string s = line_data.substr (0, 1); |
32 |
c = s[0]; |
33 |
cout << "Char: " << c << endl; |
34 |
line_data.erase(0, 1); |
35 |
} |
36 |
|
37 |
inline void match_space () { match_char (' '); } |
38 |
inline void match_nl () { match_char ('\n'); } |
39 |
inline void match_number (double &d) { |
40 |
size_t p = line_data.find_first_not_of ("0123456789.-+"); |
41 |
std::string tmp = line_data.substr (0, p); |
42 |
line_data.erase (0, p); |
43 |
d = strtod (tmp.c_str (), NULL); |
44 |
cout << "NUM: " << d << endl; |
45 |
printf("%f\n", d); |
46 |
} |
47 |
inline void match_section (char c, std::string §) { // c - section char, sect - name of section |
48 |
match_char (c); match_space (); |
49 |
size_t p = line_data.find_first_of ("\n"); |
50 |
sect = line_data.substr (0, p); |
51 |
line_data.erase (0, p); |
52 |
match_nl (); |
53 |
} |
54 |
inline void match_section (char c, double &num) { // c - section char, sect - name of section |
55 |
match_char (c); match_space (); |
56 |
match_number (num); |
57 |
match_nl (); |
58 |
} |
59 |
inline void match_asection (char & c, std::string §) { // c - section char, sect - name of section |
60 |
match_achar (c); match_space (); |
61 |
size_t p = line_data.find_first_of ("\n"); |
62 |
sect = line_data.substr (0, p); |
63 |
line_data.erase (0, p); |
64 |
match_nl (); |
65 |
} |
66 |
inline void match_asection (char & c, double &num) { // c - section char, sect - name of section |
67 |
match_achar (c); match_space (); |
68 |
match_number (num); |
69 |
match_nl (); |
70 |
} |
71 |
}; |
72 |
|
73 |
#endif |