]> gitweb.hamatoma.de Git - reqt/commitdiff
dayly work
authorhama <hama@siduction.net>
Sat, 9 Aug 2014 00:30:58 +0000 (02:30 +0200)
committerhama <hama@siduction.net>
Sat, 9 Aug 2014 00:30:58 +0000 (02:30 +0200)
rplcore/rplcore.hpp
rplcore/rplwriter.cpp [new file with mode: 0644]
rplcore/rplwriter.hpp [new file with mode: 0644]
rplexpr/rplasclasses.cpp
rplstatic/rplstatic.pro
unittests/main.cpp
unittests/rplwriter_test.cpp [new file with mode: 0644]
unittests/unittests.pro

index f58b0c6ebda11de3428290d69972f39048852de5..a09be09577407405a3b10a7ec398c7583db83c1f 100644 (file)
@@ -35,6 +35,7 @@ typedef unsigned char uint8_t;
 
 #include "rplmodules.hpp"
 #include "rplcore/rplbytestorage.hpp"
+#include "rplcore/rplwriter.hpp"
 #include "rplcore/rpllogger.hpp"
 #include "rplcore/rplexception.hpp"
 #include "rplcore/rplcontainer.hpp"
diff --git a/rplcore/rplwriter.cpp b/rplcore/rplwriter.cpp
new file mode 100644 (file)
index 0000000..4646c0e
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#include "rplcore/rplcore.hpp"
+
+const char* RplWriter::m_tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\ŧ\t\ŧ\ŧ\ŧ\t\t\t";
+
+/** @class RplWriter rplwriter.hpp "rplcore/rplwriter.hpp"
+ *
+ * @brief Implements an abstract base class for producing text lines.
+ *
+ */
+
+/**
+ * @brief Destructor.
+ *
+ * Closes the output medium.
+ * Ensures that the destructors of the derived classes are virtual.
+ */
+RplWriter::~RplWriter()
+{
+    close();
+}
+
+/**
+ * @brief Closes the output medium.
+ *
+ * This method does nothing, but overriding methods should free the resources.
+ *
+ * @note The method must be designed so that it can be called multiple times.
+ */
+void RplWriter::close()
+{
+}
+/**
+ * @brief Formats a line and write it to the output medium.
+ * @param format
+ */
+void RplWriter::formatLine(const char* format, ...)
+{
+    char buffer[64000];
+    va_list ap;
+    va_start(ap, format);
+    vsnprintf(buffer, sizeof buffer, format, ap);
+    va_end(ap);
+    writeLine(buffer);
+}
+
+/**
+ * @brief Writes a line with indention to the output medium.
+ *
+ * @param indent    indention level. Indention is limited to 20
+ * @param line      line to write (without '\n')
+ */
+void RplWriter::writeIndented(int indent, const char* line)
+{
+    formatLine("%.*s%s", indent, m_tabs, line);
+}
+
+/** @class RplWriter rplwriter.hpp "rplcore/rplwriter.hpp"
+ *
+ * @brief Implements a class which writes lines into a file.
+ */
+
+/**
+ * @brief Constructor.
+ *
+ * @param filename  the file's name
+ * @param mode      write mode, "w" for write or "a" for append
+ */
+RplFileWriter::RplFileWriter(const char* filename, const char* mode,
+                             const char* eoln) :
+    m_fp(fopen(filename, mode)),
+    m_name(filename),
+    m_eoln(eoln)
+{
+}
+
+/**
+ * @brief Writes a line to the file.
+ * @param line   the line to write
+ */
+void RplFileWriter::writeLine(const char* line)
+{
+    if (m_fp != NULL){
+        fputs(line, m_fp);
+        fputs(m_eoln, m_fp);
+    }
+}
+
+/**
+ * @brief Closes the output file.
+ */
+void RplFileWriter::close()
+{
+    if (m_fp != NULL){
+        fclose(m_fp);
+        m_fp = NULL;
+    }
+}
diff --git a/rplcore/rplwriter.hpp b/rplcore/rplwriter.hpp
new file mode 100644 (file)
index 0000000..9abdb11
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#ifndef RPLWRITER_HPP
+#define RPLWRITER_HPP
+
+class RplWriter
+{
+public:
+    virtual ~RplWriter();
+public:
+    /**
+     * @brief Writes a text line to the output medium.
+     *
+     * @param message   the text line
+     */
+    virtual void writeLine(const char* message) = 0;
+    virtual void close();
+public:
+    void formatLine(const char* format, ...);
+    void writeIndented(int indent, const char* line);
+protected:
+    static const char* m_tabs;
+};
+
+class RplFileWriter : public RplWriter
+{
+public:
+    RplFileWriter(const char* filename, const char* mode = "w",
+                  const char* eoln =  "\n");
+public:
+    virtual void writeLine(const char* line);
+    virtual void close();
+protected:
+    FILE* m_fp;
+    QByteArray m_name;
+    QByteArray m_eoln;
+};
+
+#endif // RPLWRITER_HPP
index 04201ab422c2260ef14a7b9ad9a53010dddd4645..0e73ddfc2cd77dc66a3ef22d1e4544ddac02bb92 100644 (file)
@@ -19,9 +19,6 @@ RplASBoolean* RplASBoolean::m_instance = NULL;
 RplASVoid* RplASVoid::m_instance = NULL;
 RplASFormula* RplASFormula::m_instance = NULL;
 
-///@ToDo static
-///RplSymbolSpace* RplSymbolSpace::m_global = NULL;
-
 /** @class RplSymbolSpace rplastree.hpp "rplexpr/rplastree.hpp"
  *
  * @brief Implements a symbol space for the parser.
index 736821257d8ee8bf10d1619ab2aa7b24fc6f5245..6936cccc8667fef5353ddc8a809188447f319445 100644 (file)
@@ -38,7 +38,8 @@ SOURCES += \
     ../rplexpr/rplmfparser.cpp \
     ../rplexpr/rplvm.cpp \
     ../rplexpr/rplparser.cpp \
-    ../rplcore/rplbytestorage.cpp
+    ../rplcore/rplbytestorage.cpp \
+    ../rplcore/rplwriter.cpp
 
 HEADERS += ../rplmodules.hpp \
     ../rplcore/rplconfig.hpp \
@@ -68,7 +69,8 @@ HEADERS += ../rplmodules.hpp \
     ../rplexpr/rplmfparser.hpp \
     ../rplexpr/rplvm.hpp \
     ../rplexpr/rplparser.hpp \
-    ../rplcore/rplbytestorage.hpp
+    ../rplcore/rplbytestorage.hpp \
+    ../rplcore/rplwriter.hpp
 
 unix:!symbian {
     maemo5 {
index 74a74511bb764708a67b26b5e14bf749fd34f9a1..402601652c12ef5effbc9017a47c9ae132572889 100644 (file)
@@ -11,6 +11,9 @@
 #include <QCoreApplication>
 
 void testCore(){
+    extern void testRplWriter();
+    testRplWriter();
+
     extern void testRplByteStorage();
     testRplByteStorage();
     extern void testRplQString();
@@ -39,7 +42,7 @@ void testExpr(){
 }
 
 void testStandard(){
-    testExpr();
+    //testExpr();
     testCore();
     extern void testRplMatrix();
     testRplMatrix();
diff --git a/unittests/rplwriter_test.cpp b/unittests/rplwriter_test.cpp
new file mode 100644 (file)
index 0000000..8a2700c
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+#include "rplcore/rplcore.hpp"
+#include "rplcore/rpltest.hpp"
+/**
+ * @brief Unit test for <code>RplString</code>.
+ */
+class TestRplWriter : public RplTest {
+public:
+    TestRplWriter() : RplTest("RplWriter") {}
+
+private:
+    void testFileWriter(){
+        QByteArray fn = getTempFile("rplwriter.txt");
+        RplFileWriter writer(fn);
+        writer.writeLine("abc");
+        writer.formatLine("%04d", 42);
+        writer.writeIndented(3, "123");
+        writer.close();
+        QByteArray current = RplString::read(fn, false);
+        checkE("abc\n0042\n\t\t\t123\n", current);
+    }
+
+public:
+    virtual void doIt(void) {
+        testFileWriter();
+    }
+};
+void testRplWriter() {
+    TestRplWriter test;
+    test.run();
+}
+
+
+
index a28f5c205ee6d1609f1647ef3de8a289ff5aa8c5..18dec12f282a4904b761b246cdcb8e9a0788ebd3 100644 (file)
@@ -40,5 +40,7 @@ SOURCES += main.cpp \
     ../rplexpr/rplvm_test.cpp \
     rplbytestorage_test.cpp \
     ../rplcore/rplbytestorage.cpp \
-    ../rplexpr/rplvm.cpp
+    ../rplexpr/rplvm.cpp \
+    rplwriter_test.cpp \
+    ../rplcore/rplwriter.cpp