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 #include "file_storage_impl.h"
20 FileStorage::FileStorage(const IString_Provider* string_provider):
24 m_string_provider(string_provider) {
27 FileStorage::~FileStorage() {
41 set_str_provider(const IString_Provider* string_provider) {
42 this->m_string_provider = string_provider;
44 const char* FileStorage::
46 assert(m_string_provider);
48 if (width <= 0) return NULL;
50 int string_no = get<sint_t>(width);
51 return m_string_provider->get_string_by_id(string_no);
55 get_str(char** p, int width, IStringCollector& collector) {
56 if (p == NULL) return;
57 const char* str = get_str(width);
59 int len = strlen(str) + 1;
61 strncpy((*p), str, len);
73 const char* FileStorage::
75 // the length of the string, not include '\0'
76 int len = get<sint_t>(1);
77 // the string stored in storage are end with '\0'
78 const char* str = m_storage + m_offset;
84 loadFile(const char* file_name) {
85 if (file_name == NULL) return -1;
87 FILE* fp = fopen(file_name, "rb");
92 if (m_storage != NULL) {
97 if (0 != fseek(fp, 0L, SEEK_END)) {
102 long size = ftell(fp);
105 m_storage = new char[size];
107 if (m_storage == NULL) {
112 if (0 != fseek(fp, 0L, SEEK_SET)) {
120 if (size > 0 && 1 != fread(m_storage, size, 1, fp)) {
135 loadFile(const char* file_name, int offset, int size) {
136 if (file_name == NULL) return -1;
137 if (offset < 0 || size <= 0) return -1;
139 FILE* fp = fopen(file_name, "rb");
144 if (m_storage != NULL) {
149 if (0 != fseek(fp, 0L, SEEK_END)) {
154 int file_size = ftell(fp);
155 if (file_size < offset + size) {
159 m_storage = new char[size];
160 if (m_storage == NULL) {
166 if (fseek(fp, offset, 0) != -1) {
167 nbytes = fread(m_storage, size, 1, fp);
188 get_storage(const FileStorage& storage, int offset, int block_size) {
189 if (offset < 0 || block_size <= 0) return -1;
190 if (storage.get_size() < offset + block_size) return -1;
192 if (m_storage != NULL) {
196 m_storage = new char[block_size];
197 if (m_storage == NULL) {
201 memcpy(m_storage, storage.m_storage + offset, block_size);