提交 5410bdb0 编写于 作者: X Xiangquan Xiao 提交者: Jiangtao Hu

Perception: Retire redundant FileUtil::GetFileContent. (#1700)

上级 f1033da0
......@@ -17,18 +17,19 @@
#include <string>
#include <utility>
#include "google/protobuf/text_format.h"
#include "cyber/common/log.h"
#include "modules/common/util/file.h"
#include "modules/perception/common/io/io_util.h"
#include "modules/perception/lib/config_manager/config_manager.h"
#include "modules/perception/lib/io/file_util.h"
#include "modules/perception/common/io/io_util.h"
#include "modules/perception/proto/sensor_meta_schema.pb.h"
namespace apollo {
namespace perception {
namespace common {
using apollo::common::util::GetProtoFromASCIIFile;
using apollo::perception::base::BrownCameraDistortionModel;
using apollo::perception::base::SensorInfo;
using apollo::perception::base::SensorOrientation;
......@@ -52,17 +53,10 @@ bool SensorManager::Init() {
std::string file_path = lib::FileUtil::GetAbsolutePath(
config_manager->work_root(), FLAGS_obs_sensor_meta_path);
std::string content;
if (!lib::FileUtil::GetFileContent(file_path, &content)) {
AERROR << "Failed to get SensorManager config path: "
<< FLAGS_obs_sensor_meta_path;
return false;
}
MultiSensorMeta sensor_list_proto;
if (!google::protobuf::TextFormat::ParseFromString(content,
&sensor_list_proto)) {
AERROR << "Invalid MultiSensorMeta file!";
if (!GetProtoFromASCIIFile(file_path, &sensor_list_proto)) {
AERROR << "Invalid MultiSensorMeta file: "
<< FLAGS_obs_sensor_meta_path;
return false;
}
......
......@@ -18,8 +18,7 @@
#include <utility>
#include "cyber/common/log.h"
#include "google/protobuf/text_format.h"
#include "modules/common/util/file.h"
#include "modules/perception/common/perception_gflags.h"
#include "modules/perception/lib/io/file_util.h"
......@@ -27,7 +26,7 @@ namespace apollo {
namespace perception {
namespace lib {
using google::protobuf::TextFormat;
using apollo::common::util::GetProtoFromASCIIFile;
ConfigManager::ConfigManager() {
work_root_ = FLAGS_work_root;
......@@ -70,36 +69,20 @@ bool ConfigManager::InitInternal() {
return false;
}
for (auto &model_config_file : model_config_files) {
std::string content;
if (!FileUtil::GetFileContent(model_config_file, &content)) {
AERROR << "failed to get ConfigManager config path: "
<< model_config_file;
return false;
}
for (const auto& model_config_file : model_config_files) {
ModelConfigFileListProto file_list_proto;
if (!TextFormat::ParseFromString(content, &file_list_proto)) {
if (!GetProtoFromASCIIFile(model_config_file, &file_list_proto)) {
AERROR << "invalid ModelConfigFileListProto file: "
<< FLAGS_config_manager_path;
<< model_config_file;
return false;
}
for (const std::string &model_config_file :
for (const std::string& model_config_path :
file_list_proto.model_config_path()) {
std::string abs_path =
FileUtil::GetAbsolutePath(work_root_, model_config_file);
std::string config_content;
if (!FileUtil::GetFileContent(abs_path, &config_content)) {
AERROR << "failed to get file content: " << abs_path;
return false;
}
const std::string abs_path =
FileUtil::GetAbsolutePath(work_root_, model_config_path);
MultiModelConfigProto multi_model_config_proto;
if (!TextFormat::ParseFromString(config_content,
&multi_model_config_proto)) {
if (!GetProtoFromASCIIFile(abs_path, &multi_model_config_proto)) {
AERROR << "invalid MultiModelConfigProto file: " << abs_path;
return false;
}
......
......@@ -161,41 +161,6 @@ bool FileUtil::CreateDir(const string &dir) {
return true;
}
bool FileUtil::GetFileContent(const string &path, string *content) {
if (content == nullptr) {
return false;
}
int fd = ::open(path.c_str(), O_RDONLY);
if (fd < 0) {
AWARN << "failed to open file: " << path;
return false;
}
struct stat buf;
if (::fstat(fd, &buf) != 0) {
AWARN << "failed to lstat file: " << path;
::close(fd);
return false;
}
size_t fsize = buf.st_size;
content->resize(fsize);
char *data = const_cast<char *>(content->data());
int size = 0;
size_t has_read = 0;
do {
size = ::read(fd, data + has_read, fsize - has_read);
if (size < 0) {
AWARN << "failed to read file: " << path;
::close(fd);
return false;
}
has_read += size;
} while (size > 0);
::close(fd);
return true;
}
bool FileUtil::ReadLines(const string &path, vector<string> *lines) {
std::ifstream fin(path);
if (!fin.good()) {
......
......@@ -49,8 +49,6 @@ class FileUtil {
// create folder
static bool CreateDir(const std::string &dir);
static bool GetFileContent(const std::string &path, std::string *content);
static bool ReadLines(const std::string &path,
std::vector<std::string> *lines);
......
......@@ -179,23 +179,6 @@ TEST(FileUtilTest, TestRenameFile) {
"/apollo/modules/perception/testdata/lib/data2/222.txt"));
}
TEST(FileUtilTest, TestGetFileContent) {
std::string path = "/apollo/modules/perception/testdata/lib/data/1.txt";
std::string content;
EXPECT_FALSE(FileUtil::GetFileContent(path, nullptr));
EXPECT_FALSE(FileUtil::GetFileContent(
"/apollo/modules/perception/testdata/lib/data/2.txt", &content));
EXPECT_TRUE(FileUtil::GetFileContent(path, &content));
ASSERT_EQ(
system("chmod a-r /apollo/modules/perception/testdata/lib/data3/1.txt"),
0);
ASSERT_FALSE(FileUtil::GetFileContent(
"/apollo/modules/perception/testdata/lib/data3/1.txt", &content));
ASSERT_EQ(
system("chmod a+r /apollo/modules/perception/testdata/lib/data3/1.txt"),
0);
}
} // namespace lib
} // namespace perception
} // namespace apollo
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册