[6] | 1 | #include <vrmllib/file.h> |
---|
| 2 | |
---|
| 3 | #include <stdexcept> |
---|
| 4 | #include <stack> |
---|
| 5 | #include <algorithm> |
---|
| 6 | |
---|
| 7 | #include <vrmllib/nodes.h> |
---|
| 8 | #include "commentstream.h" |
---|
| 9 | |
---|
| 10 | namespace vrmllib { |
---|
| 11 | namespace { |
---|
| 12 | |
---|
| 13 | void set_parent(std::vector<node *> &c, grouping_node *p) |
---|
| 14 | { |
---|
| 15 | for (std::vector<node *>::iterator i=c.begin(); i!=c.end(); ++i) { |
---|
| 16 | if (!*i) |
---|
| 17 | continue; |
---|
| 18 | (*i)->parent = p; |
---|
| 19 | if (grouping_node *n = dynamic_cast<grouping_node *>(*i)) |
---|
| 20 | set_parent(n->children, n); |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | } // anonymous namespace |
---|
| 25 | |
---|
| 26 | file::file(std::istream &sarg) |
---|
| 27 | { |
---|
| 28 | utillib::commentstream s(sarg, '#', '\n'); |
---|
| 29 | std::ios::iostate exceptions = sarg.exceptions(); |
---|
| 30 | sarg.exceptions(std::ios::goodbit); |
---|
| 31 | |
---|
| 32 | try { |
---|
| 33 | for (;;) { |
---|
| 34 | char t; |
---|
| 35 | s >> t; |
---|
| 36 | if (s) |
---|
| 37 | s.putback(t); |
---|
| 38 | else if (s.eof()) |
---|
| 39 | break; |
---|
| 40 | else |
---|
| 41 | throw std::runtime_error( |
---|
| 42 | "parse error: unexpected end of file"); |
---|
| 43 | |
---|
| 44 | s.exceptions(std::ios::badbit |
---|
| 45 | | std::ios::failbit |
---|
| 46 | | std::ios::eofbit); |
---|
| 47 | |
---|
| 48 | std::string word; |
---|
| 49 | s >> word; |
---|
| 50 | |
---|
| 51 | if (word == "ROUTE") { |
---|
| 52 | s >> word; |
---|
| 53 | s >> word; // TO |
---|
| 54 | s >> word; |
---|
| 55 | } else { |
---|
| 56 | node *n = node::parse_node_xdef(s, *this, word); |
---|
| 57 | if (n) |
---|
| 58 | roots.push_back(n); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | s.exceptions(std::ios::goodbit); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | } catch (std::ios::failure &e) { |
---|
| 65 | sarg.exceptions(exceptions); |
---|
| 66 | throw std::runtime_error(std::string("parse error: stream failure: ") + e.what()); |
---|
| 67 | } catch (...) { |
---|
| 68 | sarg.exceptions(exceptions); |
---|
| 69 | throw; |
---|
| 70 | } |
---|
| 71 | sarg.exceptions(exceptions); |
---|
| 72 | |
---|
| 73 | set_parent(roots, 0); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | namespace { |
---|
| 77 | inline void delnode(node *n) { delete n; } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | file::~file() |
---|
| 81 | { |
---|
| 82 | std::for_each(nodes.begin(), nodes.end(), delnode); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | } // namespace vrmllib |
---|