|
1 /* |
|
2 * Copyright (c) 2004-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 * base class for TLV (Type Length Value) classes found in SIS files |
|
19 * |
|
20 */ |
|
21 |
|
22 |
|
23 /** |
|
24 @file |
|
25 @internalComponent |
|
26 @released |
|
27 */ |
|
28 |
|
29 #ifndef __FIELD_H__ |
|
30 #define __FIELD_H__ |
|
31 |
|
32 #include "header.h" |
|
33 #include "exception.h" |
|
34 #include "element.h" |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 template <CSISFieldRoot::TFieldType FieldType> class CSISField : public CSISFieldRoot |
|
40 { |
|
41 public: |
|
42 CSISField (); |
|
43 CSISField (const bool aRequired); |
|
44 CSISField (const CSISField& aInitialiser); |
|
45 |
|
46 public: |
|
47 /** |
|
48 * Retrieves the size of the structure including the header size. |
|
49 * @param aInsideArray - whether the structure is part of an array or not. |
|
50 * @return byte count. |
|
51 */ |
|
52 virtual TFieldSize ByteCountWithHeader (const bool aInsideArray) const; |
|
53 /** |
|
54 * Skip the file reading for this field. Read pointer will be moved to the |
|
55 * next field to be read. |
|
56 * @param aFile stream for which the data read should be skipped. |
|
57 * @param aContainerSize size of the data to be skipped. |
|
58 */ |
|
59 virtual void Skip (TSISStream& aFile, const TFieldSize& aContainerSize) const; |
|
60 /** |
|
61 * stream offset before the header |
|
62 */ |
|
63 virtual TSISStream::pos_type PreHeaderPos () const; |
|
64 /** |
|
65 * stream offset after the header |
|
66 */ |
|
67 virtual TSISStream::pos_type PostHeaderPos () const; |
|
68 /** |
|
69 * Set pre header stream offset |
|
70 * @param aPos new offset |
|
71 */ |
|
72 void SetPreHeaderPos (const TSISStream::pos_type aPos); |
|
73 /** |
|
74 * Set pre header stream offset |
|
75 * @param aPos new offset |
|
76 */ |
|
77 void SetPostHeaderPos (const TSISStream::pos_type aPos); |
|
78 /** |
|
79 * Checks whether this field is a mandatory field or not. |
|
80 * @return true if the field is mandatory else false. |
|
81 */ |
|
82 bool Required () const; |
|
83 /** |
|
84 * Set the required field |
|
85 * @param aValue true if the field is mandatory else false. |
|
86 */ |
|
87 void SetRequired (bool aValue); |
|
88 |
|
89 protected: |
|
90 bool IsDataThere ( TSISStream& aFile, |
|
91 CSISHeader& aHeader, |
|
92 const CSISFieldRoot::TFieldType aArrayType, |
|
93 const bool aPeek = false) const; |
|
94 |
|
95 private: |
|
96 bool iRequired; |
|
97 TSISStream::pos_type iPreHeaderPos; |
|
98 TSISStream::pos_type iPostHeaderPos; |
|
99 }; |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 template <CSISFieldRoot::TFieldType FieldType> inline |
|
105 CSISField <FieldType>::CSISField () : |
|
106 iPreHeaderPos (0), |
|
107 iPostHeaderPos (0), |
|
108 iRequired (true) |
|
109 { |
|
110 } |
|
111 |
|
112 |
|
113 template <CSISFieldRoot::TFieldType FieldType> inline |
|
114 CSISField <FieldType>::CSISField (const bool aRequired) : |
|
115 iPreHeaderPos (0), |
|
116 iPostHeaderPos (0), |
|
117 iRequired (aRequired) |
|
118 { |
|
119 } |
|
120 |
|
121 |
|
122 template <CSISFieldRoot::TFieldType FieldType> inline |
|
123 CSISField <FieldType>::CSISField (const CSISField& aInitialiser) : |
|
124 iPreHeaderPos (aInitialiser.iPreHeaderPos), |
|
125 iPostHeaderPos (aInitialiser.iPostHeaderPos), |
|
126 iRequired (aInitialiser.Required ()) |
|
127 { |
|
128 } |
|
129 |
|
130 |
|
131 template <CSISFieldRoot::TFieldType FieldType> inline |
|
132 bool CSISField <FieldType>::Required () const |
|
133 { |
|
134 return iRequired; |
|
135 } |
|
136 |
|
137 |
|
138 template <CSISFieldRoot::TFieldType FieldType> inline |
|
139 void CSISField <FieldType>::SetRequired (bool aValue) |
|
140 { |
|
141 iRequired = aValue; |
|
142 } |
|
143 |
|
144 |
|
145 template <CSISFieldRoot::TFieldType FieldType> inline |
|
146 bool CSISField <FieldType>::IsDataThere ( |
|
147 TSISStream& aFile, CSISHeader& aHeader, const CSISFieldRoot::TFieldType aArrayType, const bool aPeek) const |
|
148 { |
|
149 if (CSISHeader::IsNextAsExpected (aFile, aHeader, FieldType, aArrayType, aPeek)) |
|
150 { |
|
151 return true; |
|
152 } |
|
153 CSISException::ThrowIf (Required (), CSISException::EFileFormat, "expected data not found"); |
|
154 return false; |
|
155 } |
|
156 |
|
157 |
|
158 template <CSISFieldRoot::TFieldType FieldType> |
|
159 CSISFieldRoot::TFieldSize CSISField <FieldType>::ByteCountWithHeader (const bool aInsideArray) const |
|
160 { |
|
161 if (WasteOfSpace ()) |
|
162 { |
|
163 return 0; |
|
164 } |
|
165 CSISFieldRoot::TFieldSize bytes (ByteCount (false)); |
|
166 return AlignedSize (bytes) + CSISHeader (FieldType, bytes).ByteCount (aInsideArray); |
|
167 } |
|
168 |
|
169 |
|
170 template <CSISFieldRoot::TFieldType FieldType> |
|
171 void CSISField <FieldType>::Skip (TSISStream& aFile, const TFieldSize& aContainerSize) const |
|
172 { |
|
173 CSISHeader header; |
|
174 if (! CSISHeader::IsNextAsExpected (aFile, header, FieldType, CSISFieldRoot::ESISUndefined, false)) |
|
175 { |
|
176 CSISException::ThrowIf (Required (), CSISException::EFileFormat, "expected data missing"); |
|
177 } |
|
178 else |
|
179 { |
|
180 CSISException::ThrowIf (header.DataSize () > aContainerSize, |
|
181 CSISException::EFileFormat, |
|
182 "data larger than container"); |
|
183 aFile.seek (AlignedSize (header.DataSize ()), std::ios_base::cur); |
|
184 } |
|
185 } |
|
186 |
|
187 template <CSISFieldRoot::TFieldType FieldType> inline |
|
188 TSISStream::pos_type CSISField <FieldType>::PreHeaderPos () const |
|
189 { |
|
190 return iPreHeaderPos; |
|
191 } |
|
192 |
|
193 template <CSISFieldRoot::TFieldType FieldType> inline |
|
194 TSISStream::pos_type CSISField <FieldType>::PostHeaderPos () const |
|
195 { |
|
196 return iPostHeaderPos; |
|
197 } |
|
198 |
|
199 template <CSISFieldRoot::TFieldType FieldType> inline |
|
200 void CSISField <FieldType>::SetPreHeaderPos (const TSISStream::pos_type aPos) |
|
201 { |
|
202 iPreHeaderPos = aPos; |
|
203 assert ((iPostHeaderPos == 0) || (iPreHeaderPos <= iPostHeaderPos)); |
|
204 } |
|
205 |
|
206 template <CSISFieldRoot::TFieldType FieldType> inline |
|
207 void CSISField <FieldType>::SetPostHeaderPos (const TSISStream::pos_type aPos) |
|
208 { |
|
209 iPostHeaderPos = aPos; |
|
210 assert ((iPreHeaderPos == 0) || (iPreHeaderPos <= iPostHeaderPos)); |
|
211 } |
|
212 |
|
213 #endif // __FIELD_H__ |
|
214 |