The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osisvariants.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * osisvariants.cpp - SWFilter descendant to hide or show textual variants
4  * in an OSIS module
5  *
6  * $Id: osisvariants.cpp 2980 2013-09-14 21:51:47Z scribe $
7  *
8  * Copyright 2006-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 <osisvariants.h>
26 #include <utilstr.h>
27 #include <utilxml.h>
28 
29 
31 
32 namespace {
33 
34  static const char oName[] = "Textual Variants";
35  static const char oTip[] = "Switch between Textual Variants modes";
36  static const char *choices[4] = { "Primary Reading", "Secondary Reading", "All Readings", "" };
37 
38  static const StringList *oValues() {
39  static const StringList oVals(&choices[0], &choices[3]);
40  return &oVals;
41  }
42 }
43 
44 
46 }
47 
48 
50 }
51 
52 
53 char OSISVariants::processText(SWBuf &text, const SWKey *key, const SWModule *module)
54 {
55 
56  int option = 0;
57  if (optionValue == choices[0]) option = 0;
58  else if (optionValue == choices[1]) option = 1;
59  else option = 2;
60 
61  if (option == 0 || option == 1) { //we want primary or variant only
62  bool intoken = false;
63  bool hide = false;
64  bool invar = false;
65 
66  SWBuf token;
67  SWBuf orig = text;
68  const char *from = orig.c_str();
69  XMLTag tag;
70 
71  //we use a fixed comparision string to make sure the loop is as fast as the original two blocks with almost the same code
72  const char* variantChoice = (option == 0) ? "x-2" : "x-1";
73 
74  for (text = ""; *from; from++) {
75  if (*from == '<') {
76  intoken = true;
77  token = "";
78  continue;
79  }
80  else if (*from == '>') { // process tokens
81  intoken = false;
82 
83  if (!strncmp(token.c_str(), "seg", 3)) {
84  tag = token;
85 
86  if (tag.getAttribute("type") && !strcmp("x-variant", tag.getAttribute("type")) && tag.getAttribute("subType") && !strcmp(variantChoice, tag.getAttribute("subType"))) {
87  invar = true;
88  hide = true;
89  continue;
90  }
91  }
92  if (!strncmp(token.c_str(), "/seg", 4)) {
93  hide = false;
94  if (invar) {
95  invar = false;
96  continue;
97  }
98  }
99  if (!hide) {
100  text += '<';
101  text.append(token);
102  text += '>';
103  }
104 
105  continue;
106  }
107  if (intoken) {
108  token += *from;
109  }
110  else if (!hide) {
111  text += *from;
112  }
113  }
114 
115  }
116 
117  return 0;
118 }
119 
120 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
Definition: utilxml.h:38
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)
virtual ~OSISVariants()
static const char * choices[4]
const char * getAttribute(const char *attribName, int partNum=-1, char partSplit= '|') const
Definition: utilxml.cpp:230
static const char oTip[]
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77