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 "metadata_handler.h"
21 MetaData_Handler(const char* file) {
22 this->file_name = file;
23 m_current_metadata_record_id = -1;
27 void MetaData_Handler::
28 set_current_metadata_record(const char* record_name) {
29 if (record_name == NULL) {
30 m_current_metadata_record_id = -1;
33 int idx = find_metadata_record_index(record_name);
35 m_current_metadata_record_id = idx;
38 int MetaData_Handler::
39 encode(const char* file)const {
40 ResourceStorage storage;
41 encode_metadata(storage, metadata);
44 return storage.get_size();
47 int MetaData_Handler::
48 encode(const char* file, int& offset)const {
49 ResourceStorage storage;
50 encode_metadata(storage, metadata);
51 storage.toFile(file, offset);
53 return storage.get_size();
56 int MetaData_Handler::
57 encode(ResourceStorage& storage)const {
58 encode_metadata(storage, metadata);
59 return storage.get_size();
62 unsigned short MetaData_Handler::
63 get_width(const char* field_name) const {
64 if (field_name == NULL) return 0;
66 int idx = m_current_metadata_record_id;
67 if (idx < 0 || idx > (int)metadata.m_vec_metadata_record.size()) return 0;
69 const MetaData_Record& metadata_record = metadata.m_vec_metadata_record.at(idx);
71 int width = get_width(field_name, metadata_record);
76 unsigned short MetaData_Handler::
77 get_width(const char* name, const MetaData_Record& metadata_record) const {
79 for (size_t i = 0; i < metadata_record.vField.size(); ++i) {
80 if (0 == strcmp(name, metadata_record.vField.at(i).m_name)) {
81 return metadata_record.vField.at(i).m_width;
87 int MetaData_Handler::
88 find_metadata_record_index(const char* name)const {
91 for (size_t i = 0; i < metadata.m_vec_metadata_record.size(); ++i) {
92 const MetaData_Record& metadata_record = metadata.m_vec_metadata_record.at(i);
94 if (0 == strcmp(metadata_record.m_name, name)) {
102 int MetaData_Handler::
103 parsing_field(const xmlNodePtr node, MetaData_Field& data, const MetaData_Width& metadataWidth) {
104 memset(data.m_name, 0, sizeof(data.m_name));
105 memset(data.m_type, 0, sizeof(data.m_type));
108 xmlChar* name = xmlGetProp(node, (const xmlChar*)"name");
109 if (name == NULL) return -1;
111 strncpy(data.m_name, (const char*)name, sizeof(data.m_name));
112 data.m_name[sizeof(data.m_name)-1] = '\0';
115 xmlChar* type = xmlGetProp(node, (const xmlChar*)"type");
116 if (type == NULL) return -1;
119 if (0 == xmlStrcmp(type, (const xmlChar*)"string_id")) {
120 /*FIXME use vaule instead string*/
121 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
122 data.m_type[sizeof(data.m_type) - 1] = '\0';
123 data.m_width = metadataWidth.string_id_width;
124 } else if (0 == xmlStrcmp(type, (const xmlChar*)"int8")) {
125 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
126 data.m_type[sizeof(data.m_type) - 1] = '\0';
128 } else if (0 == xmlStrcmp(type, (const xmlChar*)"int16")) {
129 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
130 data.m_type[sizeof(data.m_type) - 1] = '\0';
132 } else if (0 == xmlStrcmp(type, (const xmlChar*)"int32")) {
133 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
134 data.m_type[sizeof(data.m_type) - 1] = '\0';
136 } else if (0 == xmlStrcmp(type, (const xmlChar*)"float32")) {
137 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
138 data.m_type[sizeof(data.m_type) - 1] = '\0';
140 } else if (0 == xmlStrcmp(type, (const xmlChar*)"float64")) {
141 strncpy(data.m_type, (const char*)type, sizeof(data.m_type));
142 data.m_type[sizeof(data.m_type) - 1] = '\0';
153 int MetaData_Handler::
154 parsing_record(const xmlNodePtr curNode, MetaData_Record& metadataRecord, const MetaData_Width& metadataWidth) {
155 //parsing struct name
156 xmlChar* name = xmlGetProp(curNode, (const xmlChar*)"name");
157 if (name == NULL) return -1;
158 strncpy(metadataRecord.m_name, (const char*)name, sizeof(metadataRecord.m_name));
159 metadataRecord.m_name[sizeof(metadataRecord.m_name)-1] = '\0';
162 xmlNodePtr childNode = curNode->xmlChildrenNode;
164 while (childNode != NULL) {
165 if (0 == xmlStrcmp(childNode->name, (const xmlChar *)"field")) {
167 int ret = parsing_field(childNode, data, metadataWidth);
169 metadataRecord.vField.push_back(data);
173 childNode = childNode->next;
177 int MetaData_Handler::
178 parsing_metadata_type(const xmlNodePtr curNode, MetaData_Width& metadataWidth) {
179 assert(curNode != NULL);
180 xmlNodePtr childNode = curNode->xmlChildrenNode;
182 while (childNode != NULL) {
183 if (0 == xmlStrcmp(childNode->name, (const xmlChar *)"type")) {
184 xmlChar* xmlname = xmlGetProp(childNode, (const xmlChar*)"name");
185 if (xmlname == NULL) continue;
186 xmlChar* xmlwidth = xmlGetProp(childNode, (const xmlChar*)"width");
187 if (xmlwidth == NULL) {
192 /*FIXME how to assume that the atoi will get the correct num*/
193 int width = atoi((const char*)xmlwidth);
195 if (0 == xmlStrcmp(xmlname, (const xmlChar*)"string_id")) {
196 metadataWidth.string_id_width = width;
203 childNode = childNode->next;
207 void MetaData_Handler::
212 doc = xmlReadFile(file_name, NULL, 0);
214 LOGE("Could not load file.\n");
218 curNode = xmlDocGetRootElement(doc);
219 if (curNode == NULL) {
220 LOGE("empty document.\n");
224 if (0 != xmlStrcmp(curNode->name, (const xmlChar*)"metadata"))
226 LOGE("root name %s error!\n", curNode->name);
231 xmlChar* version = xmlGetProp(curNode, (const xmlChar*)"version");
233 strncpy(metadata.m_version, (const char*)version, sizeof(metadata.m_version));
234 metadata.m_version[sizeof(metadata.m_version)-1] = '\0';
237 metadata.m_version[0] = '\0';
240 MetaData_Width metadataWidth;
241 curNode = curNode->xmlChildrenNode;
243 if (0 == xmlStrcmp(curNode->name, (const xmlChar*)"metadata_type")) {
244 parsing_metadata_type(curNode, metadataWidth);
245 } else if (0 == xmlStrcmp(curNode->name, (const xmlChar*)"record")) {
246 MetaData_Record metadataRecord;
247 int ret = parsing_record(curNode, metadataRecord, metadataWidth);
249 metadata.m_vec_metadata_record.push_back(metadataRecord);
252 curNode = curNode->next;
257 inline const MetaData* MetaData_Handler::
258 get_metadata()const {