ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/txtprt_import.h
Revision: 1.3
Committed: Sun Oct 3 03:03:43 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: knowngood, old_matrix
Changes since 1.2: +1 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
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* 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 line_data.erase(0, 1);
34 }
35
36 inline void match_space () { match_char (' '); }
37 inline void match_nl () { match_char ('\n'); }
38 inline void match_number (double &d) {
39 size_t p = line_data.find_first_not_of ("0123456789.-+e");
40 std::string tmp = line_data.substr (0, p);
41 line_data.erase (0, p);
42 d = strtod (tmp.c_str (), NULL);
43 }
44 inline void match_section (char c, std::string &sect) { // c - section char, sect - name of section
45 match_char (c); match_space ();
46 size_t p = line_data.find_first_of ("\n");
47 sect = line_data.substr (0, p);
48 line_data.erase (0, p);
49 match_nl ();
50 }
51 inline void match_section (char c, double &num) { // c - section char, sect - name of section
52 match_char (c); match_space ();
53 match_number (num);
54 match_nl ();
55 }
56 inline void match_asection (char & c, std::string &sect) { // c - section char, sect - name of section
57 match_achar (c); match_space ();
58 size_t p = line_data.find_first_of ("\n");
59 sect = line_data.substr (0, p);
60 line_data.erase (0, p);
61 match_nl ();
62 }
63 inline void match_asection (char & c, double &num) { // c - section char, sect - name of section
64 match_achar (c); match_space ();
65 match_number (num);
66 match_nl ();
67 }
68 };
69
70 #endif