码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-06-05 17:43:32      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:初始化   cstring   代码   size   cts   algorithm   art   开始   log   

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

一、代码路径

链接在这 https://forgeplus.trustie.net/projects/Huawei_Technology/mindspore/tree/master/mindspore/lite/examples/quick_start_cpp/main.cc

二、代码分块解析

第一块

#include <algorithm>
#include <random>
#include <iostream>
#include <fstream>//插入需要的头文件
#include <cstring>
#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<char[]>(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<char[]> 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();//释放函数
}

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

标签:初始化   cstring   代码   size   cts   algorithm   art   开始   log   

原文地址:https://www.cnblogs.com/WangLiYuan87/p/14851289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!