#include "rplmodules.hpp"
#include "rplcore/rplbytestorage.hpp"
+#include "rplcore/rplwriter.hpp"
#include "rplcore/rpllogger.hpp"
#include "rplcore/rplexception.hpp"
#include "rplcore/rplcontainer.hpp"
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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
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.
../rplexpr/rplmfparser.cpp \
../rplexpr/rplvm.cpp \
../rplexpr/rplparser.cpp \
- ../rplcore/rplbytestorage.cpp
+ ../rplcore/rplbytestorage.cpp \
+ ../rplcore/rplwriter.cpp
HEADERS += ../rplmodules.hpp \
../rplcore/rplconfig.hpp \
../rplexpr/rplmfparser.hpp \
../rplexpr/rplvm.hpp \
../rplexpr/rplparser.hpp \
- ../rplcore/rplbytestorage.hpp
+ ../rplcore/rplbytestorage.hpp \
+ ../rplcore/rplwriter.hpp
unix:!symbian {
maemo5 {
#include <QCoreApplication>
void testCore(){
+ extern void testRplWriter();
+ testRplWriter();
+
extern void testRplByteStorage();
testRplByteStorage();
extern void testRplQString();
}
void testStandard(){
- testExpr();
+ //testExpr();
testCore();
extern void testRplMatrix();
testRplMatrix();
--- /dev/null
+/*
+ * 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();
+}
+
+
+
../rplexpr/rplvm_test.cpp \
rplbytestorage_test.cpp \
../rplcore/rplbytestorage.cpp \
- ../rplexpr/rplvm.cpp
+ ../rplexpr/rplvm.cpp \
+ rplwriter_test.cpp \
+ ../rplcore/rplwriter.cpp