• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()
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     int8_t *buffer = reinterpret_cast<int8_t *>(new (nothrow) int8_t[length]);
56     if (buffer == nullptr) {
57         return false;
58     }
59 
60     bool result = (memset_s(buffer, length, 0, length) == EOK);
61     result = result && (memcpy_s(buffer, length, data, length) == EOK);
62     data_ = buffer;
63     dataLen_ = length;
64     return result;
65 }
66 
SetFilePath(const string & filePath)67 void ResourceItem::SetFilePath(const string &filePath)
68 {
69     filePath_ = filePath;
70 }
71 
SetLimitKey(const string & limitKey)72 void ResourceItem::SetLimitKey(const string &limitKey)
73 {
74     limitKey_ = limitKey;
75 }
76 
SetName(const string & name)77 void ResourceItem::SetName(const string &name)
78 {
79     name_ = name;
80 }
81 
GetData() const82 const int8_t *ResourceItem::GetData() const
83 {
84     return data_;
85 }
86 
GetDataLength() const87 uint32_t ResourceItem::GetDataLength() const
88 {
89     return dataLen_;
90 }
91 
GetName() const92 const string &ResourceItem::GetName() const
93 {
94     return name_;
95 }
96 
GetResType() const97 const ResType &ResourceItem::GetResType() const
98 {
99     return type_;
100 }
101 
GetKeyParam() const102 const vector<KeyParam> &ResourceItem::GetKeyParam() const
103 {
104     return keyparams_;
105 }
106 
GetFilePath() const107 const string &ResourceItem::GetFilePath() const
108 {
109     return filePath_;
110 }
111 
GetLimitKey() const112 const string &ResourceItem::GetLimitKey() const
113 {
114     return limitKey_;
115 }
116 
operator =(const ResourceItem & other)117 ResourceItem &ResourceItem::operator=(const ResourceItem &other)
118 {
119     if (this == &other) {
120         return *this;
121     }
122     CopyFrom(other);
123     return *this;
124 }
125 
126 // below private founction
ReleaseData()127 void ResourceItem::ReleaseData()
128 {
129     if (data_ != nullptr) {
130         delete[] data_;
131         data_ = nullptr;
132         dataLen_ = 0;
133     }
134 }
135 
CopyFrom(const ResourceItem & other)136 void ResourceItem::CopyFrom(const ResourceItem &other)
137 {
138     name_ = other.name_;
139     keyparams_ = other.keyparams_;
140     type_ = other.type_;
141     dataLen_ = other.dataLen_;
142     filePath_ = other.filePath_;
143     limitKey_ = other.limitKey_;
144     if (!SetData(other.data_, other.dataLen_)) {
145         ReleaseData();
146     }
147 }
148 }
149 }
150 }