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 "data_buffer.h"
17 #include "distributed_camera_errno.h"
18
19 namespace OHOS {
20 namespace DistributedHardware {
DataBuffer(size_t capacity)21 DataBuffer::DataBuffer(size_t capacity)
22 {
23 if (capacity != 0) {
24 data_ = new uint8_t[capacity] {0};
25 if (data_ != nullptr) {
26 capacity_ = capacity;
27 rangeLength_ = capacity;
28 }
29 }
30 }
31
Capacity() const32 size_t DataBuffer::Capacity() const
33 {
34 return capacity_;
35 }
36
Size() const37 size_t DataBuffer::Size() const
38 {
39 return rangeLength_;
40 }
41
Offset() const42 size_t DataBuffer::Offset() const
43 {
44 return rangeOffset_;
45 }
46
Data() const47 uint8_t *DataBuffer::Data() const
48 {
49 return data_ + rangeOffset_;
50 }
51
SetRange(size_t offset,size_t size)52 int32_t DataBuffer::SetRange(size_t offset, size_t size)
53 {
54 if (!(offset <= capacity_) || !(offset + size <= capacity_)) {
55 return DCAMERA_BAD_VALUE;
56 }
57
58 rangeOffset_ = offset;
59 rangeLength_ = size;
60 return DCAMERA_OK;
61 }
62
SetInt32(const string name,int32_t value)63 void DataBuffer::SetInt32(const string name, int32_t value)
64 {
65 int32Map_[name] = value;
66 }
67
SetInt64(const string name,int64_t value)68 void DataBuffer::SetInt64(const string name, int64_t value)
69 {
70 int64Map_[name] = value;
71 }
72
SetString(const string name,string value)73 void DataBuffer::SetString(const string name, string value)
74 {
75 stringMap_[name] = value;
76 }
77
FindInt32(const string & name,int32_t & value)78 bool DataBuffer::FindInt32(const string& name, int32_t& value)
79 {
80 if (int32Map_.count(name) != 0) {
81 value = int32Map_[name];
82 return true;
83 } else {
84 value = 0;
85 return false;
86 }
87 }
88
FindInt64(const string & name,int64_t & value)89 bool DataBuffer::FindInt64(const string& name, int64_t& value)
90 {
91 if (int64Map_.count(name) != 0) {
92 value = int64Map_[name];
93 return true;
94 } else {
95 value = 0;
96 return false;
97 }
98 }
99
FindString(const string & name,string & value)100 bool DataBuffer::FindString(const string& name, string& value)
101 {
102 if (stringMap_.count(name) != 0) {
103 value = stringMap_[name];
104 return true;
105 } else {
106 value = "";
107 return false;
108 }
109 }
110
~DataBuffer()111 DataBuffer::~DataBuffer()
112 {
113 if (data_ != nullptr) {
114 delete[] data_;
115 data_ = nullptr;
116 }
117 }
118 } // namespace DistributedHardware
119 } // namespace OHOS
120