mirror of
https://github.com/openharmony/developtools_global_resource_tool.git
synced 2026-07-18 09:14:48 -04:00
b399dc9e64
Signed-off-by: zt147369 <zhangting201@huawei.com>
241 lines
7.0 KiB
C++
241 lines
7.0 KiB
C++
/*
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#ifndef OHOS_RESTOOL_RESOURCE_UTIL_H
|
|
#define OHOS_RESTOOL_RESOURCE_UTIL_H
|
|
|
|
#include <vector>
|
|
#include "file_entry.h"
|
|
#include "json/json.h"
|
|
#include "resource_data.h"
|
|
|
|
namespace OHOS {
|
|
namespace Global {
|
|
namespace Restool {
|
|
class ResourceUtil {
|
|
public:
|
|
/**
|
|
* @brief split the string with given splitter.
|
|
* @param str: input string.
|
|
* @param out: the array of strings computed by splitter.
|
|
* @param splitter: the split string.
|
|
*/
|
|
static void Split(const std::string &str, std::vector<std::string> &out, const std::string &splitter);
|
|
|
|
/**
|
|
* @brief Replace sub-string in string
|
|
* @param sourceStr: The original string to operate on
|
|
* @param oldStr: The string to be replaced
|
|
* @param newStr: The new string used
|
|
*/
|
|
static void StringReplace(std::string &sourceStr, const std::string &oldStr, const std::string &newStr);
|
|
|
|
/**
|
|
* @brief check file exist.
|
|
* @param path: file path.
|
|
* @return true if exist, other false.
|
|
*/
|
|
static bool FileExist(const std::string &path);
|
|
|
|
/**
|
|
* @brief remove all files in the directory.
|
|
* @param path: input directory.
|
|
* @return true if remove success, other false.
|
|
*/
|
|
static bool RmoveAllDir(const std::string &path);
|
|
|
|
/**
|
|
* @brief open json file.
|
|
* @param path: json file path.
|
|
* @param root: json root node
|
|
* @return true if open success, other false.
|
|
*/
|
|
static bool OpenJsonFile(const std::string &path, Json::Value &root);
|
|
|
|
/**
|
|
* @brief save json file.
|
|
* @param path: json file path.
|
|
* @param root: json root node
|
|
* @return true if save success, other false.
|
|
*/
|
|
static bool SaveToJsonFile(const std::string &path, const Json::Value &root);
|
|
|
|
/**
|
|
* @brief get resource type from directory.
|
|
* @param name: directory name.
|
|
* @return resource type.
|
|
*/
|
|
static ResType GetResTypeByDir(const std::string &name);
|
|
|
|
/**
|
|
* @brief ResType to string
|
|
* @param type: ResType
|
|
* @return resource type string.
|
|
*/
|
|
static std::string ResTypeToString(ResType type);
|
|
|
|
/**
|
|
* @brief get id name
|
|
* @param name; id name or file name
|
|
* @param type: ResType
|
|
* @return return id name.
|
|
*/
|
|
static std::string GetIdName(const std::string &name, ResType type);
|
|
|
|
/**
|
|
* @brief compose multi strings to string
|
|
* @param contents: multi strings
|
|
* @param addNull: if true, string length contains '\0'.
|
|
* @return return string, empty if error
|
|
*/
|
|
static std::string ComposeStrings(const std::vector<std::string> &contents, bool addNull = false);
|
|
|
|
/**
|
|
* @brief decompose string to multi strings
|
|
* @param content: string
|
|
* @return return string vector, empty if error
|
|
*/
|
|
static std::vector<std::string> DecomposeStrings(const std::string &content);
|
|
|
|
/**
|
|
* @brief string to ResType
|
|
* @param type: string
|
|
* @return return ResType
|
|
*/
|
|
static ResType GetResTypeFromString(const std::string &type);
|
|
|
|
/**
|
|
* @brief copy file
|
|
* @param src: source file path
|
|
* @param dst: destination file path
|
|
* @return true if success, other false
|
|
*/
|
|
static bool CopyFleInner(const std::string &src, const std::string &dst);
|
|
|
|
/**
|
|
* @brief create directories
|
|
* @param filePath: directory path
|
|
* @return true if success, other false
|
|
*/
|
|
static bool CreateDirs(const std::string &filePath);
|
|
|
|
/**
|
|
* @brief ignore file or directory
|
|
* @param filename: file or directory name
|
|
* @param isFile: ture if is file, other false
|
|
* @return true if ignore, other false
|
|
*/
|
|
static bool IsIgnoreFile(const std::string &filename, bool isFile);
|
|
|
|
/**
|
|
* @brief generate hash string
|
|
* @param key: string
|
|
* @return hash string
|
|
*/
|
|
static std::string GenerateHash(const std::string &key);
|
|
|
|
/**
|
|
* @brief get an absolute pathname
|
|
* @param path pathname
|
|
* @return absolut pathname
|
|
*/
|
|
static std::string RealPath(const std::string &path);
|
|
|
|
/**
|
|
* @brief check the directory is legal
|
|
* @param path pathname
|
|
* @return true is legal, other false;
|
|
*/
|
|
static bool IslegalPath(const std::string &path);
|
|
|
|
/**
|
|
* @brief get an keyParams for limitkey
|
|
* @param keyParams
|
|
* @return limitkey
|
|
*/
|
|
static std::string PaserKeyParam(const std::vector<KeyParam> &keyParams);
|
|
|
|
/**
|
|
* @brief Decimal to hexadecimal string
|
|
* @param int32_t Decimal
|
|
* @return Hexadecimal string
|
|
*/
|
|
static std::string DecToHexStr(const int32_t id);
|
|
|
|
/**
|
|
* @brief Check hexadecimal string
|
|
* @param string Hexadecimal string
|
|
* @return ture Hexadecimal string is legal, other false;
|
|
*/
|
|
static bool CheckHexStr(const std::string &hex);
|
|
|
|
/**
|
|
* @brief get g_contentClusterMap key string
|
|
* @return All restype string
|
|
*/
|
|
static std::string GetAllRestypeString();
|
|
|
|
/**
|
|
* @brief get \base\element dir
|
|
* @param string inputpath
|
|
* @return resource\base\element dir
|
|
*/
|
|
static FileEntry::FilePath GetBaseElementPath(const std::string input);
|
|
|
|
/**
|
|
* @brief get main dir
|
|
* @param string inputpath
|
|
* @return main dir
|
|
*/
|
|
static FileEntry::FilePath GetMainPath(const std::string input);
|
|
|
|
/**
|
|
* @brief Gets the standard size of icons under different qualifier phrases
|
|
* @param keyParams set of qualifiers
|
|
* @param uint32_t index
|
|
* @return standard size of the png
|
|
*/
|
|
static uint32_t GetNormalSize(const std::vector<KeyParam> &keyParams, uint32_t index);
|
|
|
|
/**
|
|
* @brief Check if the Unicode code belongs to the 15 plane or 16 plane
|
|
* @param int unicode
|
|
* @return ture Unicode code belongs to the 15 plane or 16 plane, other false;
|
|
*/
|
|
static bool isUnicodeInPlane15or16(int unicode);
|
|
|
|
/**
|
|
* @brief Remove spaces before and after strings
|
|
* @param str input string
|
|
*/
|
|
static void RemoveSpaces(std::string &str);
|
|
|
|
private:
|
|
enum class IgnoreType {
|
|
IGNORE_FILE,
|
|
IGNORE_DIR,
|
|
IGNORE_ALL
|
|
};
|
|
static const std::map<std::string, IgnoreType> IGNORE_FILE_REGEX;
|
|
static std::string GetLocaleLimitkey(const KeyParam &KeyParam);
|
|
static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam);
|
|
static std::string GetResolutionLimitkey(const KeyParam &KeyParam);
|
|
static std::string GetKeyParamValue(const KeyParam &KeyParam);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
#endif
|