-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_parser.h
More file actions
110 lines (101 loc) · 2.9 KB
/
db_parser.h
File metadata and controls
110 lines (101 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef DBPARSER_H
#define DBPARSER_H
#include <iostream>
#include <string>
#include <map>
#include "datastore.h"
#include "product.h"
#include "user.h"
#include "product_parser.h"
/**
* Interface for all section parsers_
* A section parser parses everything between
* <section> ... </section> in the database file
*/
class SectionParser {
public:
virtual ~SectionParser() { }
/**
* Parses the section stored in the given istream
* Increments lineno after successfully parsing each lineno
* Sets errorMsg with a descriptive message upon an error
* Returns true if an error occurred.
*/
virtual bool parse(std::istream& is, DataStore& ds,
int& lineno, std::string& errorMsg) = 0;
/**
* Reports how many items were parsed
*/
virtual void reportItemsRead(std::ostream& os) = 0;
};
/**
* Main parser utilizing section parsers to do most
* of the actual parsing. Breaks the database into
* sections and handle error output
*/
class DBParser {
public:
DBParser();
~DBParser();
/**
* Registers a section parser that will be invoked
* when a section with sectionName is found in the
* database
*/
void addSectionParser(const std::string& sectionName,
SectionParser* parser);
/**
* Registers a section parser that will be invoked
* when a section with sectionName is found in the
* database. Returns true if an error occurs.
*/
bool parse(std::string db_filename, DataStore& ds);
private:
enum PState { FIND_SECTION, IN_SECTION };
int lineno_;
std::string errorMsg_;
bool error_;
std::map<std::string, SectionParser*> parsers_;
};
/**
* Product section parser. Uses separate ProductParsers
* to parse each type of product
*/
class ProductSectionParser : public SectionParser
{
public:
ProductSectionParser();
~ProductSectionParser();
virtual bool parse(std::istream& is, DataStore& ds,
int& lineno, std::string& errorMsg);
virtual void reportItemsRead(std::ostream& os);
void addProductParser(ProductParser* p);
protected:
Product* parseProduct(const std::string& category,
std::istream& is,
int& lineno,
std::string& errorMsg);
private:
std::map<std::string, ProductParser*> prodParsers_;
unsigned int numRead_ ;
};
/**
* User section parser that parses and creates Users
*/
class UserSectionParser : public SectionParser
{
public:
UserSectionParser();
~UserSectionParser() {}
virtual bool parse(std::istream& is, DataStore& ds,
int& lineno, std::string& errorMsg);
virtual void reportItemsRead(std::ostream& os);
protected:
User* parseUser(
std::istream& is,
DataStore& ds,
std::string& errorMsg);
private:
unsigned int numRead_ ;
};
#endif