The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thmlvariants.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * thmlvariants.cpp - SWFilter descendant to hide or show textual variants
4  * in a ThML module
5  *
6  * $Id: thmlvariants.cpp 2980 2013-09-14 21:51:47Z scribe $
7  *
8  * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <stdlib.h>
25 #include <thmlvariants.h>
26 #include <utilstr.h>
27 
28 
30 
31 namespace {
32 
33  static const char oName[] = "Textual Variants";
34  static const char oTip[] = "Switch between Textual Variants modes";
35  static const char *choices[4] = { "Primary Reading", "Secondary Reading", "All Readings", "" };
36 
37  static const StringList *oValues() {
38  static const StringList oVals(&choices[0], &choices[3]);
39  return &oVals;
40  }
41 }
42 
43 
45 }
46 
47 
49 }
50 
51 
52 char ThMLVariants::processText(SWBuf &text, const SWKey *key, const SWModule *module)
53 {
54 
55  int option = 0;
56  if (optionValue == choices[0]) option = 0;
57  else if (optionValue == choices[1]) option = 1;
58  else option = 2;
59 
60  if (option == 0 || option == 1) { //we want primary or variant only
61  bool intoken = false;
62  bool hide = false;
63  bool invar = false;
64 
65  SWBuf token;
66  SWBuf orig = text;
67  const char *from = orig.c_str();
68 
69  //we use a fixed comparision string to make sure the loop is as fast as the original two blocks with almost the same code
70  const char* variantCompareString = (option == 0) ? "div type=\"variant\" class=\"1\"" : "div type=\"variant\" class=\"2\"";
71 
72  for (text = ""; *from; from++) {
73  if (*from == '<') {
74  intoken = true;
75  token = "";
76  continue;
77  }
78  else if (*from == '>') { // process tokens
79  intoken = false;
80 
81  if ( !strncmp(token.c_str(), variantCompareString, 28)) { //only one of the variants, length of the two strings is 28 in both cases
82  invar = true;
83  hide = true;
84  continue;
85  }
86  if (!strncmp(token.c_str(), "div type=\"variant\"", 18)) {
87  invar = true;
88  continue;
89  }
90  if (!strncmp(token.c_str(), "/div", 4)) {
91  hide = false;
92  if (invar) {
93  invar = false;
94  continue;
95  }
96  }
97  if (!hide) {
98  text += '<';
99  text.append(token);
100  text += '>';
101  }
102 
103  continue;
104  }
105  if (intoken) {
106  token += *from;
107  }
108  else if (!hide) {
109  text += *from;
110  }
111  }
112 
113  }
114 
115  return 0;
116 }
117 
118 
119 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
static const StringList * oValues()
SWBuf optionValue
Definition: swoptfilter.h:50
const char * c_str() const
Definition: swbuf.h:158
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
static const char oName[]
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
static const char * choices[4]
static const char oTip[]
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
virtual ~ThMLVariants()