• 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 "generic_compiler.h"
17 #include <iostream>
18 #include "file_entry.h"
19 #include "resource_util.h"
20 #include "restool_errors.h"
21 
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
GenericCompiler(ResType type,const string & output)26 GenericCompiler::GenericCompiler(ResType type, const string &output)
27     : IResourceCompiler(type, output)
28 {
29 }
~GenericCompiler()30 GenericCompiler::~GenericCompiler()
31 {
32 }
33 
CompileSingleFile(const FileInfo & fileInfo)34 uint32_t GenericCompiler::CompileSingleFile(const FileInfo &fileInfo)
35 {
36     if (IsIgnore(fileInfo)) {
37         return RESTOOL_SUCCESS;
38     }
39 
40     if (!CopyFile(fileInfo)) {
41         return RESTOOL_ERROR;
42     }
43 
44     if (!PostFile(fileInfo)) {
45         return RESTOOL_ERROR;
46     }
47     return RESTOOL_SUCCESS;
48 }
49 
PostFile(const FileInfo & fileInfo)50 bool GenericCompiler::PostFile(const FileInfo &fileInfo)
51 {
52     ResourceItem resourceItem(fileInfo.filename, fileInfo.keyParams, type_);
53     resourceItem.SetFilePath(fileInfo.filePath);
54     resourceItem.SetLimitKey(fileInfo.limitKey);
55 
56     string data = fileInfo.filename;
57     data = moduleName_ + SEPARATOR + RESOURCES_DIR + SEPARATOR + \
58         fileInfo.limitKey + SEPARATOR + fileInfo.fileCluster + SEPARATOR + data;
59     if (!resourceItem.SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length())) {
60         cerr << "Error: resource item set data fail, data: " << data << NEW_LINE_PATH << fileInfo.filePath << endl;
61         return false;
62     }
63     return MergeResourceItem(resourceItem);
64 }
65 
GetOutputFilePath(const FileInfo & fileInfo) const66 string GenericCompiler::GetOutputFilePath(const FileInfo &fileInfo) const
67 {
68     string outputFolder = GetOutputFolder(fileInfo);
69     string outputFilePath = FileEntry::FilePath(outputFolder).Append(fileInfo.filename).GetPath();
70     return outputFilePath;
71 }
72 
IsIgnore(const FileInfo & fileInfo) const73 bool GenericCompiler::IsIgnore(const FileInfo &fileInfo) const
74 {
75     return ResourceUtil::FileExist(GetOutputFilePath(fileInfo));
76 }
77 
CopyFile(const FileInfo & fileInfo) const78 bool GenericCompiler::CopyFile(const FileInfo &fileInfo) const
79 {
80     string outputFolder = GetOutputFolder(fileInfo);
81     if (!ResourceUtil::CreateDirs(outputFolder)) {
82         return false;
83     }
84     return ResourceUtil::CopyFleInner(fileInfo.filePath, GetOutputFilePath(fileInfo));
85 }
86 }
87 }
88 }