• 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      if (IsConvertToSolidXml(fileInfo)) {
58          data = FileEntry::FilePath(data).ReplaceExtension(".sxml").GetPath();
59      }
60      data = moduleName_ + SEPARATOR + RESOURCES_DIR + SEPARATOR + \
61          fileInfo.limitKey + SEPARATOR + fileInfo.fileCluster + SEPARATOR + data;
62      if (!resourceItem.SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length())) {
63          cerr << "Error: resource item set data fail, data: " << data << NEW_LINE_PATH << fileInfo.filePath << endl;
64          return false;
65      }
66      return MergeResourceItem(resourceItem);
67  }
68  
GetOutputFilePath(const FileInfo & fileInfo) const69  string GenericCompiler::GetOutputFilePath(const FileInfo &fileInfo) const
70  {
71      string outputFolder = GetOutputFolder(fileInfo);
72      string outputFilePath = FileEntry::FilePath(outputFolder).Append(fileInfo.filename).GetPath();
73      return outputFilePath;
74  }
75  
IsIgnore(const FileInfo & fileInfo) const76  bool GenericCompiler::IsIgnore(const FileInfo &fileInfo) const
77  {
78      if (IsConvertToSolidXml(fileInfo)) {
79          if (HasConvertedToSolidXml(fileInfo)) {
80              return true;
81          }
82          return false;
83      }
84      return ResourceUtil::FileExist(GetOutputFilePath(fileInfo));
85  }
86  
CopyFile(const FileInfo & fileInfo) const87  bool GenericCompiler::CopyFile(const FileInfo &fileInfo) const
88  {
89      if (previewMode_) {
90          return true;
91      }
92      if (IsConvertToSolidXml(fileInfo)) {
93          return true;
94      }
95  
96      string outputFolder = GetOutputFolder(fileInfo);
97      if (!ResourceUtil::CreateDirs(outputFolder)) {
98          return false;
99      }
100      return ResourceUtil::CopyFleInner(fileInfo.filePath, GetOutputFilePath(fileInfo));
101  }
102  
IsConvertToSolidXml(const FileInfo & fileInfo) const103  bool GenericCompiler::IsConvertToSolidXml(const FileInfo &fileInfo) const
104  {
105      if (NeedIfConvertToSolidXml() && IsXmlFile(fileInfo)) {
106          return true;
107      }
108      return false;
109  }
110  }
111  }
112  }