The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utf8html.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utf8html.cpp - SWFilter descendant to convert a UTF-8 stream to
4  * HTML escapes
5  *
6  * $Id: utf8html.cpp 3081 2014-03-05 19:52:08Z chrislit $
7  *
8  * Copyright 2001-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 <stdio.h>
25 #include <utf8html.h>
26 #include <swbuf.h>
27 
28 
30 
31 
33 }
34 
35 
36 char UTF8HTML::processText(SWBuf &text, const SWKey *key, const SWModule *module)
37 {
38  unsigned char *from;
39  char digit[10];
40  unsigned long ch;
41  if ((unsigned long)key < 2) // hack, we're en(1)/de(0)ciphering
42  return (char)-1;
43 
44  SWBuf orig = text;
45  from = (unsigned char *)orig.c_str();
46 
47  // -------------------------------
48  for (text = ""; *from; from++) {
49  ch = 0;
50  if ((*from & 128) != 128) {
51 // if (*from != ' ')
52  text += *from;
53  continue;
54  }
55  if ((*from & 128) && ((*from & 64) != 64)) {
56  // error
57  *from = 'x';
58  continue;
59  }
60  *from <<= 1;
61  int subsequent;
62  for (subsequent = 1; (*from & 128); subsequent++) {
63  *from <<= 1;
64  from[subsequent] &= 63;
65  ch <<= 6;
66  ch |= from[subsequent];
67  }
68  subsequent--;
69  *from <<=1;
70  char significantFirstBits = 8 - (2+subsequent);
71 
72  ch |= (((short)*from) << (((6*subsequent)+significantFirstBits)-8));
73  from += subsequent;
74  text += '&';
75  text += '#';
76  sprintf(digit, "%ld", ch);
77  for (char *dig = digit; *dig; dig++)
78  text += *dig;
79  text += ';';
80  }
81  return 0;
82 }
83 
84 
86 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
UTF8HTML()
Definition: utf8html.cpp:32
const char * c_str() const
Definition: swbuf.h:158
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
Definition: utf8html.cpp:36
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77