530ce13b0065be91a7d994dc6330290296a08945
[platform/core/uifw/libscl-ui-nui.git] / scl / nine_patch_file_list_bin_parser.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 "nine_patch_file_list_bin_parser.h"
19 #include <memory.h>
20 #include <assert.h>
21 #include "put_record.h"
22
23 BinNinePatchFileParser::BinNinePatchFileParser() {
24     m_size = 0;
25     memset(m_nine_patch_file_list, 0x00, sizeof(SclNinePatchInfo) * MAX_NINE_PATCH_FILE_LIST);
26     parser_info_provider = NULL;
27 }
28
29 BinNinePatchFileParser::~BinNinePatchFileParser() {
30     m_size = 0;
31 }
32 BinNinePatchFileParser* BinNinePatchFileParser::get_instance() {
33     static BinNinePatchFileParser instance;
34     return &instance;
35 }
36 void BinNinePatchFileParser::init(const FileStorage& storage, int offset, int size, IParserInfo_Provider* provider) {
37     m_storage.set_str_provider(provider);
38     m_storage.get_storage(storage, offset, size);
39     this->parser_info_provider = provider;
40
41     parsing_nine_patch_file_list();
42 }
43
44 void BinNinePatchFileParser::parsing_nine_patch_file_list() {
45     Nine_patch_width record_width;
46     set_nine_patch_width(*parser_info_provider, record_width);
47
48     // skip data_size
49     m_storage.advance(8);
50
51     SclNinePatchInfo* cur = m_nine_patch_file_list;
52     m_size = m_storage.get<sint_t>(4);
53     for (int i = 0; i < m_size; ++i) {
54         char* temp = NULL;
55         m_storage.get_str(&(temp), record_width.image_path, m_string_collector);
56         cur->image_path = (char*)temp;
57
58         cur->left = m_storage.get<sint_t>(record_width.left);
59         cur->right = m_storage.get<sint_t>(record_width.right);
60         cur->top = m_storage.get<sint_t>(record_width.top);
61         cur->bottom = m_storage.get<sint_t>(record_width.bottom);
62         cur++;
63     }
64 #ifdef __SCL_TXT_DEBUG
65     put_nine_patch_info(DECODE, m_nine_patch_file_list);
66 #endif
67 }
68
69 SclNinePatchInfo*
70 BinNinePatchFileParser::get_nine_patch_list() {
71     return m_nine_patch_file_list;
72 }
73 bool
74 BinNinePatchFileParser::get_nine_patch_info(const char* filename, SclNinePatchInfo *info) {
75     if (filename == NULL) return false;
76
77     for (int i = 0; i < MAX_NINE_PATCH_FILE_LIST && i < m_size; ++i) {
78         if ( m_nine_patch_file_list[i].image_path != NULL &&
79             0 == strcmp(m_nine_patch_file_list[i].image_path, filename) ) {
80                 if (info) {
81                     *info = m_nine_patch_file_list[i];
82                 }
83             return true;
84         }
85     }
86
87     return false;
88 }