Apollo  6.0
Open source self driving car software
file.h
Go to the documentation of this file.
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 
44 namespace apollo {
45 namespace cyber {
46 namespace common {
47 
48 // file type: file or directory
50 
51 bool SetProtoToASCIIFile(const google::protobuf::Message &message,
52  int file_descriptor);
60 bool SetProtoToASCIIFile(const google::protobuf::Message &message,
61  const std::string &file_name);
62 
71 bool GetProtoFromASCIIFile(const std::string &file_name,
72  google::protobuf::Message *message);
73 
81 bool SetProtoToBinaryFile(const google::protobuf::Message &message,
82  const std::string &file_name);
83 
92 bool GetProtoFromBinaryFile(const std::string &file_name,
93  google::protobuf::Message *message);
94 
103 bool GetProtoFromFile(const std::string &file_name,
104  google::protobuf::Message *message);
105 
112 bool GetContent(const std::string &file_name, std::string *content);
113 
118 std::string GetAbsolutePath(const std::string &prefix,
119  const std::string &relative_path);
120 
126 bool PathExists(const std::string &path);
127 
135 bool DirectoryExists(const std::string &directory_path);
136 
142 std::vector<std::string> Glob(const std::string &pattern);
143 
150 bool CopyFile(const std::string &from, const std::string &to);
151 
158 bool CopyDir(const std::string &from, const std::string &to);
159 
166 bool Copy(const std::string &from, const std::string &to);
167 
174 bool EnsureDirectory(const std::string &directory_path);
175 
182 bool RemoveAllFiles(const std::string &directory_path);
183 
190 std::vector<std::string> ListSubPaths(const std::string &directory_path,
191  const unsigned char d_type = DT_DIR);
192 
193 std::string GetFileName(const std::string &path,
194  const bool remove_extension = false);
195 
196 std::string GetCurrentPath();
197 
198 // delete file including file or directory
199 bool DeleteFile(const std::string &filename);
200 
201 bool GetType(const std::string &filename, FileType *type);
202 
203 bool CreateDir(const std::string &dir);
204 } // namespace common
205 } // namespace cyber
206 } // namespace apollo
207 
208 #endif // CYBER_COMMON_FILE_H_
std::vector< std::string > ListSubPaths(const std::string &directory_path, const unsigned char d_type=DT_DIR)
List sub-paths.
bool DeleteFile(const std::string &filename)
bool RemoveAllFiles(const std::string &directory_path)
Remove all the files under a specified directory. Note that sub-directories are NOT affected...
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool PathExists(const std::string &path)
Check if the path exists.
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
bool CopyFile(const std::string &from, const std::string &to)
Copy a file.
bool SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
std::string GetFileName(const std::string &path, const bool remove_extension=false)
bool GetType(const std::string &filename, FileType *type)
bool CopyDir(const std::string &from, const std::string &to)
Copy a directory.
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, and merges the parsed content to the proto.
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
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 ...
bool GetProtoFromASCIIFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as ascii representation of protobufs...
FileType
Definition: file.h:49
bool CreateDir(const std::string &dir)
Definition: file.h:49
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists. If not, recursively create the dir...
bool Copy(const std::string &from, const std::string &to)
Copy a file or directory.
std::string GetCurrentPath()
std::vector< std::string > Glob(const std::string &pattern)
Expand path pattern to matched paths.
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...