From 8d4981fcc1c545396df3eac87a3c1a67f3d30038 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Mon, 3 Feb 2020 15:53:56 +0300 Subject: [texo] Abolished dependency on ScriptPP. --- src/exporter.cpp | 12 +- src/exporter.hpp | 4 +- src/file.cpp | 10 +- src/file.hpp | 8 +- src/html.cpp | 192 ++++++++++--------------------- src/html.hpp | 39 ++++--- src/importer.cpp | 17 ++- src/importer.hpp | 16 +-- src/lines.cpp | 4 +- src/lines.hpp | 4 +- src/markdown.cpp | 340 ++++++++++++++++++++----------------------------------- src/markdown.hpp | 59 +++++----- src/plain.cpp | 84 ++++++-------- src/plain.hpp | 20 ++-- src/producer.cpp | 194 ++++++++++--------------------- src/producer.hpp | 65 +++++------ src/script.cpp | 38 ++++++- src/script.hpp | 14 ++- 18 files changed, 446 insertions(+), 674 deletions(-) (limited to 'src') diff --git a/src/exporter.cpp b/src/exporter.cpp index 6a190ee..d65a9fc 100644 --- a/src/exporter.cpp +++ b/src/exporter.cpp @@ -1,15 +1,17 @@ #include "exporter.hpp" +#include + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Exporter */ -bool TexoExporter::Put(const ScriptVariable & str) +bool TexoExporter::Put(const char *str) { - bool ok = true; - const int len = str.Length(); - for (int i = 0; ok && i < len; ++i) { - ok = Put(str[i]); + bool ok = true; + while (ok && *str) { + ok = Put(*str); + ++str; } return ok; } diff --git a/src/exporter.hpp b/src/exporter.hpp index 18a2e5d..b75ab0e 100644 --- a/src/exporter.hpp +++ b/src/exporter.hpp @@ -1,8 +1,6 @@ #ifndef TEXO_INCLUDED_EXPORTER #define TEXO_INCLUDED_EXPORTER -#include - /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Exporter @@ -11,7 +9,7 @@ class TexoExporter { public: virtual bool Put(char c) = 0; - virtual bool Put(const ScriptVariable & str); + virtual bool Put(const char *str); }; diff --git a/src/file.cpp b/src/file.cpp index c46a1f2..cbe9222 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -1,21 +1,23 @@ #include "file.hpp" +#include + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo File Exporter */ -TexoExporterFile::TexoExporterFile(FILE * file): file(file) +TexoFile::TexoFile(FILE *file): file(file) {} -bool TexoExporterFile::Put(char c) +bool TexoFile::Put(char c) { fputc(c, file); return !ferror(file); } -bool TexoExporterFile::Put(const ScriptVariable & str) +bool TexoFile::Put(const char *str) { - fwrite(str.c_str(), 1, str.Length(), file); + fwrite(str, 1, strlen(str), file); return !ferror(file); } diff --git a/src/file.hpp b/src/file.hpp index 171e6d3..280e6ed 100644 --- a/src/file.hpp +++ b/src/file.hpp @@ -9,17 +9,17 @@ * Texo File Exporter * Simple utility class to make output into FILE *variable. */ -class TexoExporterFile: public TexoExporter { +class TexoFile: public TexoExporter { public: - TexoExporterFile(FILE * file); + TexoFile(FILE *file); bool Put(char c); - bool Put(const ScriptVariable & str); + bool Put(const char *str); private: - FILE * file; + FILE *file; }; diff --git a/src/html.cpp b/src/html.cpp index d492c19..68a8800 100644 --- a/src/html.cpp +++ b/src/html.cpp @@ -9,46 +9,36 @@ TexoProducerHTML::TexoProducerHTML(TexoExporter & exporter): bool TexoProducerHTML::TruePut(char c) { switch (c) { - case '<': - return exporter.Put("<"); - break; - case '>': - return exporter.Put(">"); - break; - case '&': - return exporter.Put("&"); - break; - default: - return exporter.Put(c); + case '<': return exporter.Put("<"); + case '>': return exporter.Put(">"); + case '&': return exporter.Put("&"); + default: return exporter.Put(c); } } -bool TexoProducerHTML::StartCode() -{ - return exporter.Put("\n
\n");
-}
-
 bool TexoProducerHTML::StartHeader(int level)
 {
-    if (level <= 1) {
-        return exporter.Put("\n
\n"); - } else if (level == 2) { - return exporter.Put("\n
\n"); - } else if (level == 3) { - return exporter.Put("\n

\n"); - } else if (level == 4) { - return exporter.Put("\n

\n"); - } else if (level == 5) { - return exporter.Put("\n

\n"); - } else { - return exporter.Put("\n

\n"); + switch (level) { + case 1: return exporter.Put("\n

\n"); + case 2: return exporter.Put("\n
\n"); + case 3: return exporter.Put("\n

\n"); + case 4: return exporter.Put("\n

\n"); + case 5: return exporter.Put("\n

\n"); + default: return exporter.Put("\n

\n"); } } -bool TexoProducerHTML::StartParagraph() +bool TexoProducerHTML::CloseHeader(int level) { - return exporter.Put("\n

\n"); + switch (level) { + case 1: return exporter.Put("\n

\n"); + case 2: return exporter.Put("\n\n"); + case 3: return exporter.Put("\n\n"); + case 4: return exporter.Put("\n\n"); + case 5: return exporter.Put("\n\n"); + default: return exporter.Put("\n\n"); + } } bool TexoProducerHTML::StartQuote() @@ -56,127 +46,69 @@ bool TexoProducerHTML::StartQuote() return exporter.Put("\n

\n"); } -bool TexoProducerHTML::CloseCode() +bool TexoProducerHTML::CloseQuote() { - return exporter.Put("\n

\n"); + return exporter.Put("\n

\n"); } -bool TexoProducerHTML::CloseHeader(int level) -{ - if (level <= 1) { - return exporter.Put("\n\n"); - } else if (level == 2) { - return exporter.Put("\n\n"); - } else if (level == 3) { - return exporter.Put("\n\n"); - } else if (level == 4) { - return exporter.Put("\n\n"); - } else if (level == 5) { - return exporter.Put("\n\n"); - } else { - return exporter.Put("\n\n"); - } -} +bool TexoProducerHTML::StartCode() { return exporter.Put("\n
\n"); }
+bool TexoProducerHTML::CloseCode()       { return exporter.Put("\n
\n"); } -bool TexoProducerHTML::CloseParagraph() -{ - return exporter.Put("\n

\n"); -} +bool TexoProducerHTML::StartParagraph() { return exporter.Put("\n

\n"); } +bool TexoProducerHTML::CloseParagraph() { return exporter.Put("\n

\n"); } -bool TexoProducerHTML::CloseQuote() -{ - return exporter.Put("\n

\n"); -} +bool TexoProducerHTML::StartBold() { return exporter.Put(""); } +bool TexoProducerHTML::CloseBold() { return exporter.Put(""); } -bool TexoProducerHTML::StartBold() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::StartItalic() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::StartMono() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::StartStrike() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::StartUnderline() -{ - return exporter.Put(""); -} +bool TexoProducerHTML::StartItalic() { return exporter.Put(""); } +bool TexoProducerHTML::CloseItalic() { return exporter.Put(""); } -bool TexoProducerHTML::CloseBold() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::CloseItalic() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::CloseMono() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::CloseStrike() -{ - return exporter.Put(""); -} -bool TexoProducerHTML::CloseUnderline() -{ - return exporter.Put(""); -} +bool TexoProducerHTML::StartMono() { return exporter.Put(""); } +bool TexoProducerHTML::CloseMono() { return exporter.Put(""); } + +bool TexoProducerHTML::StartStrike() { return exporter.Put(""); } +bool TexoProducerHTML::CloseStrike() { return exporter.Put(""); } + +bool TexoProducerHTML::StartUnderline() { return exporter.Put(""); } +bool TexoProducerHTML::CloseUnderline() { return exporter.Put(""); } -bool TexoProducerHTML::StartLink(const ScriptVariable & link, - const ScriptVariable & title) +bool TexoProducerHTML::StartLink(const char *link, const char *title) { - bool ok = true; - if (link != "") { - ok = ok && exporter.Put(""); + if (!link) { + return true; + } + bool ok = exporter.Put(""); } -bool TexoProducerHTML::CloseLink(const ScriptVariable & link, - const ScriptVariable & title) +bool TexoProducerHTML::CloseLink(const char *link, const char *title) { - if (link != "") { - return exporter.Put(""); - } else { + if (!link) { return true; } + return exporter.Put(""); } -bool TexoProducerHTML::TruePutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title) +bool TexoProducerHTML::TruePutImage(const char *src, + const char *alt, + const char *title) { - bool ok = true; - if (src != "") { - ok = ok && exporter.Put("");
-            ok = ok && exporter.Put(alt);
-        }
-        if (title != "") {
-            ok = ok && exporter.Put(""); + if (!src) { + return true; + } + bool ok = exporter.Put("") && exporter.Put(alt);
+    }
+    if (title) {
+        ok = ok && exporter.Put(""); } bool TexoProducerHTML::TruePutHorizontalRule() diff --git a/src/html.hpp b/src/html.hpp index cc69f10..a954f30 100644 --- a/src/html.hpp +++ b/src/html.hpp @@ -2,45 +2,52 @@ #define TEXO_INCLUDED_HTML #include "exporter.hpp" -#include "importer.hpp" #include "producer.hpp" class TexoProducerHTML: public TexoProducerStrict { public: - TexoProducerHTML(TexoExporter & exporter); + TexoProducerHTML(TexoExporter &exporter); + protected: bool TruePut(char c); - bool StartCode(); + bool StartHeader(int level); - bool StartParagraph(); - bool StartQuote(); + bool CloseHeader(int level); + bool StartCode(); bool CloseCode(); - bool CloseHeader(int level); + + bool StartParagraph(); bool CloseParagraph(); + + bool StartQuote(); bool CloseQuote(); - bool StartBold(); - bool StartItalic(); - bool StartMono(); - bool StartStrike(); - bool StartUnderline(); + bool StartBold(); bool CloseBold(); + + bool StartItalic(); bool CloseItalic(); + + bool StartMono(); bool CloseMono(); + + bool StartStrike(); bool CloseStrike(); + + bool StartUnderline(); bool CloseUnderline(); - bool StartLink(const ScriptVariable & link, const ScriptVariable & title); - bool CloseLink(const ScriptVariable & link, const ScriptVariable & title); + bool StartLink(const char *link, const char *title); + bool CloseLink(const char *link, const char *title); + + + bool TruePutImage(const char *src, const char *alt, const char *title); - bool TruePutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title); bool TruePutHorizontalRule(); }; diff --git a/src/importer.cpp b/src/importer.cpp index bab7f8b..f46b7d2 100644 --- a/src/importer.cpp +++ b/src/importer.cpp @@ -4,7 +4,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Importer */ -TexoImporter::TexoImporter(TexoProducer & producer): +TexoImporter::TexoImporter(TexoProducer &producer): producer(producer), ok(true) {} @@ -24,12 +24,12 @@ bool TexoImporter::Put(char c) return ok = ok && TruePut(c); } -bool TexoImporter::Put(const ScriptVariable & s) +bool TexoImporter::Put(const char *s) { return ok = ok && TruePut(s); } -bool TexoImporter::Put(FILE * f) +bool TexoImporter::Put(FILE *f) { return ok = ok && TruePut(f); } @@ -40,17 +40,16 @@ bool TexoImporter::TrueEnd() return producer.End(); } - -bool TexoImporter::TruePut(const ScriptVariable & str) +bool TexoImporter::TruePut(const char *str) { - const int len = str.Length(); - for (int i = 0; ok && i < len; ++i) { - ok = TruePut(str[i]); + while (*str) { + ok = TruePut(*str); + ++str; } return ok; } -bool TexoImporter::TruePut(FILE * file) +bool TexoImporter::TruePut(FILE *file) { if (file) { for (int c = fgetc(file); ok && c != EOF; c = fgetc(file)) { diff --git a/src/importer.hpp b/src/importer.hpp index c5a302d..643fe5a 100644 --- a/src/importer.hpp +++ b/src/importer.hpp @@ -12,27 +12,27 @@ */ class TexoImporter { public: - TexoImporter(TexoProducer & producer); + TexoImporter(TexoProducer &producer); ~TexoImporter(); bool End(); + bool Put(char c); - bool Put(const ScriptVariable & s); - bool Put(FILE * f); + bool Put(const char *s); + bool Put(FILE *f); protected: virtual bool TrueEnd(); - virtual bool TruePut(char c) = 0; - virtual bool TruePut(const ScriptVariable & str); - virtual bool TruePut(FILE * file); + virtual bool TruePut(const char *str); + virtual bool TruePut(FILE *file); - TexoProducer & producer; - bool ok; + TexoProducer &producer; + bool ok; }; diff --git a/src/lines.cpp b/src/lines.cpp index 3aafde5..c7b605b 100644 --- a/src/lines.cpp +++ b/src/lines.cpp @@ -4,7 +4,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Lines Producer */ -TexoProducerLines::TexoProducerLines(TexoExporter & exporter): +TexoProducerLines::TexoProducerLines(TexoExporter &exporter): TexoProducer(exporter), newline(true) {} @@ -57,7 +57,7 @@ bool TexoProducerLines::Quote() /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Lines Importer */ -TexoImporterLines::TexoImporterLines(TexoProducer & producer): +TexoImporterLines::TexoImporterLines(TexoProducer &producer): TexoImporter(producer), newline(true), quote(false) {} diff --git a/src/lines.hpp b/src/lines.hpp index 8049d61..dc8f00e 100644 --- a/src/lines.hpp +++ b/src/lines.hpp @@ -11,7 +11,7 @@ */ class TexoProducerLines: public TexoProducer { public: - TexoProducerLines(TexoExporter & exporter); + TexoProducerLines(TexoExporter &exporter); bool End(); @@ -34,7 +34,7 @@ private: */ class TexoImporterLines: public TexoImporter { public: - TexoImporterLines(TexoProducer & producer); + TexoImporterLines(TexoProducer &producer); protected: diff --git a/src/markdown.cpp b/src/markdown.cpp index fd5aaec..e1f0766 100644 --- a/src/markdown.cpp +++ b/src/markdown.cpp @@ -1,7 +1,7 @@ #include "markdown.hpp" -TexoProducerMarkdown::TexoProducerMarkdown(TexoExporter & exporter): +TexoProducerMarkdown::TexoProducerMarkdown(TexoExporter &exporter): TexoProducer(exporter), quoted(false), newline(false), header(false), code(false), nospace(true) {} @@ -52,22 +52,18 @@ bool TexoProducerMarkdown::Code() bool TexoProducerMarkdown::Header(int level) { - bool ok = Close(); - if (level <= 1) { - ok = ok && exporter.Put("###### "); - } else if (level == 2) { - ok = ok && exporter.Put("##### "); - } else if (level == 3) { - ok = ok && exporter.Put("#### "); - } else if (level == 4) { - ok = ok && exporter.Put("### "); - } else if (level == 5) { - ok = ok && exporter.Put("## "); - } else { - ok = ok && exporter.Put("# "); + if (!Close()) { + return false; } header = true; - return ok; + switch (level) { + case 1: return exporter.Put("###### "); + case 2: return exporter.Put("##### "); + case 3: return exporter.Put("#### "); + case 4: return exporter.Put("### "); + case 5: return exporter.Put("## "); + default: return exporter.Put("# "); + } } bool TexoProducerMarkdown::Paragraph() @@ -83,32 +79,16 @@ bool TexoProducerMarkdown::Quote() } -bool TexoProducerMarkdown::Mono() -{ - return Mod("`"); -} -bool TexoProducerMarkdown::Bold() -{ - return Mod("**"); -} -bool TexoProducerMarkdown::Italic() -{ - return Mod("_"); -} -bool TexoProducerMarkdown::Underline() -{ - return Mod("++"); -} -bool TexoProducerMarkdown::Strike() -{ - return Mod("~~"); -} +bool TexoProducerMarkdown::Mono() { return Mod("`"); } +bool TexoProducerMarkdown::Bold() { return Mod("**"); } +bool TexoProducerMarkdown::Italic() { return Mod("_"); } +bool TexoProducerMarkdown::Underline() { return Mod("++"); } +bool TexoProducerMarkdown::Strike() { return Mod("~~"); } -bool TexoProducerMarkdown::Link(const ScriptVariable & link, - const ScriptVariable & title) +bool TexoProducerMarkdown::Link(const char *link, const char *title) { bool ok = Link(); - if (link != "") { + if (link) { ok = ok && exporter.Put('['); link_link = link; link_title = title; @@ -119,10 +99,10 @@ bool TexoProducerMarkdown::Link(const ScriptVariable & link, bool TexoProducerMarkdown::Link() { bool ok = true; - if (link_link != "") { + if (link_link) { ok = ok && exporter.Put("]("); ok = ok && exporter.Put(link_link); - if (link_title != "") { + if (link_title) { ok = ok && exporter.Put(" \""); ok = ok && exporter.Put(link_title); ok = ok && exporter.Put('"'); @@ -133,17 +113,17 @@ bool TexoProducerMarkdown::Link() } -bool TexoProducerMarkdown::PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title) +bool TexoProducerMarkdown::PutImage(const char *src, + const char *alt, + const char *title) { bool ok = true; - if (src != "") { + if (src) { ok = ok && exporter.Put("!["); ok = ok && exporter.Put(alt); ok = ok && exporter.Put("]("); ok = ok && exporter.Put(src); - if (title != "") { + if (title) { ok = ok && exporter.Put(" \""); ok = ok && exporter.Put(title); ok = ok && exporter.Put('"'); @@ -162,12 +142,11 @@ bool TexoProducerMarkdown::PutHorizontalRule() ok = ok && exporter.Put('\n'); } nospace = true; - return ok && exporter.Put( - "--------------------------------------------------\n"); + return ok && exporter.Put("------------------------------------------\n"); } -bool TexoProducerMarkdown::Mod(const ScriptVariable & str) +bool TexoProducerMarkdown::Mod(const char *str) { newline = false; nospace = false; @@ -193,7 +172,7 @@ bool TexoProducerMarkdown::Close() } -TexoImporterMarkdown::TexoImporterMarkdown(TexoProducer & producer): +TexoImporterMarkdown::TexoImporterMarkdown(TexoProducer &producer): TexoImporter(producer), state(start) {} @@ -201,72 +180,32 @@ TexoImporterMarkdown::TexoImporterMarkdown(TexoProducer & producer): bool TexoImporterMarkdown::TruePut(char c) { switch (state) { - case start: - Start(c); - break; - case text: - Text(c); - break; - case header_text: - HeaderText(c); - break; - case quote_pre: - QuotePre(c); - break; - case quote_text: - QuoteText(c); - break; - case quote_newline: - QuoteNewline(c); - break; - case code_text: - CodeText(c); - break; - case code_newline: - CodeNewline(c); - break; - case code_end: - CodeEnd(c); - break; - case backslash: - Backslash(c); - break; - case asterisk: - Asterisk(c); - break; - case underline: - Underline(c); - break; - case plus: - Plus(c); - break; - case tilde: - Tilde(c); - break; - case newline: - Newline(c); - break; - case rule: - Rule(c); - break; - case paragraph: - Paragraph(c); - break; - case header: - Header(c); - break; - case header_pre: - HeaderPre(c); - break; - case code: - Code(c); - break; + case start: return Start(c); + case text: return Text(c); + case header_text: return HeaderText(c); + case quote_pre: return QuotePre(c); + case quote_text: return QuoteText(c); + case quote_newline: return QuoteNewline(c); + case code_text: return CodeText(c); + case code_newline: return CodeNewline(c); + case code_end: return CodeEnd(c); + case backslash: return Backslash(c); + case asterisk: return Asterisk(c); + case underline: return Underline(c); + case plus: return Plus(c); + case tilde: return Tilde(c); + case newline: return Newline(c); + case rule: return Rule(c); + case paragraph: return Paragraph(c); + case header: return Header(c); + case header_pre: return HeaderPre(c); + case code: return Code(c); } - return ok; + return ok; // Impossible } -void TexoImporterMarkdown::Start(char c) +bool TexoImporterMarkdown::Start(char c) { if (c == '\n') { state = paragraph; @@ -288,107 +227,67 @@ void TexoImporterMarkdown::Start(char c) state = text; Put(c); } + return ok; } -void TexoImporterMarkdown::Text(char c) +bool TexoImporterMarkdown::Text(char c) { back = text; switch (c) { - case '\\': - state = backslash; - break; - case '\n': - state = newline; - break; - case '*': - state = asterisk; - break; - case '_': - state = underline; - break; - case '+': - state = plus; - break; - case '~': - state = tilde; - break; - case '`': - Backquote(); - break; - default: - ok = producer.Put(c); + case '\\': state = backslash; break; + case '\n': state = newline; break; + case '*': state = asterisk; break; + case '_': state = underline; break; + case '+': state = plus; break; + case '~': state = tilde; break; + case '`': Backquote(); break; + default: ok = producer.Put(c); } + return ok; } -void TexoImporterMarkdown::HeaderText(char c) +bool TexoImporterMarkdown::HeaderText(char c) { back = header_text; switch (c) { - case '\\': - state = backslash; - break; - case '\n': - state = paragraph; - break; - case '*': - state = asterisk; - break; - case '_': - state = underline; - break; - case '+': - state = plus; - break; - case '~': - state = tilde; - break; - case '`': - Backquote(); - break; - default: - ok = producer.Put(c); + case '\\': state = backslash; break; + case '\n': state = paragraph; break; + case '*': state = asterisk; break; + case '_': state = underline; break; + case '+': state = plus; break; + case '~': state = tilde; break; + case '`': Backquote(); break; + default: ok = producer.Put(c); } + return ok; } -void TexoImporterMarkdown::QuotePre(char c) +bool TexoImporterMarkdown::QuotePre(char c) { if (c != ' ') { state = quote_text; QuoteText(c); } + return ok; } -void TexoImporterMarkdown::QuoteText(char c) +bool TexoImporterMarkdown::QuoteText(char c) { back = quote_text; switch (c) { - case '\\': - state = backslash; - break; - case '\n': - state = quote_newline; - break; - case '*': - state = asterisk; - break; - case '_': - state = underline; - break; - case '+': - state = plus; - break; - case '~': - state = tilde; - break; - case '`': - Backquote(); - break; - default: - ok = producer.Put(c); + case '\\': state = backslash; break; + case '\n': state = quote_newline; break; + case '*': state = asterisk; break; + case '_': state = underline; break; + case '+': state = plus; break; + case '~': state = tilde; break; + case '`': Backquote(); break; + default: ok = producer.Put(c); } + return ok; } -void TexoImporterMarkdown::QuoteNewline(char c) +bool TexoImporterMarkdown::QuoteNewline(char c) { if (c == '>') { ok = producer.Put('\n'); @@ -397,36 +296,25 @@ void TexoImporterMarkdown::QuoteNewline(char c) state = paragraph; Paragraph(c); } + return ok; } -void TexoImporterMarkdown::CodeText(char c) +bool TexoImporterMarkdown::CodeText(char c) { back = code_text; switch (c) { - case '\\': - state = backslash; - break; - case '\n': - state = code_newline; - break; - case '*': - state = asterisk; - break; - case '_': - state = underline; - break; - case '+': - state = plus; - break; - case '~': - state = tilde; - break; - default: - ok = producer.Put(c); + case '\\': state = backslash; break; + case '\n': state = code_newline; break; + case '*': state = asterisk; break; + case '_': state = underline; break; + case '+': state = plus; break; + case '~': state = tilde; break; + default: ok = producer.Put(c); } + return ok; } -void TexoImporterMarkdown::CodeNewline(char c) +bool TexoImporterMarkdown::CodeNewline(char c) { if (c == '`') { state = code_end; @@ -436,9 +324,10 @@ void TexoImporterMarkdown::CodeNewline(char c) state = code_text; Put(c); } + return ok; } -void TexoImporterMarkdown::CodeEnd(char c) +bool TexoImporterMarkdown::CodeEnd(char c) { if (c == '`') { ++code_quote_count; @@ -451,15 +340,16 @@ void TexoImporterMarkdown::CodeEnd(char c) } else { ok = false; } + return ok; } -void TexoImporterMarkdown::Backslash(char c) +bool TexoImporterMarkdown::Backslash(char c) { - ok = producer.Put(c); state = back; + return ok = producer.Put(c); } -void TexoImporterMarkdown::Asterisk(char c) +bool TexoImporterMarkdown::Asterisk(char c) { state = back; if (c == '*') { @@ -468,9 +358,10 @@ void TexoImporterMarkdown::Asterisk(char c) ok = producer.Italic(); Put(c); } + return ok; } -void TexoImporterMarkdown::Underline(char c) +bool TexoImporterMarkdown::Underline(char c) { state = back; if (c == '_') { @@ -479,9 +370,10 @@ void TexoImporterMarkdown::Underline(char c) ok = producer.Italic(); Put(c); } + return ok; } -void TexoImporterMarkdown::Plus(char c) +bool TexoImporterMarkdown::Plus(char c) { state = back; if (c == '+') { @@ -490,9 +382,10 @@ void TexoImporterMarkdown::Plus(char c) ok = producer.Put('+'); Put(c); } + return ok; } -void TexoImporterMarkdown::Tilde(char c) +bool TexoImporterMarkdown::Tilde(char c) { state = back; if (c == '~') { @@ -501,9 +394,10 @@ void TexoImporterMarkdown::Tilde(char c) ok = producer.Put('~'); Put(c); } + return ok; } -void TexoImporterMarkdown::Newline(char c) +bool TexoImporterMarkdown::Newline(char c) { if (c == '\n') { state = paragraph; @@ -524,9 +418,10 @@ void TexoImporterMarkdown::Newline(char c) state = text; Put(c); } + return ok; } -void TexoImporterMarkdown::Rule(char c) +bool TexoImporterMarkdown::Rule(char c) { if (c == '-') { ++rule_dash_count; @@ -541,9 +436,10 @@ void TexoImporterMarkdown::Rule(char c) state = text; Put(c); } + return ok; } -void TexoImporterMarkdown::Paragraph(char c) +bool TexoImporterMarkdown::Paragraph(char c) { if (c == '#') { state = header; @@ -563,9 +459,10 @@ void TexoImporterMarkdown::Paragraph(char c) state = text; Put(c); } + return ok; } -void TexoImporterMarkdown::Header(char c) +bool TexoImporterMarkdown::Header(char c) { if (c == '#') { if (header_level > 1) { @@ -579,17 +476,19 @@ void TexoImporterMarkdown::Header(char c) } else { ok = false; } + return ok; } -void TexoImporterMarkdown::HeaderPre(char c) +bool TexoImporterMarkdown::HeaderPre(char c) { if (c != ' ') { state = header_text; HeaderText(c); } + return ok; } -void TexoImporterMarkdown::Code(char c) +bool TexoImporterMarkdown::Code(char c) { if (c == '`') { ++code_quote_count; @@ -603,6 +502,7 @@ void TexoImporterMarkdown::Code(char c) } else { ok = false; } + return ok; } void TexoImporterMarkdown::Backquote() diff --git a/src/markdown.hpp b/src/markdown.hpp index 6d9919d..60ff56d 100644 --- a/src/markdown.hpp +++ b/src/markdown.hpp @@ -8,7 +8,7 @@ class TexoProducerMarkdown: public TexoProducer { public: - TexoProducerMarkdown(TexoExporter & exporter); + TexoProducerMarkdown(TexoExporter &exporter); bool End(); @@ -25,16 +25,14 @@ public: bool Strike(); bool Underline(); - bool Link(const ScriptVariable & link, const ScriptVariable & title); + bool Link(const char *link, const char *title); bool Link(); - bool PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title); + bool PutImage(const char *src, const char *alt, const char *title); bool PutHorizontalRule(); private: - bool Mod(const ScriptVariable & str); + bool Mod(const char *str); bool Close(); bool CloseLink(); @@ -44,14 +42,14 @@ private: bool code; bool nospace; - ScriptVariable link_link; - ScriptVariable link_title; + const char *link_link; + const char *link_title; }; class TexoImporterMarkdown: public TexoImporter { public: - TexoImporterMarkdown(TexoProducer & producer); + TexoImporterMarkdown(TexoProducer &producer); protected: @@ -80,32 +78,31 @@ private: header, header_pre, code - } state, - back; + } state, back; int header_level; int rule_dash_count; int code_quote_count; - void Start(char c); - void Text(char c); - void HeaderText(char c); - void QuotePre(char c); - void QuoteText(char c); - void QuoteNewline(char c); - void CodeText(char c); - void CodeNewline(char c); - void CodeEnd(char c); - void Backslash(char c); - void Asterisk(char c); - void Underline(char c); - void Plus(char c); - void Tilde(char c); - void Newline(char c); - void Rule(char c); - void Paragraph(char c); - void Header(char c); - void HeaderPre(char c); - void Code(char c); + bool Start(char c); + bool Text(char c); + bool HeaderText(char c); + bool QuotePre(char c); + bool QuoteText(char c); + bool QuoteNewline(char c); + bool CodeText(char c); + bool CodeNewline(char c); + bool CodeEnd(char c); + bool Backslash(char c); + bool Asterisk(char c); + bool Underline(char c); + bool Plus(char c); + bool Tilde(char c); + bool Newline(char c); + bool Rule(char c); + bool Paragraph(char c); + bool Header(char c); + bool HeaderPre(char c); + bool Code(char c); void Backquote(); }; diff --git a/src/plain.cpp b/src/plain.cpp index dca394f..db3696d 100644 --- a/src/plain.cpp +++ b/src/plain.cpp @@ -4,7 +4,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Plain Producer */ -TexoProducerPlain::TexoProducerPlain(TexoExporter & exporter): +TexoProducerPlain::TexoProducerPlain(TexoExporter &exporter): TexoProducer(exporter), quoted(false), newline(false), nospace(true) {} @@ -70,15 +70,14 @@ bool TexoProducerPlain::PutHorizontalRule() ok = exporter.Put('\n'); } nospace = true; - return ok && exporter.Put( - "--------------------------------------------------\n"); + return ok && exporter.Put("------------------------------------------\n"); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Plain Importer */ -TexoImporterPlain::TexoImporterPlain(TexoProducer & producer): +TexoImporterPlain::TexoImporterPlain(TexoProducer &producer): TexoImporter(producer), state(text) {} @@ -96,48 +95,30 @@ bool TexoImporterPlain::TrueEnd() bool TexoImporterPlain::TruePut(char c) { switch (state) { - case text: - Text(c); - break; - case newline: - Newline(c); - break; - case paragraph: - Paragraph(c); - break; - case quote_pre: - QuotePre(c); - break; - case quote: - Quote(c); - break; - case quote_newline: - QuoteNewline(c); - break; - case rule: - Rule(c); - break; - case paragraph_rule: - ParagraphRule(c); - break; - } - return ok; + case text: return Text(c); + case newline: return Newline(c); + case paragraph: return Paragraph(c); + case quote_pre: return QuotePre(c); + case quote: return Quote(c); + case quote_newline: return QuoteNewline(c); + case rule: return Rule(c); + case paragraph_rule: return ParagraphRule(c); + } + return ok; // Impossible } -void TexoImporterPlain::Text(char c) +bool TexoImporterPlain::Text(char c) { - switch (c) { - case '\n': + if (c == '\n') { state = newline; - break; - default: + } else { ok = producer.Put(c); - break; } + return ok; } -void TexoImporterPlain::Newline(char c) +bool TexoImporterPlain::Newline(char c) { if (c == '\n') { state = paragraph; @@ -151,9 +132,10 @@ void TexoImporterPlain::Newline(char c) Text(c); } } + return ok; } -void TexoImporterPlain::Paragraph(char c) +bool TexoImporterPlain::Paragraph(char c) { if (c == '>') { ok = producer.Quote(); @@ -168,29 +150,29 @@ void TexoImporterPlain::Paragraph(char c) Text(c); } } + return ok; } -void TexoImporterPlain::QuotePre(char c) +bool TexoImporterPlain::QuotePre(char c) { if (c != ' ') { state = quote; Quote(c); } + return ok; } -void TexoImporterPlain::Quote(char c) +bool TexoImporterPlain::Quote(char c) { - switch (c) { - case '\n': + if (c == '\n') { state = quote_newline; - break; - default: + } else { ok = producer.Put(c); - break; } + return ok; } -void TexoImporterPlain::QuoteNewline(char c) +bool TexoImporterPlain::QuoteNewline(char c) { if (c == '>') { ok = producer.Put('\n'); @@ -207,9 +189,10 @@ void TexoImporterPlain::QuoteNewline(char c) Text(c); } } + return ok; } -void TexoImporterPlain::Rule(char c) +bool TexoImporterPlain::Rule(char c) { if (c == '\n') { ok = producer.PutHorizontalRule(); @@ -226,14 +209,13 @@ void TexoImporterPlain::Rule(char c) Text(c); } } + return ok; } -void TexoImporterPlain::ParagraphRule(char c) +bool TexoImporterPlain::ParagraphRule(char c) { if (c == '\n') { ok = producer.Paragraph(); } - if (ok) { - Rule(c); - } + return ok && Rule(c); } diff --git a/src/plain.hpp b/src/plain.hpp index adc5bd8..8863c4e 100644 --- a/src/plain.hpp +++ b/src/plain.hpp @@ -11,7 +11,7 @@ */ class TexoProducerPlain: public TexoProducer { public: - TexoProducerPlain(TexoExporter & exporter); + TexoProducerPlain(TexoExporter &exporter); bool End(); @@ -34,7 +34,7 @@ private: */ class TexoImporterPlain: public TexoImporter { public: - TexoImporterPlain(TexoProducer & producer); + TexoImporterPlain(TexoProducer &producer); protected: @@ -56,14 +56,14 @@ private: paragraph_rule } state; - void Text(char c); - void Newline(char c); - void Paragraph(char c); - void QuotePre(char c); - void Quote(char c); - void QuoteNewline(char c); - void Rule(char c); - void ParagraphRule(char c); + bool Text(char c); + bool Newline(char c); + bool Paragraph(char c); + bool QuotePre(char c); + bool Quote(char c); + bool QuoteNewline(char c); + bool Rule(char c); + bool ParagraphRule(char c); int dash_count; }; diff --git a/src/producer.cpp b/src/producer.cpp index 5d08369..7a12694 100644 --- a/src/producer.cpp +++ b/src/producer.cpp @@ -4,7 +4,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Producer */ -TexoProducer::TexoProducer(TexoExporter & exporter): exporter(exporter) +TexoProducer::TexoProducer(TexoExporter &exporter): exporter(exporter) {} // Just save exporter for future use. @@ -23,64 +23,26 @@ bool TexoProducer::Put(char c) // Block Signal Handlers // By default, nothing has to be done, and no error produced, // except for paragraphs, which must be implemented. -bool TexoProducer::Code() -{ - return true; -} - -bool TexoProducer::Header(int level) -{ - return true; -} - -bool TexoProducer::Quote() -{ - return true; -} +bool TexoProducer::Header(int level) { return true; } +bool TexoProducer::Code() { return true; } +bool TexoProducer::Quote() { return true; } // Modificator Signal Handlers // By default, nothing has to be done, and no error produced. -bool TexoProducer::Bold() -{ - return true; -} - -bool TexoProducer::Italic() -{ - return true; -} +bool TexoProducer::Bold() { return true; } +bool TexoProducer::Italic() { return true; } +bool TexoProducer::Mono() { return true; } +bool TexoProducer::Strike() { return true; } +bool TexoProducer::Underline() { return true; } -bool TexoProducer::Mono() -{ - return true; -} - -bool TexoProducer::Strike() -{ - return true; -} - -bool TexoProducer::Underline() -{ - return true; -} +bool TexoProducer::Link(const char *link, const char *title) { return true; } +bool TexoProducer::Link() { return true; } -bool TexoProducer::Link(const ScriptVariable & link, - const ScriptVariable & title) -{ - return true; -} -bool TexoProducer::Link() -{ - return true; -} - - -bool TexoProducer::PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title) +bool TexoProducer::PutImage(const char *src, + const char *alt, + const char *title) { return true; } @@ -94,7 +56,7 @@ bool TexoProducer::PutHorizontalRule() /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo Strict Producer */ -TexoProducerStrict::TexoProducerStrict(TexoExporter & exporter): +TexoProducerStrict::TexoProducerStrict(TexoExporter &exporter): TexoProducer(exporter), block(block_none), opened(0) {} @@ -113,22 +75,22 @@ bool TexoProducerStrict::Put(char c) } -bool TexoProducerStrict::Code() +bool TexoProducerStrict::Header(int level) { if (CloseBlock()) { - block = code; - return StartCode(); + block = header; + header_level = level; + return StartHeader(level); } else { return false; } } -bool TexoProducerStrict::Header(int level) +bool TexoProducerStrict::Code() { if (CloseBlock()) { - block = header; - header_level = level; - return StartHeader(level); + block = code; + return StartCode(); } else { return false; } @@ -155,33 +117,13 @@ bool TexoProducerStrict::Quote() } -bool TexoProducerStrict::Bold() -{ - return SwitchMod(bold); -} +bool TexoProducerStrict::Bold() { return SwitchMod(bold); } +bool TexoProducerStrict::Italic() { return SwitchMod(italic); } +bool TexoProducerStrict::Mono() { return SwitchMod(mono); } +bool TexoProducerStrict::Strike() { return SwitchMod(strike); } +bool TexoProducerStrict::Underline() { return SwitchMod(underline); } -bool TexoProducerStrict::Italic() -{ - return SwitchMod(italic); -} - -bool TexoProducerStrict::Mono() -{ - return SwitchMod(mono); -} - -bool TexoProducerStrict::Strike() -{ - return SwitchMod(strike); -} - -bool TexoProducerStrict::Underline() -{ - return SwitchMod(underline); -} - -bool TexoProducerStrict::Link(const ScriptVariable & path, - const ScriptVariable & title) +bool TexoProducerStrict::Link(const char *path, const char *title) { if (IsOpened(link)) { int closed = CloseMods(link); @@ -210,9 +152,10 @@ bool TexoProducerStrict::Link() return true; } -bool TexoProducerStrict::PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title) + +bool TexoProducerStrict::PutImage(const char *src, + const char *alt, + const char *title) { return Start() && TruePutImage(src, alt, title); } @@ -235,23 +178,17 @@ bool TexoProducerStrict::Start() bool TexoProducerStrict::CloseBlock() { - if (CloseMods()) { - switch (block) { - case block_none: - return true; - case code: - return CloseCode(); - case header: - return CloseHeader(header_level); - case paragraph: - return CloseParagraph(); - case quote: - return CloseQuote(); - } - return true; // Inpossible, since all cases are in switch. - } else { + if (!CloseMods()) { return false; } + switch (block) { + case block_none: return true; + case code: return CloseCode(); + case header: return CloseHeader(header_level); + case paragraph: return CloseParagraph(); + case quote: return CloseQuote(); + } + return true; // Impossible, since all cases are in switch. } bool TexoProducerStrict::SwitchMod(Mod mod) @@ -273,39 +210,27 @@ bool TexoProducerStrict::OpenMod(Mod mod) mods[opened] = mod; ++opened; switch (mod) { - case bold: - return StartBold(); - case italic: - return StartItalic(); - case link: - return StartLink(*link_link, *link_title); - case mono: - return StartMono(); - case strike: - return StartStrike(); - case underline: - return StartUnderline(); + case bold: return StartBold(); + case italic: return StartItalic(); + case link: return StartLink(*link_link, *link_title); + case mono: return StartMono(); + case strike: return StartStrike(); + case underline: return StartUnderline(); } - return true; // Inpossible, since all cases are in switch. + return true; // Impossible, since all cases are in switch. } bool TexoProducerStrict::CloseMod(Mod mod) { switch (mod) { - case bold: - return CloseBold(); - case italic: - return CloseItalic(); - case link: - return CloseLink(*link_link, *link_title); - case mono: - return CloseMono(); - case strike: - return CloseStrike(); - case underline: - return CloseUnderline(); + case bold: return CloseBold(); + case italic: return CloseItalic(); + case link: return CloseLink(*link_link, *link_title); + case mono: return CloseMono(); + case strike: return CloseStrike(); + case underline: return CloseUnderline(); } - return true; // Inpossible, since all cases are in switch. + return true; // Impossible, since all cases are in switch. } int TexoProducerStrict::CloseMods(Mod last) @@ -313,13 +238,12 @@ int TexoProducerStrict::CloseMods(Mod last) const int old = opened; while (opened) { Mod top = mods[--opened]; - if (CloseMod(top)) { - if (top == last) { - return old - opened; - } - } else { + if (!CloseMod(top)) { return -1; } + if (top == last) { + return old - opened; + } } return old - opened; } diff --git a/src/producer.hpp b/src/producer.hpp index ff79210..499a7f3 100644 --- a/src/producer.hpp +++ b/src/producer.hpp @@ -10,7 +10,7 @@ */ class TexoProducer { public: - TexoProducer(TexoExporter & exporter); // Setup exporter used for output. + TexoProducer(TexoExporter &exporter); // Setup exporter used for output. virtual bool End(); // Notify producer, what input is ended. @@ -21,8 +21,8 @@ public: // Block Signal Handlers // Only one can be opened at one moment. - virtual bool Code(); virtual bool Header(int level); + virtual bool Code(); virtual bool Paragraph() = 0; virtual bool Quote(); @@ -36,19 +36,16 @@ public: virtual bool Underline(); // Switch link to given or switch it off, if no one is given. - virtual bool Link(const ScriptVariable & link, - const ScriptVariable & title); + virtual bool Link(const char *link, const char *title); virtual bool Link(); - virtual bool PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title); + virtual bool PutImage(const char *src, const char *alt, const char *title); virtual bool PutHorizontalRule(); protected: - TexoExporter & exporter; + TexoExporter &exporter; }; @@ -69,8 +66,8 @@ public: bool Put(char c); - bool Code(); bool Header(int level); + bool Code(); bool Paragraph(); bool Quote(); @@ -81,51 +78,55 @@ public: bool Strike(); bool Underline(); - bool Link(const ScriptVariable & path, const ScriptVariable & title); + bool Link(const char *path, const char *title); bool Link(); - bool PutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title); + bool PutImage(const char *src, const char *alt, const char *title); + bool PutHorizontalRule(); protected: virtual bool TruePut(char c) = 0; - virtual bool StartCode() = 0; + virtual bool StartHeader(int level) = 0; - virtual bool StartParagraph() = 0; - virtual bool StartQuote() = 0; + virtual bool CloseHeader(int level) = 0; + virtual bool StartCode() = 0; virtual bool CloseCode() = 0; - virtual bool CloseHeader(int level) = 0; + + virtual bool StartParagraph() = 0; virtual bool CloseParagraph() = 0; + + virtual bool StartQuote() = 0; virtual bool CloseQuote() = 0; virtual bool StartBold() = 0; - virtual bool StartItalic() = 0; - virtual bool StartMono() = 0; - virtual bool StartStrike() = 0; - virtual bool StartUnderline() = 0; - virtual bool CloseBold() = 0; + + virtual bool StartItalic() = 0; virtual bool CloseItalic() = 0; + + virtual bool StartMono() = 0; virtual bool CloseMono() = 0; + + virtual bool StartStrike() = 0; virtual bool CloseStrike() = 0; + + virtual bool StartUnderline() = 0; virtual bool CloseUnderline() = 0; - virtual bool StartLink(const ScriptVariable & link, - const ScriptVariable & title) = 0; - virtual bool CloseLink(const ScriptVariable & link, - const ScriptVariable & title) = 0; + virtual bool StartLink(const char *link, const char *title) = 0; + virtual bool CloseLink(const char *link, const char *title) = 0; + + virtual bool TruePutImage(const char *src, + const char *alt, + const char *title) = 0; - virtual bool TruePutImage(const ScriptVariable & src, - const ScriptVariable & alt, - const ScriptVariable & title) = 0; - virtual bool TruePutHorizontalRule() = 0; + virtual bool TruePutHorizontalRule() = 0; private: @@ -137,8 +138,8 @@ private: enum Mod { bold, italic, link, mono, strike, underline } mods[6]; int opened; - const ScriptVariable * link_link; - const ScriptVariable * link_title; + const char **link_link; + const char **link_title; bool Start(); // Open paragraph if no block is opened. diff --git a/src/script.cpp b/src/script.cpp index 2892723..cf87bc2 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1,21 +1,47 @@ #include "script.hpp" +#include +#include + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo String Exporter */ -TexoExporterString::TexoExporterString(ScriptVariable & str): str(str) -{} +TexoString::TexoString(char *&str): str(str) +{ + str = (char *)malloc(1); + *str = 0; + len = 0; + size = 1; +} -bool TexoExporterString::Put(char c) +bool TexoString::Put(char c) { - str += c; + if (len + 1 == size) { + size *= 2; + str = (char *)realloc(str, size); + } + if (!str) { + return false; + } + str[len] = c; + ++len; + str[len] = 0; return true; } -bool TexoExporterString::Put(const ScriptVariable & addon) +bool TexoString::Put(const char *addon) { - str += addon; + int alen = strlen(addon); + if (len + alen >= size) { + size = len + alen + 1; + str = (char *)realloc(str, size); + } + if (!str) { + return false; + } + memcpy(str + len, addon, alen + 1); + len += alen; return true; } diff --git a/src/script.hpp b/src/script.hpp index 029047d..ab2221b 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -3,22 +3,24 @@ #include "exporter.hpp" + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Texo String Exporter - * Simple utility class to make output into Script Variable, - * which can be easily transformed into C string via c_str method. + * Simple utility class to make output into C string. */ -class TexoExporterString: public TexoExporter { +class TexoString: public TexoExporter { public: - TexoExporterString(ScriptVariable & str); + TexoString(char *&str); bool Put(char c); - bool Put(const ScriptVariable & addon); + bool Put(const char *addon); private: - ScriptVariable & str; + char *&str; + int len; + int size; }; -- cgit v1.2.3