X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=odcread.cc;h=e790fdd36bcdc3b211d3c4d277cf9462ee6db3dd;hb=c71568094a35bbda781ac2070e50a59a3b5ac9f7;hp=6d506685882012eef28356b70f36ef21e8fd51cb;hpb=bfa2551119497089b987c2bce3e2f512f9e2130f;p=odcread.git diff --git a/odcread.cc b/odcread.cc index 6d50668..e790fdd 100644 --- a/odcread.cc +++ b/odcread.cc @@ -1,12 +1,96 @@ #include #include #include +#include #include #include #include +#include +#include namespace odc { + class Context { + public: + virtual void addPiece(std::string &piece) = 0; + virtual std::string getPlainText() const = 0; + }; + class PartContext : public Context { + private: + std::string d_text; + public: + virtual void addPiece(std::string &piece) { + d_text += piece; + } + virtual std::string getPlainText() const { + return d_text; + } + }; + class FoldContext : public Context { + private: + bool d_collapsed; + bool d_haveFirst; // flag that first part has been set + std::string d_firstPart; + std::string d_remainder; + public: + FoldContext(bool collapsed) : d_collapsed(collapsed), d_haveFirst(false) {} + virtual void addPiece(std::string &piece) { + if (!d_haveFirst) { + d_haveFirst = true; + d_firstPart = piece; + } else { + d_remainder += piece; + } + } + virtual std::string getPlainText() const { + if (d_collapsed) { + return std::string("##=>") + d_remainder + "\n" + d_firstPart +"##<="; + } else { + return std::string("##=>") + d_firstPart + "\n" + d_remainder +"##<="; + } + } + }; + + class MyVisitor : public Visitor { + private: + std::stack d_context; + + void terminateContext() { + Context *c = d_context.top(); + d_context.pop(); + if (d_context.empty()) { + std::cout << c->getPlainText() << std::endl; + } else { + std::string text = c->getPlainText(); + d_context.top()->addPiece(text); + } + delete c; + } + + public: + virtual void partStart() { + d_context.push(new PartContext()); + } + virtual void partEnd() { + terminateContext(); + } + virtual void foldLeft(bool collapsed) { + d_context.push(new FoldContext(collapsed)); + } + virtual void foldRight() { + terminateContext(); + } + virtual void textShortPiece(const ShortPiece *piece) { + std::string text = piece->getText(); + d_context.top()->addPiece(text); + } + virtual void textLongPiece(const LongPiece *piece) { + throw "Long Piece not handled"; + //std::string text = piece->getText(); + //d_context.top()->addPiece(text); + } + }; + Store* importDocument(std::istream &is) { const INTEGER docTag = 0x6F4F4443; const INTEGER docVersion = 0; @@ -30,13 +114,16 @@ int main(int argc, char *argv[]) { } std::ifstream in(argv[1], std::ios::in | std::ios::binary); odc::Store* s = odc::importDocument(in); - std::cout << std::endl << std::endl; +// std::cout << s->toPlainText() << std::endl; +// std::cout << std::endl << std::endl; - std::cout << s->toString() << std::endl; - std::cout << in.tellg() << " " << in.eof() << std::endl; + odc::MyVisitor visitor; + s->accept(visitor); +// std::cout << s->toString() << std::endl; +// std::cout << in.tellg() << " " << in.eof() << std::endl; - odc::TypePath path; - odc::ContainerModel(0).getTypePath(&path); - std::cout << path.toString() << std::endl; +// odc::TypePath path; +// odc::ContainerModel(0).getTypePath(&path); +// std::cout << path.toString() << std::endl; return 0; }