The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utf16utf8.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utf16utf8.cpp - SWFilter descendant to convert UTF-16 to UTF-8
4  *
5  * $Id: utf16utf8.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 
24 #include <utf16utf8.h>
25 #include <swbuf.h>
26 
27 
29 
30 
32 }
33 
34 
35 char UTF16UTF8::processText(SWBuf &text, const SWKey *key, const SWModule *module)
36 {
37  unsigned short *from;
38 
39  int len;
40  unsigned long uchar;
41  unsigned short schar;
42  len = 0;
43  from = (unsigned short*) text.c_str();
44  while (*from) {
45  len += 2;
46  from++;
47  }
48 
49  SWBuf orig = text;
50  from = (unsigned short*)orig.c_str();
51 
52 
53  // -------------------------------
54 
55  for (text = ""; *from; from++) {
56  uchar = 0;
57 
58  if (*from < 0xD800 || *from > 0xDFFF) {
59  uchar = *from;
60  }
61  else if (*from >= 0xD800 && *from <= 0xDBFF) {
62  uchar = *from;
63  schar = *(from+1);
64  if (uchar < 0xDC00 || uchar > 0xDFFF) {
65  //error, do nothing
66  continue;
67  }
68  uchar &= 0x03ff;
69  schar &= 0x03ff;
70  uchar <<= 10;
71  uchar |= schar;
72  uchar += 0x10000;
73  from++;
74  }
75  else {
76  //error, do nothing
77  continue;
78  }
79 
80  if (uchar < 0x80) {
81  text += uchar;
82  }
83  else if (uchar < 0x800) {
84  text += 0xc0 | (uchar >> 6);
85  text += 0x80 | (uchar & 0x3f);
86  }
87  else if (uchar < 0x10000) {
88  text += 0xe0 | (uchar >> 12);
89  text += 0x80 | ((uchar >> 6) & 0x3f);
90  text += 0x80 | (uchar & 0x3f);
91  }
92  else if (uchar < 0x200000) {
93  text += 0xF0 | (uchar >> 18);
94  text += 0x80 | ((uchar >> 12) & 0x3F);
95  text += 0x80 | ((uchar >> 6) & 0x3F);
96  text += 0x80 | (uchar & 0x3F);
97  }
98  }
99 
100  return 0;
101 }
102 
103 
#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: utf16utf8.cpp:35
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77