Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / file_storage.cpp
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 #include "file_storage_impl.h"
19
20 FileStorage::FileStorage(const IString_Provider* string_provider):
21     m_storage(NULL),
22     m_offset(0),
23     m_size(0),
24     m_string_provider(string_provider) {
25     }
26
27 FileStorage::~FileStorage() {
28     if (m_storage) {
29         delete []m_storage;
30     }
31     m_storage = NULL;
32     m_size = 0;
33     m_offset = 0;
34 }
35
36 int FileStorage::
37 get_offset() const {
38     return m_offset;
39 }
40 void FileStorage::
41 set_str_provider(const IString_Provider* string_provider) {
42     this->m_string_provider = string_provider;
43 }
44 const char* FileStorage::
45 get_str(int width) {
46     assert(m_string_provider);
47
48     if (width <= 0) return NULL;
49
50     int string_no = get<sint_t>(width);
51     return m_string_provider->get_string_by_id(string_no);
52 }
53
54 void FileStorage::
55 get_str(char** p, int width, IStringCollector& collector) {
56     if (p == NULL) return;
57     const char* str = get_str(width);
58     if (str != NULL) {
59         int len = strlen(str) + 1;
60         *p = new char[len];
61         strncpy((*p), str, len);
62         collector.add(*p);
63     } else {
64         *p = NULL;
65     }
66 }
67
68 void FileStorage::
69 advance(int bytes) {
70     m_offset += bytes;
71 }
72
73 const char* FileStorage::
74 get_str() {
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;
79     m_offset += len + 1;
80     return str;
81 }
82
83 int FileStorage::
84     loadFile(const char* file_name) {
85     if (file_name == NULL) return -1;
86
87     FILE* fp = fopen(file_name, "rb");
88     if (fp == NULL) {
89         return -1;
90     }
91
92     if (m_storage != NULL) {
93         delete[] m_storage;
94     }
95     m_storage = NULL;
96
97     if (0 != fseek(fp, 0L, SEEK_END)) {
98         fclose(fp);
99         return -1;
100     }
101
102     long size = ftell(fp);
103
104     if (size > 0) {
105         m_storage = new char[size];
106     }
107     if (m_storage == NULL) {
108         fclose(fp);
109         return -1;
110     }
111
112     if (0 != fseek(fp, 0L, SEEK_SET)) {
113         if (m_storage) {
114             delete []m_storage;
115         }
116         m_storage = NULL;
117         fclose(fp);
118         return -1;
119     }
120     if (size > 0 && 1 != fread(m_storage, size, 1, fp)) {
121         if (m_storage) {
122             delete []m_storage;
123         }
124         m_storage = NULL;
125         fclose(fp);
126         return -1;
127     }
128     fclose(fp);
129
130     m_size = size;
131     m_offset = 0;
132     return m_size;
133 }
134 int FileStorage::
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;
138
139     FILE* fp = fopen(file_name, "rb");
140     if (fp == NULL) {
141         return -1;
142     }
143
144     if (m_storage != NULL) {
145         delete[] m_storage;
146     }
147     m_storage = NULL;
148
149     if (0 != fseek(fp, 0L, SEEK_END)) {
150         fclose(fp);
151         return -1;
152     }
153
154     int file_size = ftell(fp);
155     if (file_size < offset + size) {
156         fclose(fp);
157         return -1;
158     }
159     m_storage = new char[size];
160     if (m_storage == NULL) {
161         fclose(fp);
162         return -1;
163     }
164
165     int nbytes = 0;
166     if (fseek(fp, offset, 0) != -1) {
167         nbytes = fread(m_storage, size, 1, fp);
168     }
169     if (nbytes != 1) {
170         if (m_storage) {
171             delete []m_storage;
172         }
173         m_storage = NULL;
174         fclose(fp);
175         return -1;
176     }
177     fclose(fp);
178
179     m_size = size;
180     m_offset = 0;
181     return m_size;
182 }
183 int FileStorage::
184 get_size() const {
185     return m_size;
186 }
187 int FileStorage::
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;
191
192     if (m_storage != NULL) {
193         delete[] m_storage;
194     }
195
196     m_storage = new char[block_size];
197     if (m_storage == NULL) {
198         return -1;
199     }
200
201     memcpy(m_storage, storage.m_storage + offset, block_size);
202
203     m_size = block_size;
204     m_offset = 0;
205     return 0;
206 }