|
- /**
- * Copyright 2019-2020 Huawei Technologies 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 UTIL_UTIL_H_
- #define UTIL_UTIL_H_
-
- #include <limits.h>
- #include <string>
-
- #include "framework/common/debug/ge_log.h"
- #include "mmpa/mmpa_api.h"
-
- // For propagating errors when calling a function.
- #define GE_RETURN_IF_ERROR(expr) \
- do { \
- const ::ge::Status _status = (expr); \
- if (_status) return _status; \
- } while (0)
-
- // If expr is not true, print the log and return the specified status
- #define GE_CHK_BOOL_RET_STATUS(expr, _status, ...) \
- do { \
- bool b = (expr); \
- if (!b) { \
- GELOGE(ge::FAILED, __VA_ARGS__); \
- return _status; \
- } \
- } while (0);
-
- // If expr is not true, print the log and execute a custom statement
- #define GE_CHK_BOOL_EXEC(expr, exec_expr, ...) \
- { \
- bool b = (expr); \
- if (!b) { \
- GELOGE(ge::FAILED, __VA_ARGS__); \
- exec_expr; \
- } \
- }
-
- // If expr is true, print logs and execute custom statements
- #define GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(expr, exec_expr, ...) \
- { \
- bool b = (expr); \
- if (b) { \
- GELOGE(ge::FAILED, __VA_ARGS__); \
- exec_expr; \
- } \
- }
-
- // If expr is not SUCCESS, return the same value
- #define GE_CHK_STATUS_RET_NOLOG(expr) \
- do { \
- const ge::Status _status = (expr); \
- if (_status != ge::SUCCESS) { \
- return _status; \
- } \
- } while (0);
-
- #define GE_RETURN_WITH_LOG_IF_ERROR(expr, ...) \
- do { \
- const ::ge::Status _status = (expr); \
- if (_status) { \
- GELOGE(ge::FAILED, __VA_ARGS__); \
- return _status; \
- } \
- } while (0)
-
- // If expr is not SUCCESS, print the log and execute a custom statement
- #define GE_CHK_STATUS_EXEC(expr, exec_expr, ...) \
- do { \
- const ge::Status _status = (expr); \
- GE_CHK_BOOL_EXEC(_status == SUCCESS, exec_expr, __VA_ARGS__); \
- } while (0);
-
- // Check if the parameter is null. If yes, return PARAM_INVALID and record the error
- #define GE_CHECK_NOTNULL(val) \
- do { \
- if (val == nullptr) { \
- GELOGE(ge::FAILED, "param[%s] must not be null.", #val); \
- return ge::PARAM_INVALID; \
- } \
- } while (0)
-
- // If expr is true, execute exec_expr without printing logs
- #define GE_IF_BOOL_EXEC(expr, exec_expr) \
- { \
- if (expr) { \
- exec_expr; \
- } \
- }
-
- namespace ge {
- ///
- /// @ingroup domi_common
- /// @brief Recursively Creating a Directory
- /// @param [in] directory_path Path, which can be a multi-level directory.
- /// @return 0 success
- /// @return -1 fail
- ///
- extern int CreateDirectory(const std::string &directory_path);
-
- ///
- /// @ingroup domi_common
- /// @brief Obtains the current time string.
- /// @return Time character string in the format : %Y%m%d%H%M%S, eg: 20171011083555
- ///
- std::string CurrentTimeInStr();
-
- ///
- /// @ingroup domi_common
- /// @brief Absolute path for obtaining files.
- /// @param [in] path of input file
- /// @param [out] Absolute path of a file. If the absolute path cannot be obtained, an empty string is returned
- ///
- std::string RealPath(const char *path);
-
- ///
- /// @ingroup domi_common
- /// @brief Check whether the specified input file path is valid.
- /// 1. The specified path cannot be empty.
- /// 2. The path can be converted to an absolute path.
- /// 3. The file path exists and is readable.
- /// @param [in] file_path path of input file
- /// @param [out] result
- ///
- bool CheckInputPathValid(const std::string &file_path, const std::string &atc_param = "");
-
- ///
- /// @ingroup domi_common
- /// @brief Checks whether the specified output file path is valid.
- /// @param [in] file_path path of output file
- /// @param [out] result
- ///
- bool CheckOutputPathValid(const std::string &file_path, const std::string &atc_param = "");
-
- ///
- /// @ingroup domi_common
- /// @brief Check whether the file path meets the whitelist verification requirements.
- /// @param [in] filePath file path
- /// @param [out] result
- ///
- bool ValidateStr(const std::string &filePath, const std::string &mode);
-
- bool GetNameFromFileName(const std::string &file_name, std::string &base_name);
- } // namespace ge
- #endif // INC_FRAMEWORK_COMMON_UTIL_H_
|