~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/utilfuns/swunicod.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
 
4
 *      CrossWire Bible Society
 
5
 *      P. O. Box 2528
 
6
 *      Tempe, AZ  85280-2528
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License as published by the
 
10
 * Free Software Foundation version 2.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 */
 
18
 
 
19
#include "swunicod.h"
 
20
SWORD_NAMESPACE_START
 
21
unsigned char* UTF32to8 (unsigned long utf32, unsigned char * utf8) {
 
22
  unsigned int i;
 
23
  for (i = 0; i < 6; i++) utf8[i] = 0;
 
24
 
 
25
  if (utf32 < 0x80) {
 
26
    utf8[0] = (char)utf32;
 
27
  }
 
28
  else if (utf32 < 0x800) {
 
29
    i = utf32 & 0x3f;
 
30
    utf8[1] = 0x80 | i;
 
31
    utf32 >>= 6;
 
32
   
 
33
    i = utf32 & 0x1f;
 
34
    utf8[0] = 0xc0 | i;
 
35
  }
 
36
  else if (utf32 < 0x10000) {
 
37
    i = utf32 & 0x3f;
 
38
    utf8[2] = 0x80 | i;
 
39
    utf32 >>= 6;
 
40
 
 
41
    i = utf32 & 0x3f;
 
42
    utf8[1] = 0x80 | i;
 
43
    utf32 >>= 6;
 
44
   
 
45
    i = utf32 & 0x0f;
 
46
    utf8[0] = 0xe0 | i;
 
47
  }
 
48
  else if (utf32 < 0x200000) {
 
49
    i = utf32 & 0x3f;
 
50
    utf8[3] = 0x80 | i;
 
51
    utf32 >>= 6;
 
52
 
 
53
    i = utf32 & 0x3f;
 
54
    utf8[2] = 0x80 | i;
 
55
    utf32 >>= 6;
 
56
 
 
57
    i = utf32 & 0x3f;
 
58
    utf8[1] = 0x80 | i;
 
59
    utf32 >>= 6;
 
60
   
 
61
    i = utf32 & 0x07;
 
62
    utf8[0] = 0xf0 | i;
 
63
  }
 
64
  else if (utf32 < 0x4000000) {
 
65
    i = utf32 & 0x3f;
 
66
    utf8[4] = 0x80 | i;
 
67
    utf32 >>= 6;
 
68
 
 
69
    i = utf32 & 0x3f;
 
70
    utf8[3] = 0x80 | i;
 
71
    utf32 >>= 6;
 
72
 
 
73
    i = utf32 & 0x3f;
 
74
    utf8[2] = 0x80 | i;
 
75
    utf32 >>= 6;
 
76
 
 
77
    i = utf32 & 0x3f;
 
78
    utf8[1] = 0x80 | i;
 
79
    utf32 >>= 6;
 
80
   
 
81
    i = utf32 & 0x03;
 
82
    utf8[0] = 0xf8 | i;
 
83
  }
 
84
  else if (utf32 < 0x80000000) {
 
85
    i = utf32 & 0x3f;
 
86
    utf8[5] = 0x80 | i;
 
87
    utf32 >>= 6;
 
88
 
 
89
    i = utf32 & 0x3f;
 
90
    utf8[4] = 0x80 | i;
 
91
    utf32 >>= 6;
 
92
 
 
93
    i = utf32 & 0x3f;
 
94
    utf8[3] = 0x80 | i;
 
95
    utf32 >>= 6;
 
96
 
 
97
    i = utf32 & 0x3f;
 
98
    utf8[2] = 0x80 | i;
 
99
    utf32 >>= 6;
 
100
 
 
101
    i = utf32 & 0x3f;
 
102
    utf8[1] = 0x80 | i;
 
103
    utf32 >>= 6;
 
104
   
 
105
    i = utf32 & 0x01;
 
106
    utf8[0] = 0xfc | i;
 
107
  }
 
108
  return utf8;
 
109
}
 
110
 
 
111
unsigned long UTF8to32 (unsigned char * utf8) {
 
112
 
 
113
  unsigned char i = utf8[0];
 
114
  unsigned char count;
 
115
  unsigned long utf32 = 0;
 
116
 
 
117
  for (count = 0; i & 0x80; count++) i <<= 1;
 
118
  if (!count) {
 
119
    return utf8[0];
 
120
  }
 
121
  else if (count == 1) {
 
122
    return 0xffff;
 
123
  }
 
124
  else {
 
125
    count--;
 
126
    utf32 = i >> count;
 
127
    for (i = 1; i <= count; i++) {
 
128
      if (0xc0 & utf8[i] != 0x80) {
 
129
        return  0xffff;
 
130
      }
 
131
      utf32 <<= 6;
 
132
      utf32 |= (utf8[i] & 0x3f);
 
133
    }
 
134
  }
 
135
  return utf32;
 
136
}
 
137
 
 
138
SWORD_NAMESPACE_END