• 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 "header.h"
17 #include<algorithm>
18 #include<fstream>
19 #include<iomanip>
20 #include<iostream>
21 
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
26 const std::string Header::LICENSE_HEADER = "/*\n\
27  * Copyright (c) 2023 Huawei Device Co., Ltd.\n\
28  * Licensed under the Apache License, Version 2.0 (the \"License\");\n\
29  * you may not use this file except in compliance with the License.\n\
30  * You may obtain a copy of the License at\n\
31  *\n\
32  *     http://www.apache.org/licenses/LICENSE-2.0\n\
33  *\n\
34  * Unless required by applicable law or agreed to in writing, software\n\
35  * distributed under the License is distributed on an \"AS IS\" BASIS,\n\
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\
37  * See the License for the specific language governing permissions and\n\
38  * limitations under the License.\n\
39  */\n";
40 
Header(const string & outputPath)41 Header::Header(const string &outputPath) : outputPath_(outputPath)
42 {
43 }
44 
~Header()45 Header::~Header()
46 {
47 }
48 
Create(HandleHeaderTail headerHandler,HandleBody bodyHandler,HandleHeaderTail tailHandler) const49 uint32_t Header::Create(HandleHeaderTail headerHandler, HandleBody bodyHandler, HandleHeaderTail tailHandler) const
50 {
51     IdWorker &idWorker = IdWorker::GetInstance();
52     vector<IdWorker::ResourceId> resourceIds = idWorker.GetHeaderId();
53 
54     ofstream out(outputPath_, ofstream::out | ofstream::binary);
55     if (!out.is_open()) {
56         cerr << "Error: open failed '" << outputPath_ << "', reason: " << strerror(errno) << endl;
57         return RESTOOL_ERROR;
58     }
59 
60     stringstream buffer;
61     headerHandler(buffer);
62     for (const auto &resourceId : resourceIds) {
63         bodyHandler(buffer, resourceId);
64     }
65     tailHandler(buffer);
66     out << buffer.rdbuf();
67     out.close();
68     return RESTOOL_SUCCESS;
69 }
70 }
71 }
72 }