The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utf8latin1.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utf8latin1.cpp - SWFilter descendant to convert UTF-8 to Latin-1
4  *
5  * $Id: utf8latin1.cpp 3081 2014-03-05 19:52:08Z chrislit $
6  *
7  * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <utf8latin1.h>
24 #include <swbuf.h>
25 
26 
28 
29 
30 UTF8Latin1::UTF8Latin1(char rchar) : replacementChar(rchar) {
31 }
32 
33 
34 char UTF8Latin1::processText(SWBuf &text, const SWKey *key, const SWModule *module)
35 {
36  unsigned char *from;
37 
38  unsigned long uchar;
39  unsigned char significantFirstBits, subsequent;
40 
41  if ((unsigned long)key < 2) {// hack, we're en(1)/de(0)ciphering
42  return (char)-1;
43  }
44 
45  SWBuf orig = text;
46  from = (unsigned char*)orig.c_str();
47 
48 
49  // -------------------------------
50 
51  for (text = ""; *from; from++) {
52  uchar = 0;
53  if ((*from & 128) != 128) {
54  // if (*from != ' ')
55  uchar = *from;
56  }
57  else if ((*from & 128) && ((*from & 64) != 64)) {
58  // error, do nothing
59  continue;
60  }
61  else {
62  *from <<= 1;
63  for (subsequent = 1; (*from & 128); subsequent++) {
64  *from <<= 1;
65  from[subsequent] &= 63;
66  uchar <<= 6;
67  uchar |= from[subsequent];
68  }
69  subsequent--;
70  *from <<=1;
71  significantFirstBits = 8 - (2+subsequent);
72 
73  uchar |= (((short)*from) << (((6*subsequent)+significantFirstBits)-8));
74  from += subsequent;
75  }
76 
77  if (uchar < 0xff) {
78  text += (unsigned char)uchar;
79  }
80  else {
81  text += replacementChar;
82  }
83  }
84  return 0;
85 }
86 
87 
89 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
const char * c_str() const
Definition: swbuf.h:158
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
Definition: utf8latin1.cpp:34
UTF8Latin1(char rchar= '?')
Definition: utf8latin1.cpp:30
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
char replacementChar
Definition: utf8latin1.h:37