1 /*
2  * Copyright (c) 2024-2024 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 "dash_com_attrs_elements.h"
17 #include "dash_mpd_util.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace Plugins {
22 namespace HttpPlugin {
DashComAttrsElements()23 DashComAttrsElements::DashComAttrsElements()
24 {
25     for (int32_t index = 0; index < DASH_COMMON_ATTR_NUM; index++) {
26         commonAttr_[index].attr_.assign(commonAttrs_[index]);
27         commonAttr_[index].val_.assign("");
28     }
29 }
30 
~DashComAttrsElements()31 DashComAttrsElements::~DashComAttrsElements() {}
32 
ParseAttrs(std::shared_ptr<XmlParser> xmlParser,std::shared_ptr<XmlElement> rootElement)33 void DashComAttrsElements::ParseAttrs(std::shared_ptr<XmlParser> xmlParser, std::shared_ptr<XmlElement> rootElement)
34 {
35     if (xmlParser != nullptr) {
36         for (uint32_t index = 0; index < DASH_COMMON_ATTR_NUM; index++) {
37             xmlParser->GetAttribute(rootElement, commonAttr_[index].attr_, commonAttr_[index].val_);
38         }
39     }
40 }
41 
GetAttr(const std::string & attrName,std::string & sAttrVal) const42 void DashComAttrsElements::GetAttr(const std::string &attrName, std::string &sAttrVal) const
43 {
44     uint32_t index = DashGetAttrIndex(attrName, commonAttrs_, DASH_COMMON_ATTR_NUM);
45     if (index < DASH_COMMON_ATTR_NUM) {
46         if (commonAttr_[index].val_.length() > 0) {
47             sAttrVal = commonAttr_[index].val_;
48         } else {
49             sAttrVal.assign("");
50         }
51     }
52 }
53 
GetAttr(const std::string & attrName,uint32_t & uiAttrVal) const54 void DashComAttrsElements::GetAttr(const std::string &attrName, uint32_t &uiAttrVal) const
55 {
56     uint32_t index = DashGetAttrIndex(attrName, commonAttrs_, DASH_COMMON_ATTR_NUM);
57     if (index < DASH_COMMON_ATTR_NUM) {
58         if (commonAttr_[index].val_.length() > 0) {
59             uiAttrVal = static_cast<uint32_t>(std::atoll(commonAttr_[index].val_.c_str()));
60         } else {
61             uiAttrVal = 0;
62         }
63     }
64 }
65 
GetAttr(const std::string & attrName,int32_t & iAttrVal) const66 void DashComAttrsElements::GetAttr(const std::string &attrName, int32_t &iAttrVal) const
67 {
68     uint32_t index = DashGetAttrIndex(attrName, commonAttrs_, DASH_COMMON_ATTR_NUM);
69     if (index < DASH_COMMON_ATTR_NUM) {
70         if (commonAttr_[index].val_.length() > 0) {
71             iAttrVal = static_cast<int32_t>(std::atoll(commonAttr_[index].val_.c_str()));
72         } else {
73             iAttrVal = 0;
74         }
75     }
76 }
77 
GetAttr(const std::string & attrName,uint64_t & ullAttrVal) const78 void DashComAttrsElements::GetAttr(const std::string &attrName, uint64_t &ullAttrVal) const
79 {
80     uint32_t index = DashGetAttrIndex(attrName, commonAttrs_, DASH_COMMON_ATTR_NUM);
81     if (index < DASH_COMMON_ATTR_NUM) {
82         if (commonAttr_[index].val_.length() > 0) {
83             ullAttrVal = static_cast<uint64_t>(std::atoll(commonAttr_[index].val_.c_str()));
84         } else {
85             ullAttrVal = 0;
86         }
87     }
88 }
89 
GetAttr(const std::string & attrName,double & dAttrVal) const90 void DashComAttrsElements::GetAttr(const std::string &attrName, double &dAttrVal) const
91 {
92     uint32_t index = DashGetAttrIndex(attrName, commonAttrs_, DASH_COMMON_ATTR_NUM);
93     if (index < DASH_COMMON_ATTR_NUM) {
94         if (commonAttr_[index].val_.length() > 0) {
95             dAttrVal = atof(commonAttr_[index].val_.c_str());
96         } else {
97             dAttrVal = 0.0;
98         }
99     }
100 }
101 } // namespace HttpPluginLite
102 } // namespace Plugin
103 } // namespace Media
104 } // namespace OHOS
105