d4d10fc0195b327c4a688761d70598fce374c7b9
[platform/core/uifw/libscl-ui-nui.git] / xml2binary / include / resource_storage_impl.h
1 /*
2  * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #ifndef __RESOURCE_STORAGE_IMPL_H__
19 #define __RESOURCE_STORAGE_IMPL_H__
20 #include "resource_storage.h"
21 #include <dlog.h>
22
23 inline ResourceStorage::
24 ResourceStorage() {
25     init();
26 }
27
28 inline void ResourceStorage::init() {
29     m_storage = new char[__NEW_LENGTH__];
30     if (m_storage == NULL) {
31         //throw "init error of ResourceStorage";
32         m_size = 0;
33         m_capability = 0;
34     } else {
35         m_size = 0;
36         m_capability = __NEW_LENGTH__;
37     }
38 }
39 inline ResourceStorage::
40 ~ResourceStorage() {
41     delete []m_storage;
42     m_size = 0;
43     m_capability = 0;
44 }
45
46 inline int ResourceStorage::
47 get_size() const {
48     return m_size;
49 }
50
51
52 inline void ResourceStorage::
53 put(const char* str) {
54     /*Not allowed str null here*/
55     assert(str != NULL);
56     int len = strlen(str);
57     /*1(record string length) + len + 1(\0))*/
58     int width = 1 + len + 1;
59     check_storage(width);
60     *(m_storage + m_size) = len;
61     m_size += 1;
62
63     memcpy(m_storage + m_size, str, len);
64     m_size += len;
65
66     /*Add '\0' to the end*/
67     *(m_storage + m_size) = '\0';
68     m_size += 1;
69 }
70
71 inline int ResourceStorage::
72 capability() const {
73     return m_capability;
74 }
75
76 inline int ResourceStorage::
77 toFile(const char* fileName) {
78     if (m_storage == NULL) {
79         return -1;
80     }
81     FILE *fp = fopen(fileName, "rb+");
82     if (!fp) {
83         perror(fileName);
84         return -1;
85     }
86
87     int actual_size = m_size;
88     fwrite(m_storage, actual_size, 1, fp);
89     fclose(fp);
90
91     return actual_size;
92 }
93 inline int ResourceStorage::
94 toFile(const char* fileName, int& offset) {
95     if (m_storage == NULL) {
96         return -1;
97     }
98     FILE *fp = fopen(fileName, "rb+");
99     if (!fp) {
100         perror(fileName);
101         return -1;
102     }
103
104     if (0 != fseek(fp, offset, 0)) {
105         perror(fileName);
106         fclose(fp);
107         return -1;
108     }
109
110     if (m_size < 0) {
111         fclose(fp);
112         return -1;
113     }
114     fwrite(m_storage, m_size, 1, fp);
115     fclose(fp);
116
117     offset += m_size;
118     return m_size;
119 }
120 inline void ResourceStorage::
121 reserve(int bytes) {
122     check_storage(bytes);
123     m_size += bytes;
124 }
125 inline int ResourceStorage::
126 storage_cat(ResourceStorage& storage) {
127     if (storage.get_size() == 0) return 0;
128
129     int size = storage.get_size();
130     check_storage(size);
131     memcpy(m_storage + m_size, storage.m_storage, size);
132
133     m_size += size;
134     return m_size;
135 }
136
137 inline void ResourceStorage::
138 check_storage(int width) {
139     if (m_size + width > m_capability) {
140         expand_storage();
141     }
142 }
143 inline void ResourceStorage::
144 expand_storage() {
145     unsigned int _new_size = (unsigned int)(m_capability + __RE_NEW_LENGTH__);
146     if (_new_size > (unsigned int)__MAX_NEW_SIZE__) {
147         LOGW("expand_storage failed: size is limited to %d\n", __MAX_NEW_SIZE__);
148         return;
149     }
150
151     char* _p = new char[_new_size];
152     if (_p == NULL) {
153         LOGW("expand_storage error\n");
154         return;
155     }
156     memset(_p, 0x00, _new_size);
157     memcpy(_p, m_storage, m_size);
158     delete[] m_storage;
159
160     m_storage = _p;
161     m_capability = _new_size;
162
163     _p = NULL;
164 }
165 ///////////////////////////////////////
166 inline void ResourceStorage::
167 put_uint8(uint8 data) {
168     check_storage(sizeof(uint8));
169     uint8* p = (uint8*)(m_storage + m_size);
170     *p = data;
171     m_size += sizeof(uint8);
172 }
173 inline void ResourceStorage::
174 put_uint16(uint16 data) {
175     check_storage(sizeof(uint16));
176     uint16* p = (uint16*)(m_storage + m_size);
177     *p = data;
178     m_size += sizeof(uint16);
179 }
180 inline void ResourceStorage::
181 put_uint32(uint32 data) {
182     check_storage(sizeof(uint32));
183     uint32* p = (uint32*)(m_storage + m_size);
184     *p = data;
185     m_size += sizeof(uint32);
186 }
187 inline void ResourceStorage::
188 put_uint64(uint64 data) {
189     check_storage(sizeof(uint64));
190     uint64* p = (uint64*)(m_storage + m_size);
191     *p = data;
192     m_size += sizeof(uint64);
193 }
194 // signed value --> unsigned value
195 //that is ok
196 inline void ResourceStorage::
197 put_int8(int8 data) {
198     put_uint8(data);
199 }
200 inline void ResourceStorage::
201 put_int16(int16 data) {
202     put_uint16(data);
203 }
204 inline void ResourceStorage::
205 put_int32(int32 data) {
206     put_uint32(data);
207 }
208 inline void ResourceStorage::
209 put_int64(int64 data) {
210     put_uint64(data);
211 }
212
213 //float32
214 inline void ResourceStorage::
215 put_float32(float32 data) {
216     union{
217         float32 m;
218         uint32 n;
219     };
220
221     m = data;
222     put_uint32(n);
223 }
224 //float64
225 inline void ResourceStorage::
226 put_float64(float64 data) {
227     union{
228         float64 m;
229         uint64 n;
230     };
231
232     m = data;
233     put_uint64(n);
234 }
235
236 //unify put_xx method name
237 //the width is sizeof(T)
238 template <>
239 inline void ResourceStorage::
240 put_primitive_data<uint8>(uint8 data) {
241     put_uint8(data);
242 }
243 template <>
244 inline void ResourceStorage::
245 put_primitive_data<uint16>(uint16 data) {
246     put_uint16(data);
247 }
248 template <>
249 inline void ResourceStorage::
250 put_primitive_data<uint32>(uint32 data) {
251     put_uint32(data);
252 }
253 template <>
254 inline void ResourceStorage::
255 put_primitive_data<uint64>(uint64 data) {
256     put_uint64(data);
257 }
258 template <>
259 inline void ResourceStorage::
260 put_primitive_data<int8>(int8 data) {
261     put_int8(data);
262 }
263 template <>
264 inline void ResourceStorage::
265 put_primitive_data<int16>(int16 data) {
266     put_int16(data);
267 }
268 template <>
269 inline void ResourceStorage::
270 put_primitive_data<int32>(int32 data) {
271     put_int32(data);
272 }
273 template <>
274 inline void ResourceStorage::
275 put_primitive_data<int64>(int64 data) {
276     put_int64(data);
277 }
278 template <>
279 inline void ResourceStorage::
280 put_primitive_data<float32>(float32 data) {
281     put_float32(data);
282 }
283 template <>
284 inline void ResourceStorage::
285 put_primitive_data<float64>(float64 data) {
286     put_float64(data);
287 }
288
289 //put integer value to storage
290 //width is assigned by para "width"
291 template <>
292 inline int ResourceStorage::
293 put<sint_t>(sint_t data, int width) {
294     switch (width) {
295         case 0:
296             /*Nothing to put*/
297             break;
298         case 1:
299             put_primitive_data<int8>((int8)data);
300             break;
301         case 2:
302             put_primitive_data<int16>((int16)data);
303             break;
304         case 4:
305             put_primitive_data<int32>((int32)data);
306             break;
307         case 8:
308             put_primitive_data<int64>((int64)data);
309             break;
310         default:
311             return -1;
312     }
313
314     return 0;
315 }
316 template <>
317 inline int ResourceStorage::
318 put<uint_t>(uint_t data, int width) {
319     switch (width) {
320         case 0:
321             /*Nothing to put*/
322             break;
323         case 1:
324             put_primitive_data<uint8>((uint8)data);
325             break;
326         case 2:
327             put_primitive_data<uint16>((uint16)data);
328             break;
329         case 4:
330             put_primitive_data<uint32>((uint32)data);
331             break;
332         case 8:
333             put_primitive_data<uint64>((uint64)data);
334             break;
335         default:
336             return -1;
337     }
338
339     return 0;
340 }
341 template <>
342 inline int ResourceStorage::
343 put<float_t>(float_t data, int width) {
344     switch (width) {
345         case 0:
346             break;
347         case 4:
348             put_primitive_data<float32>((float32)data);
349             break;
350         case 8:
351             put_primitive_data<float64>((float64)data);
352             break;
353         default:
354             return -1;
355     }
356     return 0;
357 }
358 template<>
359 inline int ResourceStorage::
360 random_put<sint_t>(sint_t data, int width, int offset) {
361     if (offset <0 || offset + width > m_size) {
362         return -1;
363     }
364
365     //remember current_offset
366     int current_offset = m_size;
367
368     //temperary turn to random_offset
369     int random_offset = offset;
370     m_size = random_offset;
371
372     int ret = put<sint_t>(data, width);
373     //reback to current_offset
374     m_size = current_offset;
375
376     return ret;
377 }
378 template<>
379 inline int ResourceStorage::
380 random_put<uint_t>(uint_t data, int width, int offset) {
381     if (offset <0 || offset + width > m_size) {
382         return -1;
383     }
384
385     //remember current_offset
386     int current_offset = m_size;
387
388     //temperary turn to random_offset
389     int random_offset = offset;
390     m_size = random_offset;
391
392     int ret = put<uint_t>(data, width);
393     //reback to current_offset
394     m_size = current_offset;
395
396     return ret;
397 }
398 template <>
399 inline int ResourceStorage::
400 random_put<float_t>(float_t data, int width, int offset) {
401     if (offset <0 || offset + width > m_size) {
402         return -1;
403     }
404
405     //remember current_offset
406     int current_offset = m_size;
407
408     //temperary turn to random_offset
409     int random_offset = offset;
410     m_size = random_offset;
411
412     int ret = put<float_t>(data, width);
413     //reback to current_offset
414     m_size = current_offset;
415
416     return ret;
417 }
418
419 ///////////////////////////////////////
420 inline void ResourceStorage::
421 encode_string(const char* str, int width) {
422     if (width <= 0) return;
423
424     int id = string_encoder.add(str);
425     put<sint_t>(id, width);
426 }
427
428
429 #endif