1 /*
2  * Copyright (c) 2022 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 #ifndef OHOS_RESTOOL_RESOURCE_APPEND_H
17 #define OHOS_RESTOOL_RESOURCE_APPEND_H
18 
19 #include <fstream>
20 #include "cmd_parser.h"
21 #include "factory_resource_compiler.h"
22 #include "file_entry.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace Restool {
27 class ResourceAppend {
28 public:
29     ResourceAppend(const PackageParser &packageParser);
~ResourceAppend()30     virtual ~ResourceAppend() {};
31     uint32_t Append();
32     uint32_t Combine();
GetItems() const33     const std::map<int64_t, std::vector<std::shared_ptr<ResourceItem>>> GetItems() const
34     {
35         return items_;
36     }
37 
38 private:
39     bool Combine(const std::string &folderPath);
40     bool ScanResources(const std::string &resourcePath, const std::string &outputPath);
41     bool ScanSubResources(const FileEntry entry, const std::string &resourcePath,
42                             const std::string &outputPath);
43     bool ScanSubLimitkeyResources(const FileEntry entry, const std::string &resourcePath,
44                             const std::string &outputPath);
45     bool ScanIegalResources(const std::string &resourcePath, const std::string &outputPath);
46     bool ScanLimitKey(const std::unique_ptr<FileEntry> &entry, const std::string &limitKey,
47                     const std::string outputPath);
48     bool ScanFiles(const std::unique_ptr<FileEntry> &entry, const DirectoryInfo &directoryInfo,
49                     const std::string &outputPath);
50     bool ScanFile(const FileInfo &fileInfo, const std::string &outputPath);
51     bool ScanSingleFile(const std::string &filePath, const std::string &outputPath);
52     bool WriteFileInner(std::ostringstream &outStream, const std::string &outputPath) const;
53     bool WriteResourceItem(const ResourceItem &resourceItem, std::ostringstream &out);
54     bool LoadResourceItem(const std::string &filePath);
55     bool ScanRawFilesOrResFiles(const std::string &path, const std::string &outputPath, const std::string &limit);
56     bool WriteRawFilesOrResFiles(const std::string &filePath, const std::string &outputPath, const std::string &limit);
57     bool Push(const std::shared_ptr<ResourceItem> &resourceItem);
58     void AddRef(const std::shared_ptr<ResourceItem> &resourceItem);
59     bool LoadResourceItemFromMem(const char buffer[], int32_t length);
60     std::string ParseString(const char buffer[], int32_t length, int32_t &offset) const;
61     int32_t ParseInt32(const char buffer[], int32_t length, int32_t &offset) const;
62     bool ParseRef();
63     bool CheckModuleResourceItem(const std::shared_ptr<ResourceItem> &resourceItem, int64_t id);
64     bool IsBaseIdDefined(const FileInfo &fileInfo);
65 #ifdef __WIN32
66     bool LoadResourceItemWin(const std::string &filePath);
67 #endif
68     void CheckAllItems(std::vector<std::pair<ResType, std::string>> &noBaseResource);
69     const PackageParser &packageParser_;
70     std::map<int64_t, std::vector<std::shared_ptr<ResourceItem>>> items_;
71     std::map<int64_t, std::vector<std::shared_ptr<ResourceItem>>> itemsForModule_;
72     std::vector<std::shared_ptr<ResourceItem>> refs_;
73 };
74 }
75 }
76 }
77 #endif