sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Header STRNG.CPP
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "STRNG.H"
|
sl@0
|
21 |
|
sl@0
|
22 |
extern bool OutputUnicode;
|
sl@0
|
23 |
|
sl@0
|
24 |
ostream& operator << (ostream& out, const String& aString)
|
sl@0
|
25 |
{
|
sl@0
|
26 |
for (int i = 0; i < aString.iLength; i++)
|
sl@0
|
27 |
out << aString.iText[i];
|
sl@0
|
28 |
out << '\n';
|
sl@0
|
29 |
return out;
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
EXPORT_C void String::Externalize(ostream& out)
|
sl@0
|
33 |
{
|
sl@0
|
34 |
if (OutputUnicode)
|
sl@0
|
35 |
{
|
sl@0
|
36 |
// Convert the string to Unicode, allowing #NNNN (each N is a hex digit)
|
sl@0
|
37 |
// to represent an arbitrary Unicode character. Other values are just
|
sl@0
|
38 |
// extended, so don't use codepage 1252 values in the range 128..159.
|
sl@0
|
39 |
unsigned short* buffer = new unsigned short[iLength];
|
sl@0
|
40 |
int i = 0;
|
sl@0
|
41 |
int j = 0;
|
sl@0
|
42 |
while (i < iLength)
|
sl@0
|
43 |
{
|
sl@0
|
44 |
if (iText[i] == '#')
|
sl@0
|
45 |
{
|
sl@0
|
46 |
i++;
|
sl@0
|
47 |
char hex[5];
|
sl@0
|
48 |
hex[0] = iText[i++];
|
sl@0
|
49 |
hex[1] = iText[i++];
|
sl@0
|
50 |
hex[2] = iText[i++];
|
sl@0
|
51 |
hex[3] = iText[i++];
|
sl@0
|
52 |
hex[4] = 0;
|
sl@0
|
53 |
buffer[j++] = (unsigned short)strtoul(hex, NULL, 16);
|
sl@0
|
54 |
}
|
sl@0
|
55 |
else
|
sl@0
|
56 |
{
|
sl@0
|
57 |
buffer[j] = iText[i];
|
sl@0
|
58 |
buffer[j] &= 0xFF;
|
sl@0
|
59 |
i++;
|
sl@0
|
60 |
j++;
|
sl@0
|
61 |
}
|
sl@0
|
62 |
}
|
sl@0
|
63 |
int unicode_characters = j;
|
sl@0
|
64 |
int32 length = (unicode_characters << 1); // 16-bit data
|
sl@0
|
65 |
if (length < 0x80)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
unsigned char len = (unsigned char)(length << 1);
|
sl@0
|
68 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
69 |
}
|
sl@0
|
70 |
else if (length < 0x4000)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
uint16 len = (uint16)((length << 2) + 1);
|
sl@0
|
73 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
74 |
}
|
sl@0
|
75 |
else
|
sl@0
|
76 |
{
|
sl@0
|
77 |
// assert len<0x20000000 ?
|
sl@0
|
78 |
uint32 len = (uint32)((length << 3) + 3);
|
sl@0
|
79 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
80 |
}
|
sl@0
|
81 |
// Output Unicode characters using the Standard Compression Scheme for Unicode.
|
sl@0
|
82 |
// To save the bother of doing this properly, use a degenerate form whereby each
|
sl@0
|
83 |
// Unicode character is output as itself. 0x0F selects Unicode mode and 0xF0 quotes
|
sl@0
|
84 |
// characters that would conflict with other tags.
|
sl@0
|
85 |
out << (unsigned char)0x0F;
|
sl@0
|
86 |
|
sl@0
|
87 |
for (i = 0; i < unicode_characters; i++)
|
sl@0
|
88 |
{
|
sl@0
|
89 |
unsigned char hi = (unsigned char)(buffer[i] >> 8);
|
sl@0
|
90 |
unsigned char lo = (unsigned char)buffer[i];
|
sl@0
|
91 |
if ((hi >= 0xe0) && (hi <= 0xf2))
|
sl@0
|
92 |
out << 0xf0;
|
sl@0
|
93 |
out << hi;
|
sl@0
|
94 |
out << lo;
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
delete [] buffer;
|
sl@0
|
98 |
}
|
sl@0
|
99 |
else
|
sl@0
|
100 |
{
|
sl@0
|
101 |
int32 length = (iLength << 1) + 1; // 8-bit data
|
sl@0
|
102 |
if (length < 0x80)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
unsigned char len = (unsigned char)(length << 1);
|
sl@0
|
105 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
106 |
}
|
sl@0
|
107 |
else if (length < 0x4000)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
uint16 len = (uint16)((length << 2) + 1);
|
sl@0
|
110 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
111 |
}
|
sl@0
|
112 |
else
|
sl@0
|
113 |
{
|
sl@0
|
114 |
// assert len<0x20000000 ?
|
sl@0
|
115 |
uint32 len = (uint32)((length << 3) + 3);
|
sl@0
|
116 |
out.write((char*)&len, sizeof(len));
|
sl@0
|
117 |
}
|
sl@0
|
118 |
out.write(iText, iLength);
|
sl@0
|
119 |
}
|
sl@0
|
120 |
}
|
sl@0
|
121 |
|
sl@0
|
122 |
EXPORT_C int String::CreateText(const int aLength)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
if (aLength != iLength)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
char* text = new char[aLength + 1];
|
sl@0
|
127 |
if (text)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
iLength = aLength;
|
sl@0
|
130 |
iText = text;
|
sl@0
|
131 |
}
|
sl@0
|
132 |
else
|
sl@0
|
133 |
{
|
sl@0
|
134 |
iLength = 0;
|
sl@0
|
135 |
delete [] iText;
|
sl@0
|
136 |
iText = NULL;
|
sl@0
|
137 |
}
|
sl@0
|
138 |
}
|
sl@0
|
139 |
return iLength;
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
EXPORT_C void String::DeleteText(char* aText) const
|
sl@0
|
143 |
{
|
sl@0
|
144 |
if (aText != iText)
|
sl@0
|
145 |
delete [] aText;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
EXPORT_C String::~String()
|
sl@0
|
149 |
{
|
sl@0
|
150 |
delete [] iText;
|
sl@0
|
151 |
}
|