summaryrefslogtreecommitdiff
path: root/src/modules/filters/utf16utf8.cpp
blob: d224db27d6c30b847797a3ce1a1f8ed659dbc813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
 *
 * utf16utf8.cpp -	SWFilter descendant to convert UTF-16 to UTF-8
 *
 * $Id: utf16utf8.cpp 3157 2014-04-17 03:56:12Z greg.hellings $
 *
 * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */


#include <utf16utf8.h>
#include <swbuf.h>


SWORD_NAMESPACE_START


UTF16UTF8::UTF16UTF8() {
}


char UTF16UTF8::processText(SWBuf &text, const SWKey *key, const SWModule *module)
{
  unsigned short *from;

  int len;
  unsigned long uchar;
  unsigned short schar;
  len = 0;
  from = (unsigned short*) text.c_str();
  while (*from) {
        len += 2;
        from++;
  }

	SWBuf orig = text;
	from = (unsigned short*)orig.c_str();


  // -------------------------------

  for (text = ""; *from; from++) {
    uchar = 0;

    if (*from < 0xD800 || *from > 0xDFFF) {
      uchar = *from;
    }
    else if (*from >= 0xD800 && *from <= 0xDBFF) {
      uchar = *from;
      schar = *(from+1);
      if (uchar < 0xDC00 || uchar > 0xDFFF) {
	//error, do nothing
	continue;
      }
      uchar &= 0x03ff;
      schar &= 0x03ff;
      uchar <<= 10;
      uchar |= schar;
      uchar += 0x10000;
      from++;
    }
    else {
      //error, do nothing
      continue;
    }
    
    if (uchar < 0x80) { 
      text += uchar;
    }
    else if (uchar < 0x800) { 
      text += 0xc0 | (uchar >> 6); 
      text += 0x80 | (uchar & 0x3f);
    }
    else if (uchar < 0x10000) {
      text += 0xe0 | (uchar >> 12);
      text += 0x80 | ((uchar >> 6) & 0x3f);
      text += 0x80 | (uchar & 0x3f);
    }
    else if (uchar < 0x200000) {
      text += 0xF0 | (uchar >> 18);
      text += 0x80 | ((uchar >> 12) & 0x3F);
      text += 0x80 | ((uchar >> 6) & 0x3F);
      text += 0x80 | (uchar & 0x3F);
    }
  }
  
  return 0;
}


SWORD_NAMESPACE_END