您的位置:首页 > 博客中心 > 互联网 >

mindspore\lite\examples\quick_start_cpp/main部分方法注释

时间:2022-05-11 11:49

mindspore\lite\examples\quick_start_cpp/main部分方法注释

一、代码路径

链接在这

二、代码分块解析

第一块

#include 
#include 
#include 
#include //插入需要的头文件
#include 
#include "include/errorcode.h"
#include "include/model.h"
#include "include/context.h"
#include "include/lite_session.h"

std::string RealPath(const char *path) {//定义一个路径函数
const size_t max = 4096;//设置最大值
if (path == nullptr) {//判断路径是否为空
std::cerr << "path is nullptr" << std::endl;//输出错误信息
return "";
}
if ((strlen(path)) >= max) {//判断路径是否太长
std::cerr << "path is too long" << std::endl;//输出错误信息
return "";
}
auto resolved_path = std::make_unique(max);//用指针初始化一个数组并用auto判断类型
if (resolved_path == nullptr) {//判断该数组是否为空值
std::cerr << "new resolved_path failed" << std::endl;//输出错误信息
return "";
}
#ifdef _WIN32//当win32系统下的情况
char *real_path = _fullpath(resolved_path.get(), path, 1024);//
#else//其他系统情况
char *real_path = realpath(path, resolved_path.get());
#endif
if (real_path == nullptr || strlen(real_path) == 0) {//判断path的是否为空或者无类型
std::cerr << "file path is not valid : " << path << std::endl;//输出文件路径无效
return "";
}
std::string res = resolved_path.get();//定义一个变量res得到路径名
return res;//返回路径
}

代码块二

char *ReadFile(const char *file, size_t *size) {//定义一个读取文件函数
if (file == nullptr) {//判断文件是否为空值
std::cerr << "file is nullptr." << std::endl;//输出错误
return nullptr;
}

std::ifstream ifs(file);//构建一个文件流对象ifs
if (!ifs.good()) {//判断文件流是否正常
    std::cerr << "file: " << file << " is not exist." << std::endl;//输出错误
return nullptr;//返回空值
}

if (!ifs.is_open()) {//判断文件是否能打开
  std::cerr << "file: " << file << " open failed." << std::endl;//输出错误
return nullptr;//返回空值
}

ifs.seekg(0, std::ios::end);//读取文件
*size = ifs.tellg();//用tellg方法读取文件大小
std::unique_ptr buf(new (std::nothrow) char[*size]);
if (buf == nullptr) {//判断buff是否为空
std::cerr << "malloc buf failed, file: " << file << std::endl;
ifs.close();//关闭文件流
return nullptr;//返回空值
}

ifs.seekg(0, std::ios::beg);//把输入流定位到流的开始位置
ifs.read(buf.get(), *size);//写入文件数据
ifs.close();//关闭文件流

return buf.release();//释放函数
}

本类排行

今日推荐

热门手游