diff options
author | Fridrich Štrba <fridrich.strba@bluewin.ch> | 2013-03-22 12:30:13 +0100 |
---|---|---|
committer | Fridrich Štrba <fridrich.strba@bluewin.ch> | 2013-03-22 12:30:13 +0100 |
commit | 6b27d3cc5974edd2b65c33fd11c11a9a6ee692a3 (patch) | |
tree | 372e31aac9573fc8638c1bcb98aaca5418314af9 | |
parent | 5e1f929695e31733bfcda766effc1a92168c447b (diff) |
Option to test the callback graph, in order to detect mismatched grouping
-rw-r--r-- | src/conv/raw/vsd2raw.cpp | 183 | ||||
-rw-r--r-- | src/conv/raw/vss2raw.cpp | 183 |
2 files changed, 314 insertions, 52 deletions
diff --git a/src/conv/raw/vsd2raw.cpp b/src/conv/raw/vsd2raw.cpp index e79e343..dbc2ccc 100644 --- a/src/conv/raw/vsd2raw.cpp +++ b/src/conv/raw/vsd2raw.cpp @@ -29,16 +29,55 @@ */ #include <stdio.h> +#include <stdarg.h> #include <string.h> +#include <stack> #include "libvisio.h" #include <libwpd-stream/libwpd-stream.h> #include <libwpd/libwpd.h> +enum PainterCallback +{ + PC_START_GRAPHICS = 0, + PC_START_LAYER, + PC_START_EMBEDDED_GRAPHICS, + PC_START_TEXT_OBJECT, + PC_START_TEXT_LINE, + PC_START_TEXT_SPAN +}; + +#ifdef _U +#undef _U +#endif + +#define _U(M, L) \ + if (!m_printCallgraphScore) \ + __iuprintf M; \ + else \ + m_callStack.push(L); + +#ifdef _D +#undef _D +#endif + +#define _D(M, L) \ + if (!m_printCallgraphScore) \ + __idprintf M; \ + else \ + { \ + PainterCallback lc = m_callStack.top(); \ + if (lc != L) \ + m_callbackMisses++; \ + m_callStack.pop(); \ + } + class RawPainter : public libwpg::WPGPaintInterface { public: - RawPainter(); + RawPainter(bool printCallgraphScore); + + ~RawPainter(); void startGraphics(const ::WPXPropertyList &propList); void endGraphics(); @@ -62,6 +101,25 @@ public: void startTextSpan(const ::WPXPropertyList &propList); void endTextSpan(); void insertText(const ::WPXString &str); + +private: + int m_indent; + int m_callbackMisses; + bool m_printCallgraphScore; + std::stack<PainterCallback> m_callStack; + + void __indentUp() + { + m_indent++; + } + void __indentDown() + { + if (m_indent > 0) m_indent--; + } + + void __iprintf(const char *format, ...); + void __iuprintf(const char *format, ...); + void __idprintf(const char *format, ...); }; WPXString getPropString(const WPXPropertyList &propList) @@ -111,108 +169,177 @@ WPXString getPropString(const WPXPropertyListVector &itemList) return propString; } -RawPainter::RawPainter(): libwpg::WPGPaintInterface() +RawPainter::RawPainter(bool printCallgraphScore): + libwpg::WPGPaintInterface(), + m_indent(0), + m_callbackMisses(0), + m_printCallgraphScore(printCallgraphScore), + m_callStack() +{ +} + +RawPainter::~RawPainter() +{ + if (m_printCallgraphScore) + printf("%d\n", (int)(m_callStack.size() + m_callbackMisses)); +} + +void RawPainter::__iprintf(const char *format, ...) { + if (m_printCallgraphScore) return; + + va_list args; + va_start(args, format); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + va_end(args); +} + +void RawPainter::__iuprintf(const char *format, ...) +{ + va_list args; + va_start(args, format); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + __indentUp(); + va_end(args); +} + +void RawPainter::__idprintf(const char *format, ...) +{ + va_list args; + va_start(args, format); + __indentDown(); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + va_end(args); } void RawPainter::startGraphics(const ::WPXPropertyList &propList) { - printf("RawPainter::startGraphics(%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startGraphics(%s)\n", getPropString(propList).cstr()), PC_START_GRAPHICS); } void RawPainter::endGraphics() { - printf("RawPainter::endGraphics\n"); + _D(("RawPainter::endGraphics\n"), PC_START_GRAPHICS); } void RawPainter::startLayer(const ::WPXPropertyList &propList) { - printf("RawPainter::startLayer (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startLayer (%s)\n", getPropString(propList).cstr()), PC_START_LAYER); } void RawPainter::endLayer() { - printf("RawPainter::endLayer\n"); + _D(("RawPainter::endLayer\n"), PC_START_LAYER); } void RawPainter::startEmbeddedGraphics(const ::WPXPropertyList &propList) { - printf("RawPainter::startEmbeddedGraphics (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startEmbeddedGraphics (%s)\n", getPropString(propList).cstr()), PC_START_EMBEDDED_GRAPHICS); } void RawPainter::endEmbeddedGraphics() { - printf("RawPainter::endEmbeddedGraphics \n"); + _D(("RawPainter::endEmbeddedGraphics \n"), PC_START_EMBEDDED_GRAPHICS); } void RawPainter::setStyle(const ::WPXPropertyList &propList, const ::WPXPropertyListVector &gradient) { - printf("RawPainter::setStyle(%s, gradient: (%s))\n", getPropString(propList).cstr(), getPropString(gradient).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::setStyle(%s, gradient: (%s))\n", getPropString(propList).cstr(), getPropString(gradient).cstr()); } void RawPainter::drawRectangle(const ::WPXPropertyList &propList) { - printf("RawPainter::drawRectangle (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawRectangle (%s)\n", getPropString(propList).cstr()); } void RawPainter::drawEllipse(const ::WPXPropertyList &propList) { - printf("RawPainter::drawEllipse (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawEllipse (%s)\n", getPropString(propList).cstr()); } void RawPainter::drawPolyline(const ::WPXPropertyListVector &vertices) { - printf("RawPainter::drawPolyline (%s)\n", getPropString(vertices).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPolyline (%s)\n", getPropString(vertices).cstr()); } void RawPainter::drawPolygon(const ::WPXPropertyListVector &vertices) { - printf("RawPainter::drawPolygon (%s)\n", getPropString(vertices).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPolygon (%s)\n", getPropString(vertices).cstr()); } void RawPainter::drawPath(const ::WPXPropertyListVector &path) { - printf("RawPainter::drawPath (%s)\n", getPropString(path).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPath (%s)\n", getPropString(path).cstr()); } void RawPainter::drawGraphicObject(const ::WPXPropertyList &propList, const ::WPXBinaryData & /*binaryData*/) { - printf("RawPainter::drawGraphicObject (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawGraphicObject (%s)\n", getPropString(propList).cstr()); } void RawPainter::startTextObject(const ::WPXPropertyList &propList, const ::WPXPropertyListVector &path) { - printf("RawPainter::startTextObject (%s, path: (%s))\n", getPropString(propList).cstr(), getPropString(path).cstr()); + _U(("RawPainter::startTextObject (%s, path: (%s))\n", getPropString(propList).cstr(), getPropString(path).cstr()), PC_START_TEXT_OBJECT); } void RawPainter::endTextObject() { - printf("RawPainter::endTextObject\n"); + _D(("RawPainter::endTextObject\n"), PC_START_TEXT_OBJECT); } void RawPainter::startTextLine(const ::WPXPropertyList &propList) { - printf("RawPainter::startTextLine (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startTextLine (%s)\n", getPropString(propList).cstr()), PC_START_TEXT_LINE); } void RawPainter::endTextLine() { - printf("RawPainter::endTextLine\n"); + _D(("RawPainter::endTextLine\n"), PC_START_TEXT_LINE); } void RawPainter::startTextSpan(const ::WPXPropertyList &propList) { - printf("RawPainter::startTextSpan (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startTextSpan (%s)\n", getPropString(propList).cstr()), PC_START_TEXT_SPAN); } void RawPainter::endTextSpan() { - printf("RawPainter::endTextSpan\n"); + _D(("RawPainter::endTextSpan\n"), PC_START_TEXT_SPAN); } void RawPainter::insertText(const ::WPXString &str) { - printf("RawPainter::insertText (%s)\n", str.cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::insertText (%s)\n", str.cstr()); } @@ -224,6 +351,7 @@ int printUsage() printf("Usage: vsd2raw [OPTION] <Visio Document File>\n"); printf("\n"); printf("Options:\n"); + printf("--callgraph Display the call graph nesting level\n"); printf("--help Shows this help message\n"); return -1; } @@ -232,14 +360,17 @@ int printUsage() int main(int argc, char *argv[]) { + bool printIndentLevel = false; + char *file = 0; + if (argc < 2) return printUsage(); - char *file = 0; - for (int i = 1; i < argc; i++) { - if (!file && strncmp(argv[i], "--", 2)) + if (!strcmp(argv[i], "--callgraph")) + printIndentLevel = true; + else if (!file && strncmp(argv[i], "--", 2)) file = argv[i]; else return printUsage(); @@ -256,7 +387,7 @@ int main(int argc, char *argv[]) return 1; } - RawPainter painter; + RawPainter painter(printIndentLevel); if (!libvisio::VisioDocument::parse(&input, &painter)) { fprintf(stderr, "ERROR: Parsing of document failed!\n"); diff --git a/src/conv/raw/vss2raw.cpp b/src/conv/raw/vss2raw.cpp index 34f3a5a..58a7597 100644 --- a/src/conv/raw/vss2raw.cpp +++ b/src/conv/raw/vss2raw.cpp @@ -29,16 +29,55 @@ */ #include <stdio.h> +#include <stdarg.h> #include <string.h> +#include <stack> #include "libvisio.h" #include <libwpd-stream/libwpd-stream.h> #include <libwpd/libwpd.h> +enum PainterCallback +{ + PC_START_GRAPHICS = 0, + PC_START_LAYER, + PC_START_EMBEDDED_GRAPHICS, + PC_START_TEXT_OBJECT, + PC_START_TEXT_LINE, + PC_START_TEXT_SPAN +}; + +#ifdef _U +#undef _U +#endif + +#define _U(M, L) \ + if (!m_printCallgraphScore) \ + __iuprintf M; \ + else \ + m_callStack.push(L); + +#ifdef _D +#undef _D +#endif + +#define _D(M, L) \ + if (!m_printCallgraphScore) \ + __idprintf M; \ + else \ + { \ + PainterCallback lc = m_callStack.top(); \ + if (lc != L) \ + m_callbackMisses++; \ + m_callStack.pop(); \ + } + class RawPainter : public libwpg::WPGPaintInterface { public: - RawPainter(); + RawPainter(bool printCallgraphScore); + + ~RawPainter(); void startGraphics(const ::WPXPropertyList &propList); void endGraphics(); @@ -62,6 +101,25 @@ public: void startTextSpan(const ::WPXPropertyList &propList); void endTextSpan(); void insertText(const ::WPXString &str); + +private: + int m_indent; + int m_callbackMisses; + bool m_printCallgraphScore; + std::stack<PainterCallback> m_callStack; + + void __indentUp() + { + m_indent++; + } + void __indentDown() + { + if (m_indent > 0) m_indent--; + } + + void __iprintf(const char *format, ...); + void __iuprintf(const char *format, ...); + void __idprintf(const char *format, ...); }; WPXString getPropString(const WPXPropertyList &propList) @@ -111,108 +169,177 @@ WPXString getPropString(const WPXPropertyListVector &itemList) return propString; } -RawPainter::RawPainter(): libwpg::WPGPaintInterface() +RawPainter::RawPainter(bool printCallgraphScore): + libwpg::WPGPaintInterface(), + m_indent(0), + m_callbackMisses(0), + m_printCallgraphScore(printCallgraphScore), + m_callStack() +{ +} + +RawPainter::~RawPainter() +{ + if (m_printCallgraphScore) + printf("%d\n", (int)(m_callStack.size() + m_callbackMisses)); +} + +void RawPainter::__iprintf(const char *format, ...) { + if (m_printCallgraphScore) return; + + va_list args; + va_start(args, format); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + va_end(args); +} + +void RawPainter::__iuprintf(const char *format, ...) +{ + va_list args; + va_start(args, format); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + __indentUp(); + va_end(args); +} + +void RawPainter::__idprintf(const char *format, ...) +{ + va_list args; + va_start(args, format); + __indentDown(); + for (int i=0; i<m_indent; i++) + printf(" "); + vprintf(format, args); + va_end(args); } void RawPainter::startGraphics(const ::WPXPropertyList &propList) { - printf("RawPainter::startGraphics(%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startGraphics(%s)\n", getPropString(propList).cstr()), PC_START_GRAPHICS); } void RawPainter::endGraphics() { - printf("RawPainter::endGraphics\n"); + _D(("RawPainter::endGraphics\n"), PC_START_GRAPHICS); } void RawPainter::startLayer(const ::WPXPropertyList &propList) { - printf("RawPainter::startLayer (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startLayer (%s)\n", getPropString(propList).cstr()), PC_START_LAYER); } void RawPainter::endLayer() { - printf("RawPainter::endLayer\n"); + _D(("RawPainter::endLayer\n"), PC_START_LAYER); } void RawPainter::startEmbeddedGraphics(const ::WPXPropertyList &propList) { - printf("RawPainter::startEmbeddedGraphics (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startEmbeddedGraphics (%s)\n", getPropString(propList).cstr()), PC_START_EMBEDDED_GRAPHICS); } void RawPainter::endEmbeddedGraphics() { - printf("RawPainter::endEmbeddedGraphics \n"); + _D(("RawPainter::endEmbeddedGraphics \n"), PC_START_EMBEDDED_GRAPHICS); } void RawPainter::setStyle(const ::WPXPropertyList &propList, const ::WPXPropertyListVector &gradient) { - printf("RawPainter::setStyle(%s, gradient: (%s))\n", getPropString(propList).cstr(), getPropString(gradient).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::setStyle(%s, gradient: (%s))\n", getPropString(propList).cstr(), getPropString(gradient).cstr()); } void RawPainter::drawRectangle(const ::WPXPropertyList &propList) { - printf("RawPainter::drawRectangle (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawRectangle (%s)\n", getPropString(propList).cstr()); } void RawPainter::drawEllipse(const ::WPXPropertyList &propList) { - printf("RawPainter::drawEllipse (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawEllipse (%s)\n", getPropString(propList).cstr()); } void RawPainter::drawPolyline(const ::WPXPropertyListVector &vertices) { - printf("RawPainter::drawPolyline (%s)\n", getPropString(vertices).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPolyline (%s)\n", getPropString(vertices).cstr()); } void RawPainter::drawPolygon(const ::WPXPropertyListVector &vertices) { - printf("RawPainter::drawPolygon (%s)\n", getPropString(vertices).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPolygon (%s)\n", getPropString(vertices).cstr()); } void RawPainter::drawPath(const ::WPXPropertyListVector &path) { - printf("RawPainter::drawPath (%s)\n", getPropString(path).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawPath (%s)\n", getPropString(path).cstr()); } void RawPainter::drawGraphicObject(const ::WPXPropertyList &propList, const ::WPXBinaryData & /*binaryData*/) { - printf("RawPainter::drawGraphicObject (%s)\n", getPropString(propList).cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::drawGraphicObject (%s)\n", getPropString(propList).cstr()); } void RawPainter::startTextObject(const ::WPXPropertyList &propList, const ::WPXPropertyListVector &path) { - printf("RawPainter::startTextObject (%s, path: (%s))\n", getPropString(propList).cstr(), getPropString(path).cstr()); + _U(("RawPainter::startTextObject (%s, path: (%s))\n", getPropString(propList).cstr(), getPropString(path).cstr()), PC_START_TEXT_OBJECT); } void RawPainter::endTextObject() { - printf("RawPainter::endTextObject\n"); + _D(("RawPainter::endTextObject\n"), PC_START_TEXT_OBJECT); } void RawPainter::startTextLine(const ::WPXPropertyList &propList) { - printf("RawPainter::startTextLine (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startTextLine (%s)\n", getPropString(propList).cstr()), PC_START_TEXT_LINE); } void RawPainter::endTextLine() { - printf("RawPainter::endTextLine\n"); + _D(("RawPainter::endTextLine\n"), PC_START_TEXT_LINE); } void RawPainter::startTextSpan(const ::WPXPropertyList &propList) { - printf("RawPainter::startTextSpan (%s)\n", getPropString(propList).cstr()); + _U(("RawPainter::startTextSpan (%s)\n", getPropString(propList).cstr()), PC_START_TEXT_SPAN); } void RawPainter::endTextSpan() { - printf("RawPainter::endTextSpan\n"); + _D(("RawPainter::endTextSpan\n"), PC_START_TEXT_SPAN); } void RawPainter::insertText(const ::WPXString &str) { - printf("RawPainter::insertText (%s)\n", str.cstr()); + if (m_printCallgraphScore) + return; + + __iprintf("RawPainter::insertText (%s)\n", str.cstr()); } @@ -224,6 +351,7 @@ int printUsage() printf("Usage: vsd2raw [OPTION] <Visio Stencils File>\n"); printf("\n"); printf("Options:\n"); + printf("--callgraph Display the call graph nesting level\n"); printf("--help Shows this help message\n"); return -1; } @@ -232,14 +360,17 @@ int printUsage() int main(int argc, char *argv[]) { + bool printIndentLevel = false; + char *file = 0; + if (argc < 2) return printUsage(); - char *file = 0; - for (int i = 1; i < argc; i++) { - if (!file && strncmp(argv[i], "--", 2)) + if (!strcmp(argv[i], "--callgraph")) + printIndentLevel = true; + else if (!file && strncmp(argv[i], "--", 2)) file = argv[i]; else return printUsage(); @@ -256,7 +387,7 @@ int main(int argc, char *argv[]) return 1; } - RawPainter painter; + RawPainter painter(printIndentLevel); if (!libvisio::VisioDocument::parseStencils(&input, &painter)) { fprintf(stderr, "ERROR: Parsing of document failed!\n"); |