From: hama Date: Sat, 9 Aug 2014 00:30:58 +0000 (+0200) Subject: dayly work X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=08cd598476684c08b0d647a91b59581ef345bae9;p=reqt dayly work --- diff --git a/rplcore/rplcore.hpp b/rplcore/rplcore.hpp index f58b0c6..a09be09 100644 --- a/rplcore/rplcore.hpp +++ b/rplcore/rplcore.hpp @@ -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 index 0000000..4646c0e --- /dev/null +++ b/rplcore/rplwriter.cpp @@ -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 index 0000000..9abdb11 --- /dev/null +++ b/rplcore/rplwriter.hpp @@ -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 diff --git a/rplexpr/rplasclasses.cpp b/rplexpr/rplasclasses.cpp index 04201ab..0e73ddf 100644 --- a/rplexpr/rplasclasses.cpp +++ b/rplexpr/rplasclasses.cpp @@ -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. diff --git a/rplstatic/rplstatic.pro b/rplstatic/rplstatic.pro index 7368212..6936ccc 100644 --- a/rplstatic/rplstatic.pro +++ b/rplstatic/rplstatic.pro @@ -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 { diff --git a/unittests/main.cpp b/unittests/main.cpp index 74a7451..4026016 100644 --- a/unittests/main.cpp +++ b/unittests/main.cpp @@ -11,6 +11,9 @@ #include 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 index 0000000..8a2700c --- /dev/null +++ b/unittests/rplwriter_test.cpp @@ -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 RplString. + */ +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(); +} + + + diff --git a/unittests/unittests.pro b/unittests/unittests.pro index a28f5c2..18dec12 100644 --- a/unittests/unittests.pro +++ b/unittests/unittests.pro @@ -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