* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* ------------------------------------------------------------------------- */
#ifndef DATA_UTILS_H
#define DATA_UTILS_H
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include "acl/acl.h"
#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args)
#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args)
#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args)
#define RETURN_IF_NOT_SUCCESS(expr, format, action) \
do { \
if ((expr) != ACL_SUCCESS) { \
ERROR_LOG(format); \
action; \
} \
} while (0)
#define CALL_RT(x) \
if (auto ret = (x) != 0) { \
std::cout << "[ERROR] Failed to exec acl api " << #x << ", result: " << ret << std::endl; \
return -1; \
} else { \
std::cout << "[INFO] Succeeded to exec acl api " << #x << std::endl; \
}
#define IF_NOT_SUCCESS_RETURN_FALSE(expr, format, action) \
do { \
if ((expr) != ACL_SUCCESS) { \
ERROR_LOG(format); \
action; \
return false; \
} \
} while (0)
#define ACL_ERROR_LOG(fmt, args...) fprintf(stdout, "[ACL_ERROR] " fmt "\n", ##args)
* @brief Get Acl Recent Error Message.
*/
void GetRecentErrMsg()
{
const char *aclRecentErrMsg = nullptr;
aclRecentErrMsg = aclGetRecentErrMsg();
if (aclRecentErrMsg != nullptr) {
ACL_ERROR_LOG("%s", aclRecentErrMsg);
} else {
ACL_ERROR_LOG("Failed to get recent error message.");
}
}
* @brief Read data from file
* @param [in] filePath: file path
* @param [out] fileSize: file size
* @return read result
*/
bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize)
{
struct stat sBuf;
int fileStatus = stat(filePath.data(), &sBuf);
if (fileStatus == -1) {
ERROR_LOG("Failed to get file.");
return false;
}
if (S_ISREG(sBuf.st_mode) == 0) {
ERROR_LOG("%s is not a file, please enter a file", filePath.c_str());
return false;
}
std::ifstream file;
file.open(filePath, std::ios::binary);
if (!file.is_open()) {
ERROR_LOG("Open file failed. path = %s", filePath.c_str());
return false;
}
std::filebuf *buf = file.rdbuf();
size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in);
if (size == 0) {
ERROR_LOG("file size is 0");
file.close();
return false;
}
if (size > bufferSize) {
ERROR_LOG("file size is larger than buffer size");
file.close();
return false;
}
buf->pubseekpos(0, std::ios::in);
buf->sgetn(static_cast<char *>(buffer), size);
fileSize = size;
file.close();
return true;
}
* @brief Write data to file
* @param [in] filePath: file path
* @param [in] buffer: data to write to file
* @param [in] size: size to write
* @return write result
*/
bool WriteFile(const std::string &filePath, const void *buffer, size_t size)
{
if (buffer == nullptr) {
ERROR_LOG("Write file failed. buffer is nullptr");
return false;
}
int fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE);
if (fd < 0) {
ERROR_LOG("Open file failed. path = %s", filePath.c_str());
return false;
}
auto writeSize = write(fd, buffer, size);
(void) close(fd);
if (writeSize != size) {
ERROR_LOG("Write file Failed.");
return false;
}
return true;
}
#endif