| Bsoft 2.0.5 Bernard's software package

mdoc.h
Go to the documentation of this file.
1
7 #include <iomanip>
8 #include <sstream>
9
10 #include "json.h"
11 #include "utilities.h"
12
13 using namespace std;
14
15 #ifndef _MDOC_
16
17
18 class MDOCparser {
19 private:
20 string s, f;
21 void get_clean_line(ifstream& fmdoc) {
22 if ( fmdoc.eof() ) return;
23 getline(fmdoc, s);
24 s.erase(remove(s.end()-1, s.end(), '\r'), s.end());
25 // cout << s << endl;
26 }
27 void read_object(JSvalue& obj) {
28 if ( s.length() < 3 ) return;
29 size_t i(s.find_first_of("="));
30 string tag(s.substr(0,i-1));
31 i = s.find_first_not_of(" \t", i+1);
32 obj[tag] = parse_string(s.substr(i));
33 }
34 JSvalue read_section(ifstream& fmdoc) {
35 JSvalue obj(JSobject);
36 s = s.substr(1,s.length()-2);
37 // cout << "Reading section: " << s << endl;
38 while ( !fmdoc.eof() && s[0] != '[' ) {
39 read_object(obj);
40 get_clean_line(fmdoc);
41 }
42 return obj;
43 }
44 JSvalue parse_string(string s) {
45 // Remove any quotes
46 s.erase( remove( s.begin(), s.end(), '\"' ), s.end() );
47 istringstream ss(s);
48 vector<string> sv((istream_iterator<string>(ss)), istream_iterator<string>());
49 int ft(0);
50 for ( auto it = s.begin(); it!=s.end(); ++it ) {
51 if ( isalpha(*it) ) return JSvalue(s);
52 else if ( *it == '.' ) ft = 1;
53 }
54 if ( sv.size() > 1 ) {
55 // cout << "#" << s << "#" << tab << sv.size() << endl;
56 if ( ft ) {
57 vector<double> d(sv.size());
58 try { for ( size_t i=0; i<sv.size(); ++i ) d[i] = stod(sv[i]); }
59 catch (...) { fail("No conversion to a real number."); }
60 return JSvalue(d);
61 } else {
62 vector<long> l(sv.size());
63 try { for ( size_t i=0; i<sv.size(); ++i ) l[i] = stod(sv[i]); }
64 catch (...) { fail("No conversion to an integer."); }
65 return JSvalue(l);
66 }
67 } else {
68 if ( ft ) {
69 double d;
70 try { d = stod(s); }
71 catch (...) { fail("No conversion to a real number."); }
72 return JSvalue(d);
73 } else {
74 long l;
75 try { l = stol(s); }
76 catch (...) { fail("No conversion to an integer."); }
77 return JSvalue(l);
78 }
79 }
80 }
81 void fail(string msg) {
82 cerr << "MDOC parser error: " << msg << endl;
83 exit(-1);
84 }
85 public:
86 MDOCparser() {};
87 MDOCparser(string filename) { f = filename; }
88 JSvalue parse() {
89 if ( f.length() < 1 ) fail("No filename specified!");
90 return parse(f);
91 }
92 JSvalue parse(string filename) {
93 f = filename;
94 // cout << "Reading " << f << endl;
95 ifstream fmdoc(f);
96 if ( fmdoc.fail() ) fail("File " + f + " not opened");
97
98 JSvalue root(JSobject);
99 JSvalue sections(JSarray);
100
101 get_clean_line(fmdoc);
102
103 while ( !fmdoc.eof() ) {
104 if ( s[0] == '[' ) sections.push_back(read_section(fmdoc));
105 else {
106 read_object(root);
107 get_clean_line(fmdoc);
108 }
109 // cout << "***" << s << endl;
110 }
111
112 root["Sections"] = sections;
113
114 // cout << "Number of sections: " << sections.size() << endl;
115
116 fmdoc.close();
117
118 // cout << "done " << f << endl;
119
120 return root;
121 }
122 };
123
124 #define _MDOC_
125 #endif
JSvalue::erase
vector< JSvalue >::iterator erase(long j)
Definition: json.h:183
utilities.h
Header file for general utilities.
MDOCparser::MDOCparser
MDOCparser()
Definition: mdoc.h:86
MDOCparser::parse
JSvalue parse(string filename)
Definition: mdoc.h:92
MDOCparser::MDOCparser
MDOCparser(string filename)
Definition: mdoc.h:87
MDOCparser::parse
JSvalue parse()
Definition: mdoc.h:88
json.h
JSobject
Definition: json.h:26
JSvalue
Definition: json.h:53
JSarray
Definition: json.h:27
JSvalue::push_back
void push_back(const JSvalue &val)
Definition: json.h:191
MDOCparser
Definition: mdoc.h:18