Apollo 10.0
自动驾驶开放平台
file.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
21#ifndef CYBER_COMMON_FILE_H_
22#define CYBER_COMMON_FILE_H_
23
24#include <dirent.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30#include <cstdio>
31#include <fstream>
32#include <string>
33#include <vector>
34
35#include "google/protobuf/io/zero_copy_stream_impl.h"
36#include "google/protobuf/text_format.h"
37
38#include "cyber/common/log.h"
39
44namespace apollo {
45namespace cyber {
46namespace common {
47
48// file type: file or directory
50
51bool SetProtoToASCIIFile(const google::protobuf::Message &message,
52 int file_descriptor);
60bool SetProtoToASCIIFile(const google::protobuf::Message &message,
61 const std::string &file_name);
62
70bool SetStringToASCIIFile(const std::string &content,
71 const std::string &file_name);
72
81bool GetProtoFromASCIIFile(const std::string &file_name,
82 google::protobuf::Message *message);
83
91bool SetProtoToBinaryFile(const google::protobuf::Message &message,
92 const std::string &file_name);
93
102bool GetProtoFromBinaryFile(const std::string &file_name,
103 google::protobuf::Message *message);
104
113bool GetProtoFromFile(const std::string &file_name,
114 google::protobuf::Message *message);
115
124bool GetProtoFromJsonFile(const std::string &file_name,
125 google::protobuf::Message *message);
126
133bool GetContent(const std::string &file_name, std::string *content);
134
139std::string GetAbsolutePath(const std::string &prefix,
140 const std::string &relative_path);
141
147bool PathExists(const std::string &path);
148
149bool PathIsAbsolute(const std::string &path);
150
158bool DirectoryExists(const std::string &directory_path);
159
165std::vector<std::string> Glob(const std::string &pattern);
166
173bool CopyFile(const std::string &from, const std::string &to);
174
181bool CopyDir(const std::string &from, const std::string &to);
182
189bool Copy(const std::string &from, const std::string &to);
190
197bool EnsureDirectory(const std::string &directory_path);
198
205bool RemoveAllFiles(const std::string &directory_path);
206
213std::vector<std::string> ListSubPaths(const std::string &directory_path,
214 const unsigned char d_type = DT_DIR);
215
225size_t FindPathByPattern(const std::string &base_path, const std::string &patt,
226 const unsigned char d_type, const bool recursive,
227 std::vector<std::string> *result_list);
228
234std::string GetDirName(const std::string &path);
235
236std::string GetFileName(const std::string &path,
237 const bool remove_extension = false);
238
252bool GetFilePathWithEnv(const std::string &path, const std::string &env_var,
253 std::string *file_path);
254
255std::string GetCurrentPath();
256
257// delete file including file or directory
258bool DeleteFile(const std::string &filename);
259
260bool GetType(const std::string &filename, FileType *type);
261
262bool CreateDir(const std::string &dir);
263
264template <typename T>
265bool LoadConfig(const std::string &relative_path, T *config) {
266 CHECK_NOTNULL(config);
267 // todo: get config base relative path
268 std::string actual_config_path;
269 if (!GetFilePathWithEnv(relative_path, "APOLLO_CONF_PATH",
270 &actual_config_path)) {
271 AERROR << "conf file [" << relative_path
272 << "] is not found in APOLLO_CONF_PATH";
273 return false;
274 }
275 AINFO << "load conf file: " << actual_config_path;
276 return GetProtoFromFile(actual_config_path, config);
277}
278
279} // namespace common
280} // namespace cyber
281} // namespace apollo
282
283#endif // CYBER_COMMON_FILE_H_
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool DeleteFile(const string &filename)
Definition file.cc:509
std::vector< std::string > Glob(const std::string &pattern)
Expand path pattern to matched paths.
Definition file.cc:212
size_t FindPathByPattern(const std::string &base_path, const std::string &patt, const unsigned char d_type, const bool recursive, std::vector< std::string > *result_list)
Find path with pattern
Definition file.cc:374
bool CopyFile(const std::string &from, const std::string &to)
Copy a file.
Definition file.cc:224
bool CreateDir(const string &dir)
Definition file.cc:552
bool GetType(const string &filename, FileType *type)
Definition file.cc:493
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::string GetDirName(const std::string &path)
get directory name of path
Definition file.cc:406
bool CopyDir(const std::string &from, const std::string &to)
Copy a directory.
Definition file.cc:260
std::string GetCurrentPath()
Definition file.cc:488
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
Definition file.cc:436
bool Copy(const std::string &from, const std::string &to)
Copy a file or directory.
Definition file.cc:295
bool SetProtoToBinaryFile(const google::protobuf::Message &message, const std::string &file_name)
Sets the content of the file specified by the file_name to be the binary representation of the input ...
Definition file.cc:111
bool GetProtoFromJsonFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the json file specified by the file_name as ascii representation of protobufs,...
Definition file.cc:149
bool LoadConfig(const std::string &relative_path, T *config)
Definition file.h:265
bool PathIsAbsolute(const std::string &path)
Definition file.cc:200
bool GetProtoFromBinaryFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as binary representation of protobufs,...
Definition file.cc:118
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167
std::vector< std::string > ListSubPaths(const std::string &directory_path, const unsigned char d_type)
List sub-paths.
Definition file.cc:353
class register implement
Definition arena_queue.h:37