C++: Improved turtlePrint output (choose out stream)
authorjani <jani@asema.com>
Mon, 9 Dec 2019 23:55:43 +0000 (01:55 +0200)
committerjani <jani@asema.com>
Mon, 9 Dec 2019 23:55:43 +0000 (01:55 +0200)
Common/C++/SmartAPI/smartapi/common/ObjectPrinter.cpp
Common/C++/SmartAPI/smartapi/common/ObjectPrinter.h
Common/C++/SmartAPI/smartapi/model/Obj.cpp
Common/C++/SmartAPI/smartapi/model/Obj.h

index f4a6370c0468bb3cbf9ed552dcc0ee5ae99af13f..73e85cf4ea3eeca2f39c2533b87e38339ad86602 100644 (file)
@@ -3,8 +3,9 @@
 #include "smartapi/common/Tools.h"
 #include "smartapi/common/PROPERTY.h"
 
-ObjectPrinter::ObjectPrinter() : mExplain(false), mDestroyObjectAfterPrinting(false), mObj(NULL)
+ObjectPrinter::ObjectPrinter(FILE* _stream) : mExplain(false), mDestroyObjectAfterPrinting(false), mObj(NULL)
 {
+       mStream = _stream;
        mCache = new OntologyCache(OntologyCache::REALTIME_VALIDATION);
 }
 
@@ -17,8 +18,8 @@ ObjectPrinter::~ObjectPrinter()
 void ObjectPrinter::printAsTurtle(Obj* o)
 {
        QString str = Tools::toString(o, SERIALIZATION__TURTLE);
-       fprintf(stdout, "%s\n", str.toLatin1().data());
-       fflush(stdout);
+       fprintf(mStream, "%s\n", str.toLatin1().data());
+       fflush(mStream);
        delete this;
 }
 
@@ -180,8 +181,8 @@ void ObjectPrinter::printLine(int indentLevel, QString str)
 {
        int indent = 4 * indentLevel; // indent size is currently hardcoded to 4 chars
        QString spaces(indent, ' ');
-       fprintf(stdout, "%s%s\n", spaces.toLocal8Bit().constData(), str.toLocal8Bit().constData());
-       fflush(stdout);
+       fprintf(mStream, "%s%s\n", spaces.toLocal8Bit().constData(), str.toLocal8Bit().constData());
+       fflush(mStream);
 }
 
 QString ObjectPrinter::explainLabel(QString property, QString uri)
index 6d3941b668bf456818b9ae8c17cade4b7c4736e5..18e0e53f80a63f1b40315f4ad1ff54ad64c7e995 100644 (file)
@@ -2,6 +2,7 @@
 #define __OBJECTPRINTER_H__
 
 #include <QObject>
+#include <stdio.h>
 #include "smartapi/model/Obj.h"
 #include "smartapi/common/OntologyCache.h"
 
@@ -10,7 +11,7 @@ class ObjectPrinter : public QObject
        Q_OBJECT
 
 public:
-       ObjectPrinter();
+       ObjectPrinter(FILE* _stream = stdout);
        ~ObjectPrinter();
 
        void printAsTurtle(Obj* o);
@@ -33,6 +34,7 @@ private:
        bool mExplain;
        bool mDestroyObjectAfterPrinting;
        OntologyCache* mCache;
+       FILE* mStream;
 };
 
 #endif // __OBJECTPRINTER_H__
index 0a03d55764f008c04f364bc0bce765821da15b1a..cfea84bcb4c576b4cd655dce6c3ab0fda09295f2 100755 (executable)
@@ -1333,6 +1333,12 @@ void Obj::turtlePrint()
        op->printAsTurtle(this);
 }
 
+void Obj::turtlePrint(FILE* out)
+{
+       ObjectPrinter* op = new ObjectPrinter(out);  // note, the ObjectPrinter deletes itself automatically once printing is done
+       op->printAsTurtle(this);
+}
+
 void Obj::printOut(bool destroyAfterPrinting)
 {
        ObjectPrinter* op = new ObjectPrinter();  // note, the ObjectPrinter deletes itself automatically once printing is done
index c0c3764aabb5f79a17281a466866e09004613744..900f1b798e5d1fb9e56a582b58c17e47e6feaf32 100644 (file)
@@ -251,6 +251,7 @@ public:
 
        QString asTurtle();
        void turtlePrint();
+       void turtlePrint(FILE* out);
        void printOut(bool destroyAfterPrinting = false);
        void explain(bool destroyAfterPrinting = false);