1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "resource_item.h"
17 #include <iostream>
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Global {
22 namespace Restool {
23 using namespace std;
ResourceItem()24 ResourceItem::ResourceItem() : type_(ResType::INVALID_RES_TYPE)
25 {
26 }
27
ResourceItem(const ResourceItem & other)28 ResourceItem::ResourceItem(const ResourceItem &other)
29 {
30 CopyFrom(other);
31 }
32
ResourceItem(const string & name,const vector<KeyParam> & keyparams,ResType type)33 ResourceItem::ResourceItem(const string &name, const vector<KeyParam> &keyparams, ResType type)
34 : data_(nullptr), dataLen_(0), name_(name), keyparams_(keyparams), type_(type)
35 {
36 }
37
~ResourceItem()38 ResourceItem::~ResourceItem()
39 {
40 ReleaseData();
41 }
42
SetData(const string & data)43 bool ResourceItem::SetData(const string &data)
44 {
45 return SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length() + 1);
46 }
47
SetData(const int8_t * data,uint32_t length)48 bool ResourceItem::SetData(const int8_t *data, uint32_t length)
49 {
50 if (data == nullptr || length < 0) {
51 return false;
52 }
53
54 ReleaseData();
55 if (length == 0) {
56 // the string in the element directory can be empty
57 data_ = reinterpret_cast<int8_t *>(new (nothrow) int8_t[0]);
58 dataLen_ = 0;
59 return data_ != nullptr;
60 }
61 int8_t *buffer = reinterpret_cast<int8_t *>(new (nothrow) int8_t[length]);
62 if (buffer == nullptr) {
63 return false;
64 }
65
66 bool result = (memset_s(buffer, length, 0, length) == EOK);
67 result = result && (memcpy_s(buffer, length, data, length) == EOK);
68 data_ = buffer;
69 dataLen_ = length;
70 return result;
71 }
72
SetFilePath(const string & filePath)73 void ResourceItem::SetFilePath(const string &filePath)
74 {
75 filePath_ = filePath;
76 }
77
SetLimitKey(const string & limitKey)78 void ResourceItem::SetLimitKey(const string &limitKey)
79 {
80 limitKey_ = limitKey;
81 }
82
SetName(const string & name)83 void ResourceItem::SetName(const string &name)
84 {
85 name_ = name;
86 }
87
GetData() const88 const int8_t *ResourceItem::GetData() const
89 {
90 return data_;
91 }
92
GetDataLength() const93 uint32_t ResourceItem::GetDataLength() const
94 {
95 return dataLen_;
96 }
97
GetName() const98 const string &ResourceItem::GetName() const
99 {
100 return name_;
101 }
102
GetResType() const103 const ResType &ResourceItem::GetResType() const
104 {
105 return type_;
106 }
107
GetKeyParam() const108 const vector<KeyParam> &ResourceItem::GetKeyParam() const
109 {
110 return keyparams_;
111 }
112
GetFilePath() const113 const string &ResourceItem::GetFilePath() const
114 {
115 return filePath_;
116 }
117
GetLimitKey() const118 const string &ResourceItem::GetLimitKey() const
119 {
120 return limitKey_;
121 }
122
operator =(const ResourceItem & other)123 ResourceItem &ResourceItem::operator=(const ResourceItem &other)
124 {
125 if (this == &other) {
126 return *this;
127 }
128 CopyFrom(other);
129 return *this;
130 }
131
132 // below private founction
ReleaseData()133 void ResourceItem::ReleaseData()
134 {
135 if (data_ != nullptr) {
136 delete[] data_;
137 data_ = nullptr;
138 dataLen_ = 0;
139 }
140 }
141
CopyFrom(const ResourceItem & other)142 void ResourceItem::CopyFrom(const ResourceItem &other)
143 {
144 name_ = other.name_;
145 keyparams_ = other.keyparams_;
146 type_ = other.type_;
147 dataLen_ = other.dataLen_;
148 filePath_ = other.filePath_;
149 limitKey_ = other.limitKey_;
150 if (!SetData(other.data_, other.dataLen_)) {
151 ReleaseData();
152 }
153 }
154 }
155 }
156 }