1 /* |
|
2 * Copyright (c) 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: OSN string data implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <stdio.h> |
|
20 #include <stdarg.h> |
|
21 |
|
22 #if defined(__GNUC__) |
|
23 #include <string.h> |
|
24 #else |
|
25 #include <libc/string.h> |
|
26 #endif |
|
27 |
|
28 #include <osn/osntypes.h> |
|
29 |
|
30 #include "alfstringdata.h" |
|
31 //#include "osn/char.h" |
|
32 #include <osn/osnbadalloc.h> |
|
33 |
|
34 using namespace std; |
|
35 |
|
36 namespace osncore |
|
37 { |
|
38 |
|
39 |
|
40 |
|
41 // ------------------------------------------------------------------------- |
|
42 // stringData |
|
43 // ------------------------------------------------------------------------- |
|
44 SAlfStringData::SAlfStringData() : refCount(1), /*_unicode(0),*/ _ascii(0), _maxAscii(QS_INTERNAL_BUFFER_CHARS), _isAsciiValid(1) |
|
45 { |
|
46 //#ifdef QSTRING_DEBUG_ALLOCATIONS |
|
47 // stringDataInstances++; |
|
48 //#endif |
|
49 refCount = 1; |
|
50 _length=0; |
|
51 // _unicode = 0; |
|
52 _isUnicodeValid=0; |
|
53 _isAsciiValid = 1; |
|
54 _isHeapAllocated=0; |
|
55 _ascii = _internalBuffer; |
|
56 _internalBuffer[0] = 0; |
|
57 } |
|
58 |
|
59 SAlfStringData::~SAlfStringData() |
|
60 { |
|
61 // ASSERT(refCount == 0); |
|
62 /* if (_unicode && !isUnicodeInternal()) |
|
63 { |
|
64 DELETE_QCHAR(_unicode); |
|
65 } |
|
66 */ |
|
67 if (_ascii && !isAsciiInternal()) |
|
68 { |
|
69 DELETE_CHAR(_ascii); |
|
70 } |
|
71 |
|
72 } |
|
73 |
|
74 |
|
75 // Copy data |
|
76 void SAlfStringData::initialize(const char *a, int l) |
|
77 { |
|
78 refCount = 1; |
|
79 _length = l; |
|
80 // _unicode = 0; |
|
81 _isUnicodeValid = 0; |
|
82 _maxUnicode = 0; |
|
83 _isAsciiValid = 1; |
|
84 _isHeapAllocated = 0; |
|
85 |
|
86 if (l > QS_INTERNAL_BUFFER_CHARS) |
|
87 { |
|
88 _maxAscii = ALLOC_CHAR_GOOD_SIZE(l+1); |
|
89 _ascii = ALLOC_CHAR(_maxAscii); |
|
90 //Make sure we have memory before doing data copy. |
|
91 if(_ascii) |
|
92 { |
|
93 if (a) |
|
94 { |
|
95 memcpy(_ascii, a, l); |
|
96 } |
|
97 _ascii[l] = 0; |
|
98 } |
|
99 else{ |
|
100 _length = 0; |
|
101 refCount = 0; |
|
102 _length = 0; |
|
103 // _unicode = 0; |
|
104 _isUnicodeValid = 0; |
|
105 _maxUnicode = 0; |
|
106 _isAsciiValid = 1; |
|
107 _isHeapAllocated = 0; |
|
108 throw bad_alloc(); |
|
109 } |
|
110 } |
|
111 else { |
|
112 _maxAscii = QS_INTERNAL_BUFFER_CHARS; |
|
113 _ascii = _internalBuffer; |
|
114 if (a) |
|
115 memcpy(_internalBuffer, a, l); |
|
116 _internalBuffer[l] = 0; |
|
117 } |
|
118 |
|
119 |
|
120 } |
|
121 |
|
122 |
|
123 bool SAlfStringData::increaseAsciiSize(uint size) |
|
124 { |
|
125 //ASSERT(this != QString::shared_null); |
|
126 |
|
127 uint newSize = (uint)ALLOC_CHAR_GOOD_SIZE((size * 3 + 1) / 2); |
|
128 |
|
129 /* |
|
130 if (!_isAsciiValid) |
|
131 return false; |
|
132 */ |
|
133 // ASSERT(_isAsciiValid); |
|
134 |
|
135 if (isAsciiInternal()) { |
|
136 char *newAscii = ALLOC_CHAR(newSize); |
|
137 if( !newAscii ) return false; |
|
138 |
|
139 if (_length) |
|
140 memcpy(newAscii, _ascii, _length); |
|
141 _ascii = newAscii; |
|
142 } else { |
|
143 char* ap = _ascii; |
|
144 _ascii = REALLOC_CHAR( _ascii, newSize ); |
|
145 |
|
146 if( !_ascii ) |
|
147 { |
|
148 // memory manager won't delete the original pointer if realloc failed |
|
149 _ascii = ap; |
|
150 return false; |
|
151 } |
|
152 } |
|
153 |
|
154 _maxAscii = newSize; |
|
155 _isAsciiValid = 1; |
|
156 _isUnicodeValid = 0; |
|
157 return true; |
|
158 } |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 char* SAlfStringData::ascii() |
|
165 { |
|
166 // return _isAsciiValid ? _ascii :0; |
|
167 return _ascii; |
|
168 } |
|
169 |
|
170 |
|
171 /* |
|
172 AlfChar *SAlfStringData::makeUnicode() |
|
173 { |
|
174 //ASSERT(this != QString::shared_null); |
|
175 |
|
176 if (_isAsciiValid){ |
|
177 char copyBuf[QS_INTERNAL_BUFFER_CHARS]; |
|
178 // char *str; |
|
179 |
|
180 if (_unicode && !isUnicodeInternal()) |
|
181 DELETE_QCHAR(_unicode); |
|
182 |
|
183 if (_length <= QS_INTERNAL_BUFFER_UCHARS){ |
|
184 if (isAsciiInternal()) { |
|
185 uint i = _length; |
|
186 char *tp = ©Buf[0], *fp = _ascii; |
|
187 while (i--) |
|
188 *tp++ = *fp++; |
|
189 // str = ©Buf[0]; |
|
190 _isAsciiValid = 0; |
|
191 } |
|
192 else |
|
193 { |
|
194 // str = _ascii; |
|
195 |
|
196 } |
|
197 _unicode = (AlfChar *)_internalBuffer; |
|
198 _maxUnicode = QS_INTERNAL_BUFFER_UCHARS; |
|
199 } |
|
200 else { |
|
201 uint newSize = ALLOC_QCHAR_GOOD_SIZE(_length); |
|
202 _unicode = ALLOC_QCHAR(newSize); |
|
203 if( !_unicode ) |
|
204 { |
|
205 _maxUnicode = 0; |
|
206 return 0; |
|
207 } |
|
208 _maxUnicode = newSize; |
|
209 // str = _ascii; |
|
210 } |
|
211 // uint i = _length; |
|
212 // AlfChar *cp = _unicode; |
|
213 // while ( i-- ) |
|
214 // *cp++ = *str++; |
|
215 |
|
216 _isUnicodeValid = 1; |
|
217 } |
|
218 // else if (!_isUnicodeValid) |
|
219 // FATAL("invalid character cache",0); |
|
220 |
|
221 return _unicode; |
|
222 } |
|
223 |
|
224 */ |
|
225 } // osncore |
|
226 |
|