Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / xml2binary / include / string_encoder.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 __STRING_ENCODER_H__
19 #define __STRING_ENCODER_H__
20 #include "istring_encoder.h"
21 #include "resource_storage_impl.h"
22 #include <vector>
23 #include <string>
24 /* limit the max num to 2^32-1*/
25 const int MAX_NUM_WIDTH = 4;
26
27 class String_Encoder:public IString_Encoder{
28     public:
29         String_Encoder() {
30             m_file.clear();
31         }
32
33         void set_path(const char* file) {
34             m_file = file;
35         }
36
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;
41
42             std::string str_temp = std::string(str);
43
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());
49                 }
50             }
51
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;
55         }
56         int encode() const {
57             ResourceStorage storage;
58             encode(storage);
59             storage.toFile(m_file.c_str());
60             return storage.get_size();
61         }
62         int encode(int& offset) const {
63             ResourceStorage storage;
64             encode(storage);
65             storage.toFile(m_file.c_str(), offset);
66
67             return storage.get_size();
68         }
69
70         int encode(ResourceStorage& storage) const {
71             /* record the strings' num*/
72             storage.put<sint_t>(m_vec_string.size(), MAX_NUM_WIDTH);
73
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());
78             }
79
80             return storage.get_size();
81         }
82
83     private:
84         std::vector<std::string> m_vec_string;
85         std::string m_file;
86 };
87 #endif