The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
encfiltmgr.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * encfiltmgr.cpp - implementaion of class EncodingFilterMgr, subclass of
4  * SWFilterMgr, used to transcode all module text to a
5  * requested encoding
6  *
7  * $Id: encfiltmgr.cpp 3781 2020-08-23 10:25:31Z scribe $
8  *
9  * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
10  * CrossWire Bible Society
11  * P. O. Box 2528
12  * Tempe, AZ 85280-2528
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  */
24 
25 #include <encfiltmgr.h>
26 #include <utilstr.h>
27 
28 #include <scsuutf8.h>
29 #include <latin1utf8.h>
30 #include <utf16utf8.h>
31 
32 #include <unicodertf.h>
33 #include <utf8latin1.h>
34 #include <utf8utf16.h>
35 #include <utf8html.h>
36 
37 #ifdef _ICU_
38 #include <utf8scsu.h>
39 #endif
40 
41 #include <swmodule.h>
42 
43 #include <swmgr.h>
44 
45 
47 
48 
49 /******************************************************************************
50  * EncodingFilterMgr Constructor - initializes instance of EncodingFilterMgr
51  *
52  * ENT:
53  * enc - Encoding format to emit
54  */
56  : SWFilterMgr() {
57 
58  scsuutf8 = new SCSUUTF8();
59  latin1utf8 = new Latin1UTF8();
60  utf16utf8 = new UTF16UTF8();
61 
62  encoding = enc;
63 
64  switch (encoding) {
65  case ENC_LATIN1: targetenc = new UTF8Latin1(); break;
66  case ENC_UTF16: targetenc = new UTF8UTF16(); break;
67  case ENC_RTF: targetenc = new UnicodeRTF(); break;
68  case ENC_HTML: targetenc = new UTF8HTML(); break;
69 #ifdef _ICU_
70  case ENC_SCSU: targetenc = new UTF8SCSU(); break;
71 #endif
72  default: // i.e. case ENC_UTF8
73  targetenc = NULL;
74  }
75 }
76 
77 
78 /******************************************************************************
79  * EncodingFilterMgr Destructor - Cleans up instance of EncodingFilterMgr
80  */
82  delete scsuutf8;
83  delete latin1utf8;
84  delete utf16utf8;
85  delete targetenc;
86 }
87 
88 
90 
91  ConfigEntMap::iterator entry;
92 
93  SWBuf encoding = ((entry = section.find("Encoding")) != section.end()) ? (*entry).second : (SWBuf)"";
94  if (!encoding.length() || !stricmp(encoding.c_str(), "Latin-1")) {
95  module->addRawFilter(latin1utf8);
96  }
97  else if (!stricmp(encoding.c_str(), "SCSU")) {
98  module->addRawFilter(scsuutf8);
99  }
100  else if (!stricmp(encoding.c_str(), "UTF-16")) {
101  module->addRawFilter(utf16utf8);
102  }
103 }
104 
105 
107  if (targetenc)
108  module->addEncodingFilter(targetenc);
109 }
110 
111 
112 /******************************************************************************
113  * EncodingFilterMgr::Encoding - sets/gets encoding
114  *
115  * ENT: enc - new encoding or 0 to simply get the current encoding
116  *
117  * RET: encoding
118  */
120  if (enc && enc != encoding) {
121  encoding = enc;
122  SWFilter *oldfilter = targetenc;
123 
124  switch (encoding) {
125  case ENC_LATIN1: targetenc = new UTF8Latin1(); break;
126  case ENC_UTF16: targetenc = new UTF8UTF16(); break;
127  case ENC_RTF: targetenc = new UnicodeRTF(); break;
128  case ENC_HTML: targetenc = new UTF8HTML(); break;
129 #ifdef _ICU_
130  case ENC_SCSU: targetenc = new UTF8SCSU(); break;
131 #endif
132  default: // i.e. case ENC_UTF8
133  targetenc = NULL;
134  }
135 
136  ModMap::const_iterator module;
137 
138  if (oldfilter != targetenc) {
139  if (oldfilter) {
140  if (!targetenc) {
141  for (module = getParentMgr()->Modules.begin(); module != getParentMgr()->Modules.end(); module++)
142  module->second->removeRenderFilter(oldfilter);
143  }
144  else {
145  for (module = getParentMgr()->Modules.begin(); module != getParentMgr()->Modules.end(); module++)
146  module->second->replaceRenderFilter(oldfilter, targetenc);
147  }
148  delete oldfilter;
149  }
150  else if (targetenc) {
151  for (module = getParentMgr()->Modules.begin(); module != getParentMgr()->Modules.end(); module++)
152  module->second->addRenderFilter(targetenc);
153  }
154  }
155  }
156 }
157 
158 
SWFilter * utf16utf8
Definition: encfiltmgr.h:45
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
virtual SWModule & addEncodingFilter(SWFilter *newFilter)
Definition: swmodule.h:627
unsigned long length() const
Definition: swbuf.h:197
ModMap Modules
Definition: swmgr.h:322
SWText * module
Definition: osis2mod.cpp:105
int stricmp(const char *s1, const char *s2)
Definition: utilstr.cpp:194
SWFilter * targetenc
Definition: encfiltmgr.h:46
return NULL
Definition: regex.c:7953
const char * c_str() const
Definition: swbuf.h:158
virtual void addEncodingFilters(SWModule *module, ConfigEntMap &section)
Definition: encfiltmgr.cpp:106
virtual SWModule & removeRenderFilter(SWFilter *oldFilter)
Definition: swmodule.h:585
SWFilter * latin1utf8
Definition: encfiltmgr.h:43
virtual SWModule & addRawFilter(SWFilter *newFilter)
Definition: swmodule.h:694
virtual SWMgr * getParentMgr()
Definition: swfiltermgr.cpp:43
SWORD_NAMESPACE_START typedef multimapwithdefault< SWBuf, SWBuf, std::less< SWBuf > > ConfigEntMap
Definition: swconfig.h:35
void setEncoding(char enc)
Definition: encfiltmgr.cpp:119
SWFilter * scsuutf8
Definition: encfiltmgr.h:44
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual void addRawFilters(SWModule *module, ConfigEntMap &section)
Definition: encfiltmgr.cpp:89
virtual SWModule & replaceRenderFilter(SWFilter *oldFilter, SWFilter *newFilter)
Definition: swmodule.h:599
EncodingFilterMgr(char encoding=ENC_UTF8)
Definition: encfiltmgr.cpp:55