|
1 /* |
|
2 * Copyright (c) 2007-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 the License "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 * Note: This file may contain code to generate corrupt files for test purposes. |
|
16 * Such code is excluded from production builds by use of compiler defines; |
|
17 * it is recommended that such code should be removed if this code is ever published publicly. |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 /** |
|
23 @file |
|
24 @internalComponent |
|
25 @released |
|
26 */ |
|
27 |
|
28 #include "sisstring.h" |
|
29 #include "utility.h" |
|
30 #include "exception.h" |
|
31 #include "utils.h" |
|
32 #include "utf8.h" |
|
33 |
|
34 // we cannot write or read a string directly from a file, because we don't know what format the |
|
35 // MS implementation of STL will use for the std::string. So we need to go via an intemediate buffer. |
|
36 // But strings can be variable size, including very big indeed, so that buffer will have to be |
|
37 // allocated as need be. Buffer allocation and deletion is expensive. It is sensible to allocate |
|
38 // a common buffer, and only change its size when necessary. To reduce the frequency of file size |
|
39 // changes, the actual allocation oversteps the mark, so the next time something requests a slightly |
|
40 // bigger string, no allocation is required (usually). These variables could be private static members of our |
|
41 // string class, but that would mean some minor change to this code would require the entire |
|
42 // product to be recompiled. |
|
43 |
|
44 static TUint8* gBuffer = NULL; |
|
45 static unsigned long gBufferLength = 0; |
|
46 static const unsigned long gChunk = 512; |
|
47 |
|
48 |
|
49 |
|
50 void VerifyBufferSize (const unsigned long aSize) |
|
51 { |
|
52 if (aSize == 0) |
|
53 { |
|
54 return; |
|
55 } |
|
56 if (aSize > gBufferLength) |
|
57 { |
|
58 delete [] gBuffer; |
|
59 gBufferLength = aSize + gChunk; |
|
60 gBuffer = new TUint8 [gBufferLength]; |
|
61 } |
|
62 } |
|
63 |
|
64 |
|
65 void CSISString::ExitInstance () |
|
66 { |
|
67 delete [] gBuffer; |
|
68 gBuffer = NULL; |
|
69 gBufferLength = 0; |
|
70 } |
|
71 |
|
72 |
|
73 void CSISString::Read (TSISStream& aFile, const TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType) |
|
74 { |
|
75 CSISHeader header (CSISFieldRoot::ESISString); |
|
76 if (IsDataThere (aFile, header, aArrayType)) |
|
77 { |
|
78 if (header.DataSize () == 0) |
|
79 { |
|
80 iData.erase (); |
|
81 } |
|
82 else |
|
83 { |
|
84 unsigned int dataSize = header.DataSize(); |
|
85 VerifyBufferSize (dataSize); |
|
86 dataSize = AlignedSize(dataSize); |
|
87 aFile.read (gBuffer, dataSize); |
|
88 |
|
89 if(sizeof(WCHAR) != 2) |
|
90 { |
|
91 int targetLength = dataSize*sizeof(wchar_t); |
|
92 wchar_t *ptrUCS4 = new wchar_t [dataSize]; |
|
93 UTF16* sourceStart = reinterpret_cast<UTF16*>(gBuffer); |
|
94 UTF16* sourceEnd = sourceStart + dataSize; |
|
95 UCS4* targetStart = reinterpret_cast<UCS4*>(ptrUCS4); |
|
96 UCS4* targetEnd = targetStart + dataSize*2; |
|
97 ConvertUTF16toUCS4(&sourceStart, sourceEnd, &targetStart, targetEnd); |
|
98 iData.assign ((ptrUCS4), static_cast <std::wstring::size_type> (dataSize / 2)); |
|
99 } |
|
100 else |
|
101 { |
|
102 iData.assign ( reinterpret_cast <wchar_t*> (gBuffer), |
|
103 static_cast <std::wstring::size_type> (header.DataSize () / sizeof (wchar_t))); |
|
104 } |
|
105 } |
|
106 |
|
107 } |
|
108 } |
|
109 |
|
110 |
|
111 void CSISString::Write (TSISStream& aFile, const bool aIsArrayElement) const |
|
112 { |
|
113 if (! WasteOfSpace ()) |
|
114 { |
|
115 #ifdef GENERATE_ERRORS |
|
116 if (CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugInsaneString)) |
|
117 { |
|
118 CSISHeader (CSISFieldRoot::ESISString, rand ()).Write (aFile, aIsArrayElement); |
|
119 } |
|
120 else |
|
121 #endif // GENERATE_ERRORS |
|
122 { |
|
123 CSISHeader (CSISFieldRoot::ESISString, ByteCount (aIsArrayElement)).Write (aFile, aIsArrayElement); |
|
124 } |
|
125 |
|
126 // SIS file format only supports UCS-2 format , so need conversion here if wchar is 4 bytes (Linux OS) |
|
127 if(sizeof(WCHAR) != 2) |
|
128 { |
|
129 int targetLength = iData.size()*2; |
|
130 UTF16 *ptrUtf16 = new UTF16[targetLength + 1]; |
|
131 UCS4* sourceStart = reinterpret_cast<UCS4*>(const_cast<wchar_t*>(iData.c_str())); |
|
132 UCS4* sourceEnd = sourceStart + (iData.size()*sizeof(WCHAR)); |
|
133 UTF16* targetStart = reinterpret_cast<UTF16*>(ptrUtf16); |
|
134 UTF16* targetEnd = targetStart + targetLength; |
|
135 ConvertUCS4toUTF16(&sourceStart, sourceEnd, &targetStart, targetEnd); |
|
136 int endOffset = (UTF16*)targetStart - ptrUtf16; |
|
137 targetStart = reinterpret_cast<UTF16*>(ptrUtf16) + endOffset; |
|
138 *targetStart = 0; |
|
139 aFile.write (reinterpret_cast <const TUint8*> (ptrUtf16),targetLength); |
|
140 } |
|
141 else |
|
142 { |
|
143 aFile.write (reinterpret_cast <const TUint8*> (iData.c_str ()), ByteCount (aIsArrayElement)); |
|
144 } |
|
145 WriteFiller (aFile); |
|
146 } |
|
147 } |
|
148 |
|
149 |
|
150 void CSISString::Dump (std::ostream& aStream, const int aLevel) const |
|
151 { |
|
152 aStream << "\"" << wstring2string (iData) << "\""; |
|
153 } |
|
154 |
|
155 |
|
156 void CSISString::CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const |
|
157 { |
|
158 if(!WasteOfSpace()) |
|
159 { |
|
160 CSISHeader (CSISFieldRoot::ESISString, ByteCount (false)).CalculateCrc (aCRC, aIsArrayElement); |
|
161 CSISFieldRoot::DoPaddedCrc(aCRC, reinterpret_cast <const TUint8*> (iData.c_str ()), ByteCount (aIsArrayElement)); |
|
162 } |
|
163 } |
|
164 |
|
165 |
|
166 CSISFieldRoot::TFieldSize CSISString::ByteCount (const bool aInsideArray) const |
|
167 { |
|
168 if(sizeof(wchar_t) != 2) |
|
169 return static_cast <CSISFieldRoot::TFieldSize> (iData.size () * sizeof (wchar_t)/2); |
|
170 else |
|
171 return static_cast <CSISFieldRoot::TFieldSize> (iData.size () * sizeof (wchar_t)); |
|
172 } |
|
173 |
|
174 |
|
175 bool CSISString::WasteOfSpace () const |
|
176 { |
|
177 return ! Required () && iData.empty (); |
|
178 } |
|
179 |