From: hama Date: Sat, 9 Aug 2014 10:00:15 +0000 (+0200) Subject: dayly work X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=fc213e9756a43933584469184737cbef149cc050;p=reqt dayly work --- diff --git a/rplcore/rplwriter.cpp b/rplcore/rplwriter.cpp index 4646c0e..eca58a6 100644 --- a/rplcore/rplwriter.cpp +++ b/rplcore/rplwriter.cpp @@ -9,7 +9,8 @@ #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"; +const char* RplWriter::m_tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; +int RplWriter::m_maxIndention = strlen(RplWriter::m_tabs); /** @class RplWriter rplwriter.hpp "rplcore/rplwriter.hpp" * @@ -38,9 +39,38 @@ RplWriter::~RplWriter() void RplWriter::close() { } +/** + * @brief Puts a given count of '\t' to the output medium. + * + * @param indent indention level, number of '\t' + */ +void RplWriter::indent(int indent) +{ + if (indent > m_maxIndention) + indent = m_maxIndention; + format("%.*s", indent, m_tabs); +} + +/** + * @brief Formats a string and write it to the output medium. + * + * @param format format string with placeholders like sprintf() + * @param ... variable arguments, values for the placeholders + */ +void RplWriter::format(const char* format, ...) +{ + char buffer[64000]; + va_list ap; + va_start(ap, format); + vsnprintf(buffer, sizeof buffer, format, ap); + va_end(ap); + write(buffer); +} /** * @brief Formats a line and write it to the output medium. - * @param format + * + * @param format format string with placeholders like sprintf() + * @param ... variable arguments, values for the placeholders */ void RplWriter::formatLine(const char* format, ...) { @@ -56,11 +86,31 @@ void RplWriter::formatLine(const char* format, ...) * @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') + * @param format format string with placeholders like sprintf() + * @param ... variable arguments, values for the placeholders */ void RplWriter::writeIndented(int indent, const char* line) { - formatLine("%.*s%s", indent, m_tabs, line); + RplWriter::indent(indent); + writeLine(line); +} + +/** + * @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::formatIndented(int indent, const char* format, ...) +{ + RplWriter::indent(indent); + + char buffer[64000]; + va_list ap; + va_start(ap, format); + vsnprintf(buffer, sizeof buffer, format, ap); + va_end(ap); + writeLine(buffer); } /** @class RplWriter rplwriter.hpp "rplcore/rplwriter.hpp" @@ -82,14 +132,26 @@ RplFileWriter::RplFileWriter(const char* filename, const char* mode, { } +/** + * @brief Writes a string to the file. + * @param message the string to write + */ +void RplFileWriter::write(const char* message) +{ + if (m_fp != NULL){ + fputs(message, m_fp); + } +} + /** * @brief Writes a line to the file. - * @param line the line to write + * @param line the line to write. If NULL an empty line will be written */ void RplFileWriter::writeLine(const char* line) { if (m_fp != NULL){ - fputs(line, m_fp); + if (line != NULL) + fputs(line, m_fp); fputs(m_eoln, m_fp); } } diff --git a/rplcore/rplwriter.hpp b/rplcore/rplwriter.hpp index 9abdb11..d9d5772 100644 --- a/rplcore/rplwriter.hpp +++ b/rplcore/rplwriter.hpp @@ -15,18 +15,28 @@ class RplWriter public: virtual ~RplWriter(); public: + /** + * @brief Writes a text to the output medium. + * + * @param message the message + */ + virtual void write(const char* message) = 0; /** * @brief Writes a text line to the output medium. * - * @param message the text line + * @param line the text line. If NULL an empty line will be written */ - virtual void writeLine(const char* message) = 0; + virtual void writeLine(const char* line = NULL) = 0; virtual void close(); public: + void indent(int indent); + void format(const char* format, ...); void formatLine(const char* format, ...); void writeIndented(int indent, const char* line); + void formatIndented(int indent, const char* format, ...); protected: static const char* m_tabs; + static int m_maxIndention; }; class RplFileWriter : public RplWriter @@ -35,7 +45,8 @@ public: RplFileWriter(const char* filename, const char* mode = "w", const char* eoln = "\n"); public: - virtual void writeLine(const char* line); + virtual void write(const char* line); + virtual void writeLine(const char* line = NULL); virtual void close(); protected: FILE* m_fp; diff --git a/rplexpr/rplasclasses.cpp b/rplexpr/rplasclasses.cpp index 0e73ddf..39ea4e3 100644 --- a/rplexpr/rplasclasses.cpp +++ b/rplexpr/rplasclasses.cpp @@ -132,23 +132,20 @@ RplASMethod* RplSymbolSpace::findMethod(const QString& name) const /** * @brief Writes the content of the instance into a file. * - * @param fp the target file + * @param writer writes to output * @param indent nesting level: so many tabs will be used as prefix * @param header NULL or the headline */ -void RplSymbolSpace::dump(FILE* fp, int indent, const char* header) +void RplSymbolSpace::dump(RplWriter& writer, int indent, const char* header) { - char tabs[32]; - memset(tabs, '\t', sizeof tabs); - tabs[(unsigned) indent < sizeof tabs ? indent : sizeof tabs - 1] = '\0'; if (header != NULL) - fprintf(fp, "%s%s\n", tabs, header); - fprintf(fp, "%s= %s (%s) parent: %s\n", tabs, m_name.toUtf8().constData(), + writer.writeLine(header); + writer.formatIndented(indent, "= %s (%s) parent: %s", m_name.toUtf8().constData(), spaceTypeName(m_type), m_parent == NULL ? "" : m_parent->name().toUtf8().constData()); QList sorted; if (m_classes.size() > 0){ - fprintf(fp, "%s== Classes:\n", tabs); + writer.writeIndented(indent, "== Classes:"); sorted.reserve(m_classes.size()); ClassMap::iterator it; for (it = m_classes.begin(); it != m_classes.end(); it++){ @@ -158,11 +155,11 @@ void RplSymbolSpace::dump(FILE* fp, int indent, const char* header) QList::iterator it2; for (it2 = sorted.begin(); it2 != sorted.end(); it2++){ RplASClass* clazz = m_classes[*it2]; - clazz->dump(fp, indent); + clazz->dump(writer, indent); } } if (m_methods.size() > 0){ - fprintf(fp, "%s== Methods:\n", tabs); + writer.writeIndented(indent, "== Methods:"); sorted.clear(); sorted.reserve(m_variables.size()); MethodMap::iterator it3; @@ -174,14 +171,14 @@ void RplSymbolSpace::dump(FILE* fp, int indent, const char* header) for (it4 = sorted.begin(); it4 != sorted.end(); it4++){ RplASMethod* method = m_methods[*it4]; do { - method->dump(fp, indent); + method->dump(writer, indent); method = method->sibling(); } while (method != NULL); } } if (m_variables.size() > 0){ - fprintf(fp, "%s== Variables:\n", tabs); + writer.writeIndented(indent, "== Variables:"); sorted.clear(); sorted.reserve(m_variables.size()); VariableMap::iterator it5; @@ -192,12 +189,12 @@ void RplSymbolSpace::dump(FILE* fp, int indent, const char* header) QList::iterator it6; for (it6 = sorted.begin(); it6 != sorted.end(); it6++){ RplASVarDefinition* var = m_variables[*it6]; - var->dump(fp, indent); + var->dump(writer, indent); } } if (m_body != NULL){ - fprintf(fp, "%s== Body:\n", tabs); - RplASNode1::dumpStatements(fp, indent, m_body); + writer.writeIndented(indent, "== Body:"); + RplASNode1::dumpStatements(writer, indent, m_body); } } @@ -867,22 +864,18 @@ RplVariable::RplVariable(const QString& name) : } /** - * @brief Writes the content of the instance into a file. + * @brief Writes the content of the instance into a output media. * - * @param fp the target file + * @param writer writes to output * @param indent nesting level: so many tabs will be used as prefix * @param header NULL or the headline */ -void RplVariable::dump(FILE* fp, int indent) +void RplVariable::dump(RplWriter& writer, int indent) { - char tabs[32]; - memset(tabs, '\t', sizeof tabs); - tabs[(unsigned) indent < sizeof tabs ? indent : sizeof tabs - 1] = '\0'; - QByteArray name1 = m_type == NULL ? "NoneType" : m_type->name().toUtf8(); QByteArray name2 = m_name.toUtf8(); QByteArray val = m_value.toString().toUtf8(); - fprintf(fp, "%s%s %s: value: %s\n", tabs, + writer.formatIndented(indent, "%s %s: value: %s", name1.constData(), name2.constData(), val.constData()); } diff --git a/rplexpr/rplasclasses.hpp b/rplexpr/rplasclasses.hpp index 05ffd1a..c666a8d 100644 --- a/rplexpr/rplasclasses.hpp +++ b/rplexpr/rplasclasses.hpp @@ -15,7 +15,7 @@ class RplVariable { public: RplVariable(const QString& name); public: - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); protected: QString m_name; @@ -53,7 +53,7 @@ public: RplVariable* findVariable(const QString& name) const; RplASClass* findClass(const QString& name) const; RplASMethod* findMethod(const QString& name) const; - void dump(FILE* fp, int indent, const char* header = NULL); + void dump(RplWriter& writer, int indent, const char* header = NULL); const QString& name() const; RplASItem* body() const; void setBody(RplASItem* body); diff --git a/rplexpr/rplastree.cpp b/rplexpr/rplastree.cpp index 55a1bdc..21a4789 100644 --- a/rplexpr/rplastree.cpp +++ b/rplexpr/rplastree.cpp @@ -21,11 +21,11 @@ unsigned int RplASItem::m_nextId = 1; * * The output is sorted by key. * - * @param fp target file + * @param writer writes to output * @param map map to dump * @param withEndOfLine true: '\n' will be written at the end */ -void dumpMap(FILE* fp, RplASMapOfVariants& map, bool withEndOfLine) +void dumpMap(RplWriter& writer, RplASMapOfVariants& map, bool withEndOfLine) { QList sorted; sorted.reserve(map.size()); @@ -38,11 +38,15 @@ void dumpMap(FILE* fp, RplASMapOfVariants& map, bool withEndOfLine) bool first = true; for (it2 = sorted.begin(); it2 != sorted.end(); it2++){ RplASVariant* value = map[*it2]; - fprintf(fp, "%c'%s':%s", first ? '{' : ',', (*it2).toUtf8().constData(), + writer.format("%c'%s':%s", first ? '{' : ',', (*it2).toUtf8().constData(), value->toString().toUtf8().constData()); first = false; } - fprintf(fp, "%s}%s", first ? "{" : "", withEndOfLine ? "\n" : ""); + if (first) + writer.write("{"); + writer.write("}"); + if (withEndOfLine) + writer.writeLine(); } /** @class RplASException rplastree.hpp "rplexpr/rplastree.hpp" @@ -545,14 +549,13 @@ RplASNode1* RplASConstant::calc(RplASVariant& value) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASConstant::dump(FILE* fp, int indent) +void RplASConstant::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sconst id: %d value: %s %s\n", tabs, m_id, + writer.formatIndented(indent, "const id: %d value: %s %s", m_id, m_value.toString().toUtf8().constData(), positionStr(buffer, sizeof buffer)); } @@ -614,18 +617,17 @@ RplASNode1* RplASListConstant::calc(RplASVariant& value) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASListConstant::dump(FILE* fp, int indent) +void RplASListConstant::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%slistConst id: %d %s\n", tabs, m_id, + writer.formatIndented(indent, "listConst id: %d %s", m_id, positionStr(buffer, sizeof buffer)); QString sValue = m_value.toString(8092); - fprintf(fp, "%s\t%s\n", tabs, sValue.toUtf8().constData()); + writer.writeIndented(indent + 1, sValue.toUtf8().constData()); } /** @@ -674,16 +676,16 @@ RplASNode1* RplASMapConstant::calc(RplASVariant& ) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASMapConstant::dump(FILE* fp, int indent) +void RplASMapConstant::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%smapConst id: %d %s\n%s", tabs, - m_id, positionStr(buffer, sizeof buffer), tabs); - dumpMap(fp, *map(), true); + writer.formatIndented(indent, "mapConst id: %d %s", + m_id, positionStr(buffer, sizeof buffer)); + writer.indent(indent); + dumpMap(writer, *map(), true); } @@ -767,14 +769,13 @@ RplASNode1* RplASNamedValue::calc(RplASVariant& , RplStackFrame* ) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASNamedValue::dump(FILE* fp, int indent) +void RplASNamedValue::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%snamedValue %s id: %d attr: 0x%x %s\n", tabs, + writer.formatIndented(indent, "namedValue %s id: %d attr: 0x%x %s", m_name.toUtf8().constData(), m_id, m_attributes, positionStr(buffer, sizeof buffer)); } @@ -801,20 +802,19 @@ RplASIndexedValue::RplASIndexedValue() : } /** - * @brief Writes the internals into a file. + * @brief Writes the internals into an output media. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASIndexedValue::dump(FILE* fp, int indent) +void RplASIndexedValue::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sindexedValue id: %d index: %d parent: %d %s\n", tabs, + writer.formatIndented(indent, "indexedValue id: %d index: %d parent: %d %s", m_id, m_child2->id(), m_child->id(), positionStr(buffer, sizeof buffer)); - m_child2->dump(fp, indent + 1); - m_child->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); + m_child->dump(writer, indent + 1); } /** @class RplVarDefinition rplastree.hpp "rplexpr/rplastree.hpp" @@ -838,25 +838,24 @@ RplASVarDefinition::RplASVarDefinition() : /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASVarDefinition::dump(FILE* fp, int indent) +void RplASVarDefinition::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); RplASNamedValue* namedValue = dynamic_cast(m_child2); QByteArray name = namedValue->name().toUtf8(); char buffer[256]; - fprintf(fp, "%svarDef %s id: %d namedValue: %d value: %d succ: %d %s\n", - tabs, name.constData(), m_id, + writer.formatIndented(indent, "varDef %s id: %d namedValue: %d value: %d succ: %d %s", + name.constData(), m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child3 == NULL ? 0 : m_child3->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); if (m_child3 != NULL) - m_child3->dump(fp, indent + 1); + m_child3->dump(writer, indent + 1); } /** @@ -941,22 +940,21 @@ RplASNode1* RplASExprStatement::calc(RplASVariant& ) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASExprStatement::dump(FILE* fp, int indent) +void RplASExprStatement::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; if (m_id == 40) m_id = 40; - fprintf(fp, "%sExpr id: %d expr: %d succ: %d %s\n", tabs, m_id, + writer.formatIndented(indent, "Expr id: %d expr: %d succ: %d %s", m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); } /** @class RplASNode1 rplastree.hpp "rplexpr/rplastree.hpp" @@ -1004,15 +1002,16 @@ void RplASNode1::setChild(RplASItem* child) /** * @brief Writes the internals of a statement list into a file. * - * @param fp file to write + * @param writer writes to output media * @param indent the indent level of the statement list * @param chain the chain of statements to dump */ -void RplASNode1::dumpStatements(FILE* fp, int indent, RplASItem* statements) +void RplASNode1::dumpStatements(RplWriter& writer, int indent, + RplASItem* statements) { RplASNode1* chain = dynamic_cast(statements); while (chain != NULL){ - chain->dump(fp, indent); + chain->dump(writer, indent); chain = dynamic_cast(chain->m_child); } } @@ -1267,21 +1266,20 @@ int RplASUnaryOp::getOperator() const /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASUnaryOp::dump(FILE* fp, int indent) +void RplASUnaryOp::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sUnary %d op: %s (%d) expr: %d %s\n", tabs, + writer.formatIndented(indent, "Unary %d op: %s (%d) expr: %d %s", m_id, RplLexer::m_active->nameOfOp(m_operator).constData(), m_operator, m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer) ); if (m_child != NULL) - m_child->dump(fp, indent + 1); + m_child->dump(writer, indent + 1); } @@ -1360,18 +1358,17 @@ bool RplASCondition::calcAsBool() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASCondition::dump(FILE* fp, int indent) +void RplASCondition::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sCondition %d Child: %d %s\n", tabs, m_id, + writer.formatIndented(indent, "Condition %d Child: %d %s", m_id, m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child != NULL) - m_child->dump(fp, indent); + m_child->dump(writer, indent); } /** @class RplASIf rplastree.hpp "rplexpr/rplastree.hpp" @@ -1421,25 +1418,25 @@ void RplASIf::execute() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASIf::dump(FILE* fp, int indent) +void RplASIf::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sIf id: %d condition: %d then: %d else: %d succ: %d%s\n", tabs, + writer.formatIndented(indent, + "If id: %d condition: %d then: %d else: %d succ: %d%s", m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child3 == NULL ? 0 : m_child3->id(), m_child4 == NULL ? 0 : m_child4->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); if (m_child3 != NULL) - m_child3->dump(fp, indent + 1); + m_child3->dump(writer, indent + 1); if (m_child4 != NULL) - m_child4->dump(fp, indent + 1); + m_child4->dump(writer, indent + 1); } /** @class RplASFor rplastree.hpp "rplexpr/rplastree.hpp" @@ -1481,14 +1478,13 @@ void RplASForIterated::execute() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASForIterated::dump(FILE* fp, int indent) +void RplASForIterated::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sforIt id: %d var: %d set: %d body: %d succ: %d %s\n", tabs, + writer.formatIndented(indent, "forIt id: %d var: %d set: %d body: %d succ: %d %s", m_id, m_child3 == NULL ? 0 : m_child3->id(), m_child4 == NULL ? 0 : m_child4->id(), @@ -1496,10 +1492,10 @@ void RplASForIterated::dump(FILE* fp, int indent) m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child3 != NULL) - m_child3->dump(fp, indent + 1); + m_child3->dump(writer, indent + 1); if (m_child4 != NULL) - m_child4->dump(fp, indent + 1); - dumpStatements(fp, indent + 1, m_child2); + m_child4->dump(writer, indent + 1); + dumpStatements(writer, indent + 1, m_child2); } /** @class RplASForCounted rplastree.hpp "rplexpr/rplastree.hpp" @@ -1558,15 +1554,14 @@ void RplASForCounted::execute() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASForCounted::dump(FILE* fp, int indent) +void RplASForCounted::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sforC id: %d var: %d from: %d to: %d step: %d body: %d succ: %d %s\n", - tabs, m_id, + writer.formatIndented(indent, "forC id: %d var: %d from: %d to: %d step: %d body: %d succ: %d %s", + m_id, m_child3 == NULL ? 0 : m_child3->id(), m_child4 == NULL ? 0 : m_child4->id(), m_child5 == NULL ? 0 : m_child5->id(), @@ -1575,14 +1570,14 @@ void RplASForCounted::dump(FILE* fp, int indent) m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child3 != NULL) - m_child3->dump(fp, indent + 1); + m_child3->dump(writer, indent + 1); if (m_child4 != NULL) - m_child4->dump(fp, indent + 1); + m_child4->dump(writer, indent + 1); if (m_child5 != NULL) - m_child5->dump(fp, indent + 1); + m_child5->dump(writer, indent + 1); if (m_child6 != NULL) - m_child6->dump(fp, indent + 1); - dumpStatements(fp, indent + 1, m_child2); + m_child6->dump(writer, indent + 1); + dumpStatements(writer, indent + 1, m_child2); } /** @class RplASWhile rplastree.hpp "rplexpr/rplastree.hpp" @@ -1626,21 +1621,22 @@ void RplASWhile::execute() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASWhile::dump(FILE* fp, int indent) +void RplASWhile::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); + char buffer[256]; - fprintf(fp, "%swhile id: %d condition: %d body: %d succ: %d %s\n", tabs, m_id, + writer.formatIndented(indent, "while id: %d condition: %d body: %d succ: %d %s", + m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child3 == NULL ? 0 : m_child3->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); - dumpStatements(fp, indent + 1, m_child3); + m_child2->dump(writer, indent + 1); + dumpStatements(writer, indent + 1, m_child3); } /** @class RplASRepeat rplastree.hpp "rplexpr/rplastree.hpp" @@ -1686,21 +1682,21 @@ void RplASRepeat::execute() /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASRepeat::dump(FILE* fp, int indent) +void RplASRepeat::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%srepeat id: %d condition: %d body: %d succ: %d %s\n", tabs, m_id, + writer.formatIndented(indent, "repeat id: %d condition: %d body: %d succ: %d %s", + m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child3 == NULL ? 0 : m_child3->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); - dumpStatements(fp, indent + 1, m_child3); + m_child2->dump(writer, indent + 1); + dumpStatements(writer, indent + 1, m_child3); } /** @class RplASClass rplastree.hpp "rplexpr/rplastree.hpp" @@ -1744,16 +1740,15 @@ const QString& RplASClass::name() const /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASClass::dump(FILE* fp, int indent) +void RplASClass::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); - fprintf(fp, "%sclass %s super: %s\n", tabs, m_name.toUtf8().constData(), + writer.formatIndented(indent, "class %s super: %s", m_name.toUtf8().constData(), m_superClass == NULL ? "" : m_superClass->name().toUtf8().constData()); - m_symbols->dump(fp, indent); + m_symbols->dump(writer, indent); } /** @@ -1953,29 +1948,27 @@ void RplASTree::clear() */ void RplASTree::dump(const char* filename, int flags, const char* header) { - FILE* fp = fopen(filename, "w"); - if (fp != NULL){ - if (header != NULL) - fprintf(fp, "%s\n", header); - if (flags & DMP_GLOBALS){ - m_global->dump(fp, 0, "=== Globals:"); + RplFileWriter writer(filename); + if (header != NULL) + writer.writeLine(header); + if (flags & DMP_GLOBALS){ + m_global->dump(writer, 0, "=== Globals:"); + } + if (flags & DMP_MODULES){ + QList sorted; + sorted.reserve(m_modules.size()); + SymbolSpaceMap::iterator it; + for (it = m_modules.begin(); it != m_modules.end(); it++){ + sorted.append(it.key()); } - if (flags & DMP_MODULES){ - QList sorted; - sorted.reserve(m_modules.size()); - SymbolSpaceMap::iterator it; - for (it = m_modules.begin(); it != m_modules.end(); it++){ - sorted.append(it.key()); - } - qSort(sorted.begin(), sorted.end(), qLess()); - QList::iterator it2; - for (it2 = sorted.begin(); it2 != sorted.end(); it2++){ - RplSymbolSpace* space = m_modules[*it2]; - space->dump(fp, 0); - } + qSort(sorted.begin(), sorted.end(), qLess()); + QList::iterator it2; + for (it2 = sorted.begin(); it2 != sorted.end(); it2++){ + RplSymbolSpace* space = m_modules[*it2]; + space->dump(writer, 0); } - fclose(fp); } + writer.close(); } /** @class RplASMethodCall rplastree.hpp "rplexpr/rplastree.hpp" @@ -2007,23 +2000,22 @@ RplASMethodCall::RplASMethodCall(const QString& name, RplASItem* parent) : /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASMethodCall::dump(FILE* fp, int indent) +void RplASMethodCall::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%scall %s Id: %d args: %d parent: %d succ: %d %s\n", tabs, + writer.formatIndented(indent, "call %s Id: %d args: %d parent: %d succ: %d %s", m_name.toUtf8().constData(), m_id, m_child2 == NULL ? 0 : m_child2->id(), m_child3 == NULL ? 0 : m_child3->id(), m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); if (m_child3 != NULL) - m_child3->dump(fp, indent + 1); + m_child3->dump(writer, indent + 1); } /** @@ -2100,24 +2092,24 @@ void RplASBinaryOp::setOperator(int op) /** * @brief Writes the internals into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASBinaryOp::dump(FILE* fp, int indent) +void RplASBinaryOp::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); + const QByteArray& opName = RplLexer::m_active->nameOfOp(m_operator); char buffer[256]; - fprintf(fp, "%sBinOp id: %d op: %s (%d) left: %d right: %d %s\n", - tabs, m_id, + writer.formatIndented(indent, "BinOp id: %d op: %s (%d) left: %d right: %d %s", + m_id, opName.constData(), m_operator, m_child == NULL ? 0 : m_child->id(), m_child2 == NULL ? 0 : m_child2->id(), positionStr(buffer, sizeof buffer)); if (indent < 32 && m_child != NULL) - m_child->dump(fp, indent + 1); + m_child->dump(writer, indent + 1); if (indent < 32 && m_child2 != NULL) - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); } /** @class RplASMethod rplastree.hpp "rplexpr/rplastree.hpp" @@ -2149,26 +2141,27 @@ RplASMethod::RplASMethod(const QString& name, RplASTree& tree) : /** * @brief Writes the internals of the instance into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASMethod::dump(FILE* fp, int indent) +void RplASMethod::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); + char buffer[256]; - fprintf(fp, "%sMethod %s %s(", tabs, + writer.indent(indent); + writer.format("Method %s %s(", m_resultType == NULL ? "" : m_resultType->name().toUtf8().constData(), m_name.toUtf8().constData()); RplSymbolSpace* parent = m_symbols->parent(); - fprintf(fp, ") id: %d parent: %s args: %d body: %d %s\n", m_id, + writer.formatLine(") id: %d parent: %s args: %d body: %d %s", m_id, parent == NULL ? "" : parent->name().toUtf8().constData(), m_child2 == NULL ? 0 : m_child2->id(), m_child->id(), positionStr(buffer, sizeof buffer)); if (m_child2 != NULL) - m_child2->dump(fp, indent + 1); - dumpStatements(fp, indent + 1, m_child); - m_symbols->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); + dumpStatements(writer, indent + 1, m_child); + m_symbols->dump(writer, indent + 1); } /** * @brief Returns the symbol space of the instance. @@ -2260,32 +2253,31 @@ RplASArgument::RplASArgument() : /** * @brief Writes the internals of the instance into a file. * - * @param fp target file + * @param writer writes to output * @param no current number of the argument: 1..N * @param indent nesting level - * @param tabs a string with tabs (count: indent) */ -void RplASArgument::dumpOne(FILE* fp, int no, int indent, char tabs[]) +void RplASArgument::dumpOne(RplWriter& writer, int no, int indent) { int succ = m_child == NULL ? 0 : m_child->id(); - fprintf(fp, "%sarg %d id: %d expr: %d succ: %d\n", tabs, no, m_id, + writer.formatIndented(indent, "arg %d id: %d expr: %d succ: %d", no, m_id, child2()->id(), succ); - m_child2->dump(fp, indent + 1); + m_child2->dump(writer, indent + 1); } /** * @brief Writes the internals of the instance into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASArgument::dump(FILE* fp, int indent) +void RplASArgument::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); + RplASArgument* current = this; int no = 0; do { - current->dumpOne(fp, ++no, indent, tabs); + current->dumpOne(writer, ++no, indent); current = static_cast(current->child()); } while (current != NULL); } @@ -2311,18 +2303,17 @@ RplASField::RplASField(const QString& name) : /** * @brief Writes the internals of the instance into a file. * - * @param fp target file + * @param writer writes to output * @param indent nesting level */ -void RplASField::dump(FILE* fp, int indent) +void RplASField::dump(RplWriter& writer, int indent) { - DEFINE_TABS(indent); char buffer[256]; - fprintf(fp, "%sfield %s id: %d parent: %d succ: %s\n", tabs, + writer.formatIndented(indent, "field %s id: %d parent: %d succ: %s", m_name.toUtf8().constData(), m_id, m_child == NULL ? 0 : m_child->id(), positionStr(buffer, sizeof buffer)); - m_child->dump(fp, indent + 1); + m_child->dump(writer, indent + 1); } diff --git a/rplexpr/rplastree.hpp b/rplexpr/rplastree.hpp index 2e21c12..cf36ecd 100644 --- a/rplexpr/rplastree.hpp +++ b/rplexpr/rplastree.hpp @@ -2,7 +2,7 @@ * 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/. + * You also can use the licence from http://www.wtwriterl.net/. * The original sources can be found on https://github.com/republib. */ @@ -134,13 +134,13 @@ public: char* positionStr(char buffer[], size_t bufferSize) const; public: /** - * @brief Writes the content of the instance into a file. + * @brief Writes the content of the instance into an output medium. * - * @param fp the target file + * @param writer writes to output media * @param indent nesting level: so many tabs will be used as prefix * @param header NULL or the headline */ - virtual void dump(FILE* fp, int indent) = 0; + virtual void dump(RplWriter& writer,int indent) = 0; public: static void reset(); RplASItemType nodeType() const; @@ -178,7 +178,7 @@ public: public: virtual RplASNode1* calc(RplASVariant& value); public: - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer,int indent); RplASVariant& value(); private: RplASVariant m_value; @@ -194,7 +194,7 @@ public: RplASItem* child() const; void setChild(RplASItem* child); public: - static void dumpStatements(FILE* fp, int indent, RplASItem* statements); + static void dumpStatements(RplWriter& writer, int indent, RplASItem* statements); protected: RplASItem* m_child; }; @@ -271,7 +271,7 @@ public: public: virtual RplASNode1* calc(RplASVariant& value); public: - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer,int indent); RplASVariant& value(); RplASListOfVariants* list(); private: @@ -284,7 +284,7 @@ public: public: virtual RplASNode1* calc(RplASVariant& value); public: - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer,int indent); RplASVariant& value(); RplASMapOfVariants* map(); private: @@ -316,7 +316,7 @@ public: const QString& name() const; public: virtual RplASNode1* calc(RplASVariant& value, RplStackFrame* frame); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); RplASClass* dataType() const; protected: @@ -329,7 +329,7 @@ class RplASIndexedValue : public RplASNode2 { public: RplASIndexedValue(); public: - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); }; class RplASStatement @@ -348,7 +348,7 @@ public: public: virtual void execute(); virtual RplASNode1* calc(RplASVariant& value); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); const QString& name() const; RplASClass* datatype() const; }; @@ -360,7 +360,7 @@ public: public: virtual void execute(); virtual RplASNode1* calc(RplASVariant& value); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); }; class RplASUnaryOp : public RplASNode1 @@ -369,7 +369,7 @@ public: RplASUnaryOp(int op, RplASItemType type); public: int getOperator() const; - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); private: int m_operator; @@ -381,7 +381,7 @@ public: public: int getOperator() const; void setOperator(int op); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); private: int m_operator; }; @@ -393,7 +393,7 @@ public: public: RplASNode1* calc(RplASVariant& value); bool calcAsBool(); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); }; class RplASIf : public RplASNode4, public RplASStatement @@ -402,7 +402,7 @@ public: RplASIf(); public: virtual void execute(); - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); }; class RplASForIterated : public RplASNode4, public RplASStatement @@ -411,7 +411,7 @@ public: RplASForIterated(RplASNamedValue* variable); public: virtual void execute(); - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); }; class RplASForCounted : public RplASNode6, public RplASStatement @@ -420,7 +420,7 @@ public: RplASForCounted(RplASNamedValue* variable); public: virtual void execute(); - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); }; class RplASWhile : public RplASNode3, public RplASStatement @@ -429,7 +429,7 @@ public: RplASWhile(); public: virtual void execute(); - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); }; class RplASRepeat : public RplASNode3, public RplASStatement @@ -438,7 +438,7 @@ public: RplASRepeat(); public: virtual void execute(); - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); }; class RplASArgument : public RplASNode2 @@ -446,9 +446,9 @@ class RplASArgument : public RplASNode2 public: RplASArgument(); public: - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); protected: - void dumpOne(FILE* fp, int no, int indent, char tabs[]); + void dumpOne(RplWriter& writer, int no, int indent); }; class RplASMethod; @@ -457,7 +457,7 @@ class RplASMethodCall : public RplASNode3, public RplASStatement public: RplASMethodCall(const QString& name, RplASItem* parent); public: - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); virtual void execute(); public: @@ -485,7 +485,7 @@ class RplASField : public RplASNode1 public: RplASField(const QString& name); public: - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); private: QString m_name; }; @@ -499,7 +499,7 @@ public: RplASMethod(const QString& name, RplASTree& tree); public: void execute(); - void dump(FILE* fp, int indent); + void dump(RplWriter& writer, int indent); RplSymbolSpace* symbols() const; void setSymbols(); const QString& name() const; @@ -557,7 +557,7 @@ public: virtual QString toString(void *object, int maxLength = 80) const = 0; public: const QString& name() const; - virtual void dump(FILE* fp, int indent); + virtual void dump(RplWriter& writer, int indent); void setSymbols(); protected: QString m_name; diff --git a/unittests/main.cpp b/unittests/main.cpp index 4026016..b7676aa 100644 --- a/unittests/main.cpp +++ b/unittests/main.cpp @@ -42,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 index 8a2700c..5816301 100644 --- a/unittests/rplwriter_test.cpp +++ b/unittests/rplwriter_test.cpp @@ -21,9 +21,13 @@ private: writer.writeLine("abc"); writer.formatLine("%04d", 42); writer.writeIndented(3, "123"); + writer.indent(2); + writer.write("pi"); + writer.format("%3c%.2f", ':', 3.1415); + writer.writeLine(); writer.close(); QByteArray current = RplString::read(fn, false); - checkE("abc\n0042\n\t\t\t123\n", current); + checkE("abc\n0042\n\t\t\t123\n\t\tpi :3.14\n", current); } public: