The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utf8utf16.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utf8utf16.cpp - SWFilter descendant to convert UTF-8 to UTF-16
4  *
5  * $Id: utf8utf16.cpp 3749 2020-07-06 23:51:56Z scribe $
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 <utf8utf16.h>
25 #include <utilstr.h>
26 #include <swbuf.h>
27 
28 
30 
31 
33 }
34 
35 
36 char UTF8UTF16::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
37  const unsigned char *from;
38  SWBuf orig = text;
39 
40  from = (const unsigned char *)orig.c_str();
41 
42  // -------------------------------
43  text = "";
44  while (*from) {
45 
46  SW_u32 ch = getUniCharFromUTF8(&from);
47 
48  if (!ch) continue; // invalid char
49 
50  if (ch < 0x10000) {
51  text.setSize(text.size()+2);
52  *((SW_u16 *)(text.getRawData() + (text.size() - 2))) = (SW_u16)ch;
53  }
54  else {
55  SW_u16 utf16;
56  utf16 = (SW_s16)((ch - 0x10000) / 0x400 + 0xD800);
57  text.setSize(text.size()+4);
58  *((SW_u16 *)(text.getRawData() + (text.size() - 4))) = utf16;
59  utf16 = (SW_s16)((ch - 0x10000) % 0x400 + 0xDC00);
60  *((SW_u16 *)(text.getRawData() + (text.size() - 2))) = utf16;
61  }
62  }
63  text.setSize(text.size()+2);
64  *((SW_u16 *)(text.getRawData() + (text.size() - 2))) = (SW_u16)0;
65  text.setSize(text.size()-2);
66 
67  return 0;
68 }
69 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
char * getRawData()
Definition: swbuf.h:379
const char * c_str() const
Definition: swbuf.h:158
unsigned short SW_u16
Definition: sysdata.h:38
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
Definition: utf8utf16.cpp:36
unsigned long size() const
Definition: swbuf.h:185
unsigned int SW_u32
Definition: sysdata.h:41
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
SW_u32 getUniCharFromUTF8(const unsigned char **buf, bool skipValidation=false)
Definition: utilstr.h:88
void setSize(unsigned long len)
Definition: swbuf.h:255
signed short SW_s16
Definition: sysdata.h:37