• 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 "xml_key_node.h"
17 #include<algorithm>
18 #include<fstream>
19 #include<iostream>
20 #include<vector>
21 
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
26 const map<XmlKeyNode::KeyType, string> XmlKeyNode::KEY_TO_FILE_NAME = {
27     {KeyType::NODE, "nodes.key" },
28     {KeyType::ATTRIBUTE, "attributes.key" },
29     {KeyType::CONSTANT, "constants.key" },
30     {KeyType::CONTENT, "contents.key" }
31 };
XmlKeyNode()32 XmlKeyNode::XmlKeyNode() : keyId_(0)
33 {
34     PushKey("");
35 }
36 
~XmlKeyNode()37 XmlKeyNode::~XmlKeyNode()
38 {
39 }
40 
PushKey(const string & name)41 int32_t XmlKeyNode::PushKey(const string &name)
42 {
43     auto result = keyMap_.emplace(name, keyId_);
44     if (result.second) {
45         return keyId_++;
46     }
47     return result.first->second;
48 }
49 
SaveToFile(const string & filePath) const50 bool XmlKeyNode::SaveToFile(const string &filePath) const
51 {
52     ofstream out(filePath, ofstream::out | ofstream::binary);
53     if (!out.is_open()) {
54         cerr << "Error: open fail," << filePath << endl;
55         return false;
56     }
57 
58     vector<pair<string, int32_t>> sets(keyMap_.begin(), keyMap_.end());
59     sort(sets.begin(), sets.end(), [](const auto &a, const auto &b) {
60         return a.second < b.second;
61     });
62 
63     char null = 0;
64     for (const auto &iter : sets) {
65         out.write(reinterpret_cast<const char *>(iter.first.c_str()), iter.first.length());
66         out.write(&null, sizeof(char));
67     }
68     return true;
69 }
70 
LoadFromFile(const string & filePath)71 bool XmlKeyNode::LoadFromFile(const string &filePath)
72 {
73     return LoadFromFile(filePath, nullptr);
74 }
75 
LoadFromFile(const std::string & filePath,RefParser parser)76 bool XmlKeyNode::LoadFromFile(const std::string &filePath, RefParser parser)
77 {
78     ifstream in(filePath, ifstream::in | ifstream::binary);
79     if (!in.is_open()) {
80         cerr << "Error: open fail," << filePath << endl;
81         return false;
82     }
83 
84     string inputLine;
85     getline(in, inputLine);
86     if (inputLine.empty()) {
87         return true;
88     }
89 
90     string::size_type offset = 0;
91     while (offset < inputLine.length()) {
92         string item(inputLine.c_str() + offset);
93         offset = offset + item.length() + sizeof(char);
94         if (parser && !parser(item)) {
95             return false;
96         }
97         PushKey(item);
98     }
99     return true;
100 }
101 
GetKeyValue(int32_t keyId,std::string & value) const102 bool XmlKeyNode::GetKeyValue(int32_t keyId, std::string &value) const
103 {
104     auto result = find_if(keyMap_.begin(), keyMap_.end(), [&keyId](const auto &iter) {
105         return keyId == iter.second;
106     });
107     if (result == keyMap_.end()) {
108         return false;
109     }
110     value = result->first;
111     return true;
112 }
113 }
114 }
115 }