diff options
author | Roberto C. Sanchez <roberto@connexer.com> | 2014-03-29 10:53:49 -0400 |
---|---|---|
committer | Roberto C. Sanchez <roberto@connexer.com> | 2014-03-29 10:53:49 -0400 |
commit | 8c8aa6b07e595cfac56838b5964ab3e96051f1b2 (patch) | |
tree | da38e2c1979148dbd3b0c7b87f930746f5ba7f44 /src/modules/filters/thmlvariants.cpp | |
parent | 8d3fc864d094eeadc721f8e93436b37a5fab173e (diff) |
Imported Upstream version 1.5.7
Diffstat (limited to 'src/modules/filters/thmlvariants.cpp')
-rw-r--r-- | src/modules/filters/thmlvariants.cpp | 133 |
1 files changed, 31 insertions, 102 deletions
diff --git a/src/modules/filters/thmlvariants.cpp b/src/modules/filters/thmlvariants.cpp index fda0950..b8ab653 100644 --- a/src/modules/filters/thmlvariants.cpp +++ b/src/modules/filters/thmlvariants.cpp @@ -1,18 +1,18 @@ /****************************************************************************** * - * thmlvariants - SWFilter decendant to hide or show textual variants + * thmlvariants - SWFilter descendant to hide or show textual variants * in a ThML module. */ #include <stdlib.h> -#include <string.h> #include <thmlvariants.h> #ifndef __GNUC__ #else #include <unixstr.h> #endif +SWORD_NAMESPACE_START const char ThMLVariants::primary[] = "Primary Reading"; const char ThMLVariants::secondary[] = "Secondary Reading"; @@ -35,7 +35,9 @@ ThMLVariants::~ThMLVariants() { void ThMLVariants::setOptionValue(const char *ival) { - option = (!stricmp(ival, primary)); + if (!stricmp(ival, primary)) option = 0; + else if (!stricmp(ival, secondary)) option = 1; + else option = 2; } const char *ThMLVariants::getOptionValue() @@ -51,128 +53,54 @@ const char *ThMLVariants::getOptionValue() } } -char ThMLVariants::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module) +char ThMLVariants::processText(SWBuf &text, const SWKey *key, const SWModule *module) { - if (option == 0) { //we want primary only - char *to, *from, token[2048]; // cheese. Fix. - int tokpos = 0; + if ( option == 0 || option == 1) { //we want primary or variant only bool intoken = false; - int len; bool hide = false; - - len = strlen(text) + 1; // shift string to right of buffer - if (len < maxlen) { - memmove(&text[maxlen - len], text, len); - from = &text[maxlen - len]; - } - else from = text; - // ------------------------------- + SWBuf token; + SWBuf orig = text; + const char *from = orig.c_str(); - for (to = text; *from; from++) { + //we use a fixed comparision string to make sure the loop is as fast as the original two blocks with almost the same code + const char* variantCompareString = (option == 0) ? "div type=\"variant\" class=\"1\"" : "div type=\"variant\" class=\"2\""; + + for (text = ""; *from; from++) { if (*from == '<') { intoken = true; - tokpos = 0; - token[0] = 0; - token[1] = 0; - token[2] = 0; + token = ""; continue; } - if (*from == '>') { // process tokens + else if (*from == '>') { // process tokens intoken = false; - if (!strncmp(token, "div type=\"variant\"", 19)) { - hide = true; - continue; - } - else if (!strncmp(token, "/div", 4)) { - hide = false; - continue; + + if ( !strncmp(token.c_str(), variantCompareString, 28)) { //only one of the variants, length of the two strings is 28 in both cases + hide = true; + continue; } - - // if not a footnote token, keep token in text - if (!hide) { - *to++ = '<'; - for (char *tok = token; *tok; tok++) - *to++ = *tok; - *to++ = '>'; - } - continue; - } - if (intoken) { - if (tokpos < 2045) - token[tokpos++] = *from; - token[tokpos+2] = 0; - } - else { if (!hide) { - *to++ = *from; - } - } - } - *to++ = 0; - *to = 0; - - } - else if (option == 1) { //we want variant only - char *to, *from, token[2048]; // cheese. Fix. - int tokpos = 0; - bool intoken = false; - int len; - bool hide = false; - - len = strlen(text) + 1; // shift string to right of buffer - if (len < maxlen) { - memmove(&text[maxlen - len], text, len); - from = &text[maxlen - len]; - } - else from = text; - - // ------------------------------- - - for (to = text; *from; from++) { - if (*from == '<') { - intoken = true; - tokpos = 0; - token[0] = 0; - token[1] = 0; - token[2] = 0; - continue; - } - if (*from == '>') { // process tokens - intoken = false; - if (!strncmp(token, "div type=\"primary\"", 19)) { - hide = true; - continue; + text += '<'; + text.append(token); + text += '>'; } - else if (!strncmp(token, "/div", 4)) { - hide = false; - continue; + if (!strncmp(token.c_str(), "/div", 4)) { + hide = false; + continue; } - // if not a footnote token, keep token in text - if (!hide) { - *to++ = '<'; - for (char *tok = token; *tok; tok++) - *to++ = *tok; - *to++ = '>'; - } continue; } if (intoken) { - if (tokpos < 2045) - token[tokpos++] = *from; - token[tokpos+2] = 0; + token += *from; } - else { - if (!hide) { - *to++ = *from; - } + else if (!hide) { + text += *from; } } - *to++ = 0; - *to = 0; } + return 0; } @@ -181,3 +109,4 @@ char ThMLVariants::ProcessText(char *text, int maxlen, const SWKey *key, const S +SWORD_NAMESPACE_END |