1 /*
2  * Copyright (c) 2020 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 "stylemgr/app_style_list.h"
17 
18 namespace OHOS {
19 namespace ACELite {
Reset()20 void AppStyleList::Reset()
21 {
22     AppStyle* next = nullptr;
23     while (firstStyle_ != nullptr) {
24         next = const_cast<AppStyle *>(firstStyle_->GetNext());
25         firstStyle_->Reset();
26         delete firstStyle_;
27         firstStyle_ = next;
28     }
29     lastStyle_ = nullptr;
30 }
31 
AddStyle(AppStyle * newStyle)32 void AppStyleList::AddStyle(AppStyle *newStyle)
33 {
34     if (newStyle == nullptr) {
35         return;
36     }
37 
38     // the first one
39     if (firstStyle_ == nullptr) {
40         firstStyle_ = newStyle;
41         lastStyle_ = newStyle;
42         return;
43     }
44 
45     // fresh new one
46     newStyle->SetPre(lastStyle_);
47     lastStyle_->SetNext(newStyle);
48     lastStyle_ = newStyle;
49 }
50 
GetExistStyle(const char * name) const51 AppStyle *AppStyleList::GetExistStyle(const char *name) const
52 {
53     if (firstStyle_ == nullptr) {
54         return nullptr;
55     }
56     if (name == nullptr || strlen(name) == 0) {
57         return nullptr;
58     }
59     const AppStyle *current = firstStyle_;
60     while (current != nullptr) {
61         // point to next immediately
62         const AppStyle *existCurrentStyle = current;
63         current = existCurrentStyle->GetNext();
64         const char *styleName = existCurrentStyle->GetStyleName();
65 
66         if (styleName == nullptr || strlen(styleName) == 0) {
67             continue;
68         }
69         if ((strlen(styleName) == strlen(name)) && strcmp(styleName, name) == 0) {
70             // exist
71             return const_cast<AppStyle *>(existCurrentStyle);
72         }
73     }
74 
75     return nullptr;
76 }
77 } // namespace ACELite
78 } // namespace OHOS
79