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