| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_DB_DB_READER_HH |
|---|
| 21 | #define GUARD_DB_DB_READER_HH |
|---|
| 22 | |
|---|
| 23 | #include "illformed_db.hh" |
|---|
| 24 | #include "../util.hh" |
|---|
| 25 | |
|---|
| 26 | #include <boost/noncopyable.hpp> |
|---|
| 27 | #include <iostream> |
|---|
| 28 | |
|---|
| 29 | namespace db |
|---|
| 30 | { |
|---|
| 31 | class rollback {}; |
|---|
| 32 | |
|---|
| 33 | class db_reader : public boost::noncopyable |
|---|
| 34 | { |
|---|
| 35 | std::istream &dbfile; |
|---|
| 36 | boost::scoped_ptr<std::string> line_pushed_back; |
|---|
| 37 | void prim_readline(std::string &line) { |
|---|
| 38 | if (line_pushed_back) |
|---|
| 39 | { |
|---|
| 40 | line = *line_pushed_back; |
|---|
| 41 | line_pushed_back.reset(); |
|---|
| 42 | } |
|---|
| 43 | else |
|---|
| 44 | std::getline(dbfile, line); |
|---|
| 45 | } |
|---|
| 46 | public: |
|---|
| 47 | db_reader(std::istream &is) : dbfile(is) {} |
|---|
| 48 | void readline(std::string &line) { |
|---|
| 49 | prim_readline(line); |
|---|
| 50 | if (line.substr(0, 7) == ".BEGIN ") throw rollback(); |
|---|
| 51 | } |
|---|
| 52 | void pushback_line(std::string line) { |
|---|
| 53 | BOOST_ASSERT(!line_pushed_back); |
|---|
| 54 | line_pushed_back.reset(new std::string(line)); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | std::string readline(std::string &line, std::string cmd){ |
|---|
| 58 | readline(line); |
|---|
| 59 | util::strip(line); |
|---|
| 60 | if (line.substr(0, cmd.length()+1) != cmd + " ") |
|---|
| 61 | throw illformed_db(cmd + " missing"); |
|---|
| 62 | return line.substr(cmd.length()+1); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void endrec(std::string &line) { |
|---|
| 66 | prim_readline(line); |
|---|
| 67 | if (!dbfile || line.substr(0, 7) == ".BEGIN ") |
|---|
| 68 | throw rollback(); |
|---|
| 69 | util::strip(line); |
|---|
| 70 | if (line != ".END") throw illformed_db(".END missing"); |
|---|
| 71 | } |
|---|
| 72 | bool valid() { return dbfile; } |
|---|
| 73 | }; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | #endif |
|---|