]> git.tdb.fi Git - libs/datafile.git/blob - source/parser.cpp
Add binary data format
[libs/datafile.git] / source / parser.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #include <cctype>
8 #include <sstream>
9 #include "binaryparser.h"
10 #include "parser.h"
11 #include "statement.h"
12 #include "textparser.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace DataFile {
18
19 Parser::Parser(istream &i, const string &s):
20         in(i),
21         src(s),
22         good(true),
23         mode(new TextParser(in, src))
24 { }
25
26 Parser::~Parser()
27 {
28         delete mode;
29 }
30
31 Statement Parser::parse()
32 {
33         if(!good)
34                 throw Exception("Parser is not good");
35
36         try
37         {
38                 while(1)
39                 {
40                         Statement st=mode->parse();
41                         if(st.keyword=="__bin")
42                         {
43                                 delete mode;
44                                 mode=new BinaryParser(in, src);
45                         }
46                         else if(st.keyword=="__text")
47                         {
48                                 delete mode;
49                                 mode=new TextParser(in, src);
50                         }
51                         else
52                                 return st;
53                 }
54         }
55         catch(const Exception &e)
56         {
57                 good=false;
58                 throw;
59         }
60 }
61
62 } // namespace DataFile
63 } // namespace Msp