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 __STRING_ENCODER_H__
19 #define __STRING_ENCODER_H__
20 #include "istring_encoder.h"
21 #include "resource_storage_impl.h"
24 /* limit the max num to 2^32-1*/
25 const int MAX_NUM_WIDTH = 4;
27 class String_Encoder:public IString_Encoder{
33 void set_path(const char* file) {
37 /* @ insert str to string vector */
38 /* @ return the ID of the str */
39 int add(const char* str) {
40 if (str == NULL) return -1;
42 std::string str_temp = std::string(str);
44 /* check if the str_temp is already in the vector*/
45 std::vector<std::string>::iterator it;
46 for (it = m_vec_string.begin(); it != m_vec_string.end(); ++it) {
47 if (str_temp == *it) {
48 return (it - m_vec_string.begin());
52 /* the str_temp is not in the vector, so insert it to the vector*/
53 m_vec_string.push_back(str_temp);
54 return m_vec_string.size() -1;
57 ResourceStorage storage;
59 storage.toFile(m_file.c_str());
60 return storage.get_size();
62 int encode(int& offset) const {
63 ResourceStorage storage;
65 storage.toFile(m_file.c_str(), offset);
67 return storage.get_size();
70 int encode(ResourceStorage& storage) const {
71 /* record the strings' num*/
72 storage.put<sint_t>(m_vec_string.size(), MAX_NUM_WIDTH);
74 /* record each string*/
75 std::vector<std::string>::const_iterator it;
76 for (it = m_vec_string.begin(); it != m_vec_string.end(); ++it) {
77 storage.put(it->c_str());
80 return storage.get_size();
84 std::vector<std::string> m_vec_string;