2 * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __RESOURCE_STORAGE_IMPL_H__
19 #define __RESOURCE_STORAGE_IMPL_H__
20 #include "resource_storage.h"
23 inline ResourceStorage::
28 inline void ResourceStorage::init() {
29 m_storage = new char[__NEW_LENGTH__];
30 if (m_storage == NULL) {
31 //throw "init error of ResourceStorage";
36 m_capability = __NEW_LENGTH__;
39 inline ResourceStorage::
46 inline int ResourceStorage::
52 inline void ResourceStorage::
53 put(const char* str) {
54 /*Not allowed str null here*/
56 int len = strlen(str);
57 /*1(record string length) + len + 1(\0))*/
58 int width = 1 + len + 1;
60 *(m_storage + m_size) = len;
63 memcpy(m_storage + m_size, str, len);
66 /*Add '\0' to the end*/
67 *(m_storage + m_size) = '\0';
71 inline int ResourceStorage::
76 inline int ResourceStorage::
77 toFile(const char* fileName) {
78 if (m_storage == NULL) {
81 FILE *fp = fopen(fileName, "rb+");
87 int actual_size = m_size;
88 fwrite(m_storage, actual_size, 1, fp);
93 inline int ResourceStorage::
94 toFile(const char* fileName, int& offset) {
95 if (m_storage == NULL) {
98 FILE *fp = fopen(fileName, "rb+");
104 if (0 != fseek(fp, offset, 0)) {
114 fwrite(m_storage, m_size, 1, fp);
120 inline void ResourceStorage::
122 check_storage(bytes);
125 inline int ResourceStorage::
126 storage_cat(ResourceStorage& storage) {
127 if (storage.get_size() == 0) return 0;
129 int size = storage.get_size();
131 memcpy(m_storage + m_size, storage.m_storage, size);
137 inline void ResourceStorage::
138 check_storage(int width) {
139 if (m_size + width > m_capability) {
143 inline void ResourceStorage::
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__);
151 char* _p = new char[_new_size];
153 LOGW("expand_storage error\n");
156 memset(_p, 0x00, _new_size);
157 memcpy(_p, m_storage, m_size);
161 m_capability = _new_size;
165 ///////////////////////////////////////
166 inline void ResourceStorage::
167 put_uint8(uint8 data) {
168 check_storage(sizeof(uint8));
169 uint8* p = (uint8*)(m_storage + m_size);
171 m_size += sizeof(uint8);
173 inline void ResourceStorage::
174 put_uint16(uint16 data) {
175 check_storage(sizeof(uint16));
176 uint16* p = (uint16*)(m_storage + m_size);
178 m_size += sizeof(uint16);
180 inline void ResourceStorage::
181 put_uint32(uint32 data) {
182 check_storage(sizeof(uint32));
183 uint32* p = (uint32*)(m_storage + m_size);
185 m_size += sizeof(uint32);
187 inline void ResourceStorage::
188 put_uint64(uint64 data) {
189 check_storage(sizeof(uint64));
190 uint64* p = (uint64*)(m_storage + m_size);
192 m_size += sizeof(uint64);
194 // signed value --> unsigned value
196 inline void ResourceStorage::
197 put_int8(int8 data) {
200 inline void ResourceStorage::
201 put_int16(int16 data) {
204 inline void ResourceStorage::
205 put_int32(int32 data) {
208 inline void ResourceStorage::
209 put_int64(int64 data) {
214 inline void ResourceStorage::
215 put_float32(float32 data) {
225 inline void ResourceStorage::
226 put_float64(float64 data) {
236 //unify put_xx method name
237 //the width is sizeof(T)
239 inline void ResourceStorage::
240 put_primitive_data<uint8>(uint8 data) {
244 inline void ResourceStorage::
245 put_primitive_data<uint16>(uint16 data) {
249 inline void ResourceStorage::
250 put_primitive_data<uint32>(uint32 data) {
254 inline void ResourceStorage::
255 put_primitive_data<uint64>(uint64 data) {
259 inline void ResourceStorage::
260 put_primitive_data<int8>(int8 data) {
264 inline void ResourceStorage::
265 put_primitive_data<int16>(int16 data) {
269 inline void ResourceStorage::
270 put_primitive_data<int32>(int32 data) {
274 inline void ResourceStorage::
275 put_primitive_data<int64>(int64 data) {
279 inline void ResourceStorage::
280 put_primitive_data<float32>(float32 data) {
284 inline void ResourceStorage::
285 put_primitive_data<float64>(float64 data) {
289 //put integer value to storage
290 //width is assigned by para "width"
292 inline int ResourceStorage::
293 put<sint_t>(sint_t data, int width) {
299 put_primitive_data<int8>((int8)data);
302 put_primitive_data<int16>((int16)data);
305 put_primitive_data<int32>((int32)data);
308 put_primitive_data<int64>((int64)data);
317 inline int ResourceStorage::
318 put<uint_t>(uint_t data, int width) {
324 put_primitive_data<uint8>((uint8)data);
327 put_primitive_data<uint16>((uint16)data);
330 put_primitive_data<uint32>((uint32)data);
333 put_primitive_data<uint64>((uint64)data);
342 inline int ResourceStorage::
343 put<float_t>(float_t data, int width) {
348 put_primitive_data<float32>((float32)data);
351 put_primitive_data<float64>((float64)data);
359 inline int ResourceStorage::
360 random_put<sint_t>(sint_t data, int width, int offset) {
361 if (offset <0 || offset + width > m_size) {
365 //remember current_offset
366 int current_offset = m_size;
368 //temperary turn to random_offset
369 int random_offset = offset;
370 m_size = random_offset;
372 int ret = put<sint_t>(data, width);
373 //reback to current_offset
374 m_size = current_offset;
379 inline int ResourceStorage::
380 random_put<uint_t>(uint_t data, int width, int offset) {
381 if (offset <0 || offset + width > m_size) {
385 //remember current_offset
386 int current_offset = m_size;
388 //temperary turn to random_offset
389 int random_offset = offset;
390 m_size = random_offset;
392 int ret = put<uint_t>(data, width);
393 //reback to current_offset
394 m_size = current_offset;
399 inline int ResourceStorage::
400 random_put<float_t>(float_t data, int width, int offset) {
401 if (offset <0 || offset + width > m_size) {
405 //remember current_offset
406 int current_offset = m_size;
408 //temperary turn to random_offset
409 int random_offset = offset;
410 m_size = random_offset;
412 int ret = put<float_t>(data, width);
413 //reback to current_offset
414 m_size = current_offset;
419 ///////////////////////////////////////
420 inline void ResourceStorage::
421 encode_string(const char* str, int width) {
422 if (width <= 0) return;
424 int id = string_encoder.add(str);
425 put<sint_t>(id, width);