| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #ifndef IFFPARSER_ |
|---|
| 9 | #define IFFPARSER_ |
|---|
| 10 | |
|---|
| 11 | #include <vector> |
|---|
| 12 | #include <iostream> |
|---|
| 13 | |
|---|
| 14 | namespace iff |
|---|
| 15 | { |
|---|
| 16 | |
|---|
| 17 | typedef std::vector<struct Chunk *> Chunk_list; |
|---|
| 18 | |
|---|
| 19 | struct Chunk { |
|---|
| 20 | |
|---|
| 21 | virtual ~Chunk() |
|---|
| 22 | { |
|---|
| 23 | Chunk_list *fod = free_on_destroy(); |
|---|
| 24 | if (fod) { |
|---|
| 25 | for (Chunk_list::iterator i=fod->begin(); i!=fod->end(); ++i) { |
|---|
| 26 | delete *i; |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | virtual Chunk_list *free_on_destroy() { return 0; } |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | template<typename Iter> |
|---|
| 35 | class GenericParser { |
|---|
| 36 | public: |
|---|
| 37 | GenericParser(); |
|---|
| 38 | GenericParser(std::ostream &os); |
|---|
| 39 | |
|---|
| 40 | virtual ~GenericParser(); |
|---|
| 41 | |
|---|
| 42 | void clear(); |
|---|
| 43 | void parse(Iter begin, Iter end); |
|---|
| 44 | |
|---|
| 45 | inline const Chunk_list &chunks() const; |
|---|
| 46 | |
|---|
| 47 | protected: |
|---|
| 48 | virtual Chunk *parse_chunk_data(const std::string &tag, const std::string &context, Iter it, Iter end) = 0; |
|---|
| 49 | Chunk *parse_chunk(Iter &it, const std::string &context); |
|---|
| 50 | |
|---|
| 51 | inline std::ostream &os() { return os_; } |
|---|
| 52 | |
|---|
| 53 | private: |
|---|
| 54 | GenericParser& operator = (const GenericParser&) { return *this; } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | Chunk_list chunks_; |
|---|
| 58 | std::ostream &os_; |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | # define IP_TMP template<class Iter> |
|---|
| 65 | |
|---|
| 66 | IP_TMP GenericParser<Iter>::GenericParser() |
|---|
| 67 | : os_(std::cout) |
|---|
| 68 | { |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | IP_TMP GenericParser<Iter>::GenericParser(std::ostream &os) |
|---|
| 72 | : os_(os) |
|---|
| 73 | { |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | IP_TMP GenericParser<Iter>::~GenericParser() |
|---|
| 77 | { |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | IP_TMP void GenericParser<Iter>::clear() |
|---|
| 81 | { |
|---|
| 82 | chunks_.clear(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | IP_TMP void GenericParser<Iter>::parse(Iter begin, Iter end) |
|---|
| 86 | { |
|---|
| 87 | Iter it = begin; |
|---|
| 88 | while (it < end) { |
|---|
| 89 | Chunk *chk = parse_chunk(it, ""); |
|---|
| 90 | if (chk) chunks_.push_back(chk); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | IP_TMP Chunk *GenericParser<Iter>::parse_chunk(Iter &it, const std::string &context) |
|---|
| 95 | { |
|---|
| 96 | std::string tag; |
|---|
| 97 | for (int i=0; i<4; ++i) tag += *(it++); |
|---|
| 98 | unsigned int len = ((static_cast<unsigned int>(*(it)) & 0xFF) << 24) | |
|---|
| 99 | ((static_cast<unsigned int>(*(it+1)) & 0xFF) << 16) | |
|---|
| 100 | ((static_cast<unsigned int>(*(it+2)) & 0xFF) << 8) | |
|---|
| 101 | (static_cast<unsigned int>(*(it+3)) & 0xFF); |
|---|
| 102 | it += 4; |
|---|
| 103 | os_ << "DEBUG INFO: iffparser: reading chunk " << tag << ", length = " << len << ", context = " << context << "\n"; |
|---|
| 104 | Chunk *chk = parse_chunk_data(tag, context, it, it+len); |
|---|
| 105 | if (!chk) os_ << "DEBUG INFO: iffparser: \tprevious chunk not handled\n"; |
|---|
| 106 | it += len; |
|---|
| 107 | if ((len % 2) != 0) ++it; |
|---|
| 108 | return chk; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | IP_TMP const Chunk_list &GenericParser<Iter>::chunks() const |
|---|
| 112 | { |
|---|
| 113 | return chunks_; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | #endif |
|---|