Apollo 10.0
自动驾驶开放平台
apollo::cyber::common 命名空间参考

class  GlobalData
 

枚举

enum  FileType { TYPE_FILE , TYPE_DIR }
 

函数

std::string GetEnv (const std::string &var_name, const std::string &default_value="")
 
const std::string WorkRoot ()
 
bool SetProtoToASCIIFile (const google::protobuf::Message &message, int file_descriptor)
 
bool SetProtoToASCIIFile (const google::protobuf::Message &message, const std::string &file_name)
 Sets the content of the file specified by the file_name to be the ascii representation of the input protobuf.
 
bool SetStringToASCIIFile (const std::string &content, const std::string &file_name)
 Sets the content of the file specified by the file_name to be the ascii representation of the input string.
 
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, and merges the parsed content to the proto.
 
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 protobuf.
 
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, and merges the parsed content to the proto.
 
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 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, and merges the parsed content to the proto.
 
bool GetContent (const std::string &file_name, std::string *content)
 Get file content as string.
 
std::string GetAbsolutePath (const std::string &prefix, const std::string &relative_path)
 Get absolute path by concatenating prefix and relative_path.
 
bool PathExists (const std::string &path)
 Check if the path exists.
 
bool PathIsAbsolute (const std::string &path)
 
bool DirectoryExists (const std::string &directory_path)
 Check if the directory specified by directory_path exists and is indeed a directory.
 
std::vector< std::string > Glob (const std::string &pattern)
 Expand path pattern to matched paths.
 
bool CopyFile (const std::string &from, const std::string &to)
 Copy a file.
 
bool IsValidPath (const std::string &path)
 
bool CopyDir (const std::string &from, const std::string &to)
 Copy a directory.
 
bool Copy (const std::string &from, const std::string &to)
 Copy a file or directory.
 
bool EnsureDirectory (const std::string &directory_path)
 Check if a specified directory specified by directory_path exists.
 
bool RemoveAllFiles (const std::string &directory_path)
 Remove all the files under a specified directory.
 
std::vector< std::string > ListSubPaths (const std::string &directory_path, const unsigned char d_type=DT_DIR)
 List sub-paths.
 
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
 
std::string GetDirName (const std::string &path)
 get directory name of path
 
std::string GetFileName (const std::string &path, const bool remove_extension)
 
bool GetFilePathWithEnv (const std::string &path, const std::string &env_var, std::string *file_path)
 get file path, judgement priority:
 
std::string GetCurrentPath ()
 
bool GetType (const string &filename, FileType *type)
 
bool DeleteFile (const string &filename)
 
bool CreateDir (const string &dir)
 
bool DeleteFile (const std::string &filename)
 
bool GetType (const std::string &filename, FileType *type)
 
bool CreateDir (const std::string &dir)
 
template<typename T >
bool LoadConfig (const std::string &relative_path, T *config)
 
template<typename T >
UnixToGpsSeconds (T unix_seconds)
 
int64_t UnixToGpsMicroseconds (int64_t unix_microseconds)
 
int64_t UnixToGpsNanoseconds (int64_t unix_nanoseconds)
 
template<typename T >
GpsToUnixSeconds (T gps_seconds)
 
int64_t GpsToUnixMicroseconds (int64_t gps_microseconds)
 
int64_t GpsToUnixNanoseconds (int64_t gps_nanoseconds)
 
uint64_t GpsToUnixMicroseconds (uint64_t gps_microseconds)
 
uint64_t GpsToUnixNanoseconds (uint64_t gps_nanoseconds)
 
uint64_t StringToUnixSeconds (const std::string &time_str, const std::string &format_str="%Y-%m-%d %H:%M:%S")
 
std::string UnixSecondsToString (uint64_t unix_seconds, const std::string &format_str="%Y-%m-%d-%H:%M:%S")
 
std::size_t Hash (const std::string &key)
 
template<typename Enum >
auto ToInt (Enum const value) -> typename std::underlying_type< Enum >::type
 

变量

constexpr int32_t UNIX_GPS_DIFF = 315964800
 
constexpr int64_t ONE_MILLION = 1000000L
 
constexpr int64_t ONE_BILLION = 1000000000L
 

枚举类型说明

◆ FileType

枚举值
TYPE_FILE 
TYPE_DIR 

在文件 file.h49 行定义.

函数说明

◆ Copy()

bool apollo::cyber::common::Copy ( const std::string &  from,
const std::string &  to 
)

Copy a file or directory.

参数
fromThe path to copy from.
toThe path to copy to.
返回
If the action is successful.

在文件 file.cc295 行定义.

295 {
296 return DirectoryExists(from) ? CopyDir(from, to) : CopyFile(from, to);
297}
bool CopyFile(const std::string &from, const std::string &to)
Copy a file.
Definition file.cc:224
bool CopyDir(const std::string &from, const std::string &to)
Copy a directory.
Definition file.cc:260
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
Definition file.cc:207

◆ CopyDir()

bool apollo::cyber::common::CopyDir ( const std::string &  from,
const std::string &  to 
)

Copy a directory.

参数
fromThe path to copy from.
toThe path to copy to.
返回
If the action is successful.

在文件 file.cc260 行定义.

260 {
261 DIR *directory = opendir(from.c_str());
262 if (directory == nullptr) {
263 AERROR << "Cannot open directory " << from;
264 return false;
265 }
266 if (!IsValidPath(from) || !IsValidPath(to)) {
267 AERROR << "invalid path format: " << from << " to " << to;
268 return false;
269 }
270
271 bool ret = true;
272 if (EnsureDirectory(to)) {
273 struct dirent *entry;
274 while ((entry = readdir(directory)) != nullptr) {
275 // skip directory_path/. and directory_path/..
276 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
277 continue;
278 }
279 const std::string sub_path_from = from + "/" + entry->d_name;
280 const std::string sub_path_to = to + "/" + entry->d_name;
281 if (entry->d_type == DT_DIR) {
282 ret &= CopyDir(sub_path_from, sub_path_to);
283 } else {
284 ret &= CopyFile(sub_path_from, sub_path_to);
285 }
286 }
287 } else {
288 AERROR << "Cannot create target directory " << to;
289 ret = false;
290 }
291 closedir(directory);
292 return ret;
293}
#define AERROR
Definition log.h:44

◆ CopyFile()

bool apollo::cyber::common::CopyFile ( const std::string &  from,
const std::string &  to 
)

Copy a file.

参数
fromThe file path to copy from.
toThe file path to copy to.
返回
If the action is successful.

在文件 file.cc224 行定义.

224 {
225 std::ifstream src(from, std::ios::binary);
226 if (!src) {
227 AWARN << "Source path could not be normally opened: " << from;
228 std::string command = "cp -r " + from + " " + to;
229 ADEBUG << command;
230 const int ret = std::system(command.c_str());
231 if (ret == 0) {
232 ADEBUG << "Copy success, command returns " << ret;
233 return true;
234 } else {
235 ADEBUG << "Copy error, command returns " << ret;
236 return false;
237 }
238 }
239
240 std::ofstream dst(to, std::ios::binary);
241 if (!dst) {
242 AERROR << "Target path is not writable: " << to;
243 return false;
244 }
245
246 dst << src.rdbuf();
247 return true;
248}
#define ADEBUG
Definition log.h:41
#define AWARN
Definition log.h:43

◆ CreateDir() [1/2]

bool apollo::cyber::common::CreateDir ( const std::string &  dir)

◆ CreateDir() [2/2]

bool apollo::cyber::common::CreateDir ( const string &  dir)

在文件 file.cc552 行定义.

552 {
553 int ret = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
554 if (ret != 0) {
555 AWARN << "failed to create dir. [dir: " << dir
556 << "] [err: " << strerror(errno) << "]";
557 return false;
558 }
559 return true;
560}

◆ DeleteFile() [1/2]

bool apollo::cyber::common::DeleteFile ( const std::string &  filename)

◆ DeleteFile() [2/2]

bool apollo::cyber::common::DeleteFile ( const string &  filename)

在文件 file.cc509 行定义.

509 {
510 if (!PathExists(filename)) {
511 return true;
512 }
513 FileType type;
514 if (!GetType(filename, &type)) {
515 return false;
516 }
517 if (type == TYPE_FILE) {
518 if (remove(filename.c_str()) != 0) {
519 AERROR << "failed to remove file: " << filename;
520 return false;
521 }
522 return true;
523 }
524 DIR *dir = opendir(filename.c_str());
525 if (dir == nullptr) {
526 AWARN << "failed to opendir: " << filename;
527 return false;
528 }
529 dirent *dir_info = nullptr;
530 while ((dir_info = readdir(dir)) != nullptr) {
531 if (strcmp(dir_info->d_name, ".") == 0 ||
532 strcmp(dir_info->d_name, "..") == 0) {
533 continue;
534 }
535 string temp_file = filename + "/" + string(dir_info->d_name);
536 FileType temp_type;
537 if (!GetType(temp_file, &temp_type)) {
538 AWARN << "failed to get file type: " << temp_file;
539 closedir(dir);
540 return false;
541 }
542 if (type == TYPE_DIR) {
543 DeleteFile(temp_file);
544 }
545 remove(temp_file.c_str());
546 }
547 closedir(dir);
548 remove(filename.c_str());
549 return true;
550}
bool DeleteFile(const string &filename)
Definition file.cc:509
bool GetType(const string &filename, FileType *type)
Definition file.cc:493
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195

◆ DirectoryExists()

bool apollo::cyber::common::DirectoryExists ( const std::string &  directory_path)

Check if the directory specified by directory_path exists and is indeed a directory.

参数
directory_pathDirectory path.
返回
If the directory specified by directory_path exists and is indeed a directory.

在文件 file.cc207 行定义.

207 {
208 struct stat info;
209 return stat(directory_path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
210}

◆ EnsureDirectory()

bool apollo::cyber::common::EnsureDirectory ( const std::string &  directory_path)

Check if a specified directory specified by directory_path exists.

If not, recursively create the directory (and its parents).

参数
directory_pathDirectory path.
返回
If the directory does exist or its creation is successful.

在文件 file.cc299 行定义.

299 {
300 std::string path = directory_path;
301 for (size_t i = 1; i < directory_path.size(); ++i) {
302 if (directory_path[i] == '/') {
303 // Whenever a '/' is encountered, create a temporary view from
304 // the start of the path to the character right before this.
305 path[i] = 0;
306
307 if (mkdir(path.c_str(), S_IRWXU) != 0) {
308 if (errno != EEXIST) {
309 return false;
310 }
311 }
312
313 // Revert the temporary view back to the original.
314 path[i] = '/';
315 }
316 }
317
318 // Make the final (full) directory.
319 if (mkdir(path.c_str(), S_IRWXU) != 0) {
320 if (errno != EEXIST) {
321 return false;
322 }
323 }
324
325 return true;
326}

◆ FindPathByPattern()

size_t apollo::cyber::common::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

参数
base_pathsearch root
pattpattern to compare with entry->d_name for filter
d_typeentry type for filter
recursivesearch directory recursively
result_lista vector reference for storing the search result
返回
the result count

在文件 file.cc374 行定义.

376 {
377 DIR *directory = opendir(base_path.c_str());
378 size_t result_cnt = 0;
379 if (directory == nullptr) {
380 AWARN << "cannot open directory " << base_path;
381 return result_cnt;
382 }
383 struct dirent *entry;
384 for (entry = readdir(directory); entry != nullptr;
385 entry = readdir(directory)) {
386 std::string entry_path = base_path + "/" + std::string(entry->d_name);
387 // skip `.` and `..`
388 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
389 // TODO(liangjinping): support regex or glob or other pattern mode
390 if ((patt == "" || strcmp(entry->d_name, patt.c_str()) == 0) &&
391 entry->d_type == d_type) {
392 // found
393 result_list->emplace_back(entry_path);
394 ++result_cnt;
395 }
396 if (recursive && (entry->d_type == DT_DIR)) {
397 result_cnt +=
398 FindPathByPattern(entry_path, patt, d_type, recursive, result_list);
399 }
400 }
401 }
402 closedir(directory);
403 return result_cnt;
404}

◆ GetAbsolutePath()

std::string apollo::cyber::common::GetAbsolutePath ( const std::string &  prefix,
const std::string &  relative_path 
)

Get absolute path by concatenating prefix and relative_path.

返回
The absolute path.

在文件 file.cc179 行定义.

180 {
181 if (relative_path.empty()) {
182 return prefix;
183 }
184 // If prefix is empty or relative_path is already absolute.
185 if (prefix.empty() || relative_path.front() == '/') {
186 return relative_path;
187 }
188
189 if (prefix.back() == '/') {
190 return prefix + relative_path;
191 }
192 return prefix + "/" + relative_path;
193}

◆ GetContent()

bool apollo::cyber::common::GetContent ( const std::string &  file_name,
std::string *  content 
)

Get file content as string.

参数
file_nameThe name of the file to read content.
contentThe file content.
返回
If the action is successful.

在文件 file.cc167 行定义.

167 {
168 std::ifstream fin(file_name);
169 if (!fin) {
170 return false;
171 }
172
173 std::stringstream str_stream;
174 str_stream << fin.rdbuf();
175 *content = str_stream.str();
176 return true;
177}

◆ GetCurrentPath()

std::string apollo::cyber::common::GetCurrentPath ( )

在文件 file.cc488 行定义.

488 {
489 char tmp[PATH_MAX];
490 return getcwd(tmp, sizeof(tmp)) ? std::string(tmp) : std::string("");
491}
Definition future.h:29

◆ GetDirName()

std::string apollo::cyber::common::GetDirName ( const std::string &  path)

get directory name of path

参数
path
返回
dirname of path

在文件 file.cc406 行定义.

406 {
407 std::string::size_type end = path.rfind('/');
408 if (end == std::string::npos) {
409 // not found, return current dir
410 return ".";
411 }
412 return path.substr(0, end);
413}

◆ GetEnv()

std::string apollo::cyber::common::GetEnv ( const std::string &  var_name,
const std::string &  default_value = "" 
)
inline

在文件 environment.h29 行定义.

30 {
31 const char* var = std::getenv(var_name.c_str());
32 if (var == nullptr) {
33 AWARN << "Environment variable [" << var_name << "] not set, fallback to "
34 << default_value;
35 return default_value;
36 }
37 return std::string(var);
38}

◆ GetFileName()

std::string apollo::cyber::common::GetFileName ( const std::string &  path,
const bool  remove_extension 
)

在文件 file.cc415 行定义.

415 {
416 std::string::size_type start = path.rfind('/');
417 if (start == std::string::npos) {
418 start = 0;
419 } else {
420 // Move to the next char after '/'.
421 ++start;
422 }
423
424 std::string::size_type end = std::string::npos;
425 if (remove_extension) {
426 end = path.rfind('.');
427 // The last '.' is found before last '/', ignore.
428 if (end != std::string::npos && end < start) {
429 end = std::string::npos;
430 }
431 }
432 const auto len = (end != std::string::npos) ? end - start : end;
433 return path.substr(start, len);
434}

◆ GetFilePathWithEnv()

bool apollo::cyber::common::GetFilePathWithEnv ( const std::string &  path,
const std::string &  env_var,
std::string *  file_path 
)

get file path, judgement priority:

  1. absolute path.
  2. relative path starts with '.'.
  3. add environment variable prefix before the path.
  4. a common relative path.
参数
pathinput file path string.
env_varenvironment var string.
file_paththe output file path.
返回
if no valid path found, return false.

在文件 file.cc436 行定义.

437 {
438 if (path.empty()) {
439 return false;
440 }
441 if (PathIsAbsolute(path)) {
442 // an absolute path
443 *file_path = path;
444 return PathExists(path);
445 }
446
447 bool relative_path_exists = false;
448 if (PathExists(path)) {
449 // relative path exists
450 *file_path = path;
451 relative_path_exists = true;
452 }
453 if (path.front() == '.') {
454 // relative path but not exist.
455 return relative_path_exists;
456 }
457
458 const char *var = std::getenv(env_var.c_str());
459 if (var == nullptr) {
460 AWARN << "GetFilePathWithEnv: env " << env_var << " not found.";
461 return relative_path_exists;
462 }
463 std::string env_path = std::string(var);
464
465 // search by environment variable
466 size_t begin = 0;
467 size_t index;
468 do {
469 index = env_path.find(':', begin);
470 auto p = env_path.substr(begin, index - begin);
471 if (p.empty()) {
472 continue;
473 }
474 if (p.back() != '/') {
475 p += '/' + path;
476 } else {
477 p += path;
478 }
479 if (PathExists(p)) {
480 *file_path = p;
481 return true;
482 }
483 begin = index + 1;
484 } while (index != std::string::npos);
485 return relative_path_exists;
486}

◆ GetProtoFromASCIIFile()

bool apollo::cyber::common::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, and merges the parsed content to the proto.

参数
file_nameThe name of the file to parse whose content.
messageThe proto to carry the parsed content in the specified file.
返回
If the action is successful.

在文件 file.cc89 行定义.

90 {
91 using google::protobuf::TextFormat;
92 using google::protobuf::io::FileInputStream;
93 using google::protobuf::io::ZeroCopyInputStream;
94 int file_descriptor = open(file_name.c_str(), O_RDONLY);
95 if (file_descriptor < 0) {
96 AERROR << "Failed to open file " << file_name << " in text mode.";
97 // Failed to open;
98 return false;
99 }
100
101 ZeroCopyInputStream *input = new FileInputStream(file_descriptor);
102 bool success = TextFormat::Parse(input, message);
103 if (!success) {
104 AERROR << "Failed to parse file " << file_name << " as text proto.";
105 }
106 delete input;
107 close(file_descriptor);
108 return success;
109}

◆ GetProtoFromBinaryFile()

bool apollo::cyber::common::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, and merges the parsed content to the proto.

参数
file_nameThe name of the file to parse whose content.
messageThe proto to carry the parsed content in the specified file.
返回
If the action is successful.

在文件 file.cc118 行定义.

119 {
120 std::fstream input(file_name, std::ios::in | std::ios::binary);
121 if (!input.good()) {
122 AERROR << "Failed to open file " << file_name << " in binary mode.";
123 return false;
124 }
125 if (!message->ParseFromIstream(&input)) {
126 AERROR << "Failed to parse file " << file_name << " as binary proto.";
127 return false;
128 }
129 return true;
130}

◆ GetProtoFromFile()

bool apollo::cyber::common::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.

参数
file_nameThe name of the file to parse whose content.
messageThe proto to carry the parsed content in the specified file.
返回
If the action is successful.

在文件 file.cc132 行定义.

133 {
134 if (!PathExists(file_name)) {
135 AERROR << "File [" << file_name << "] does not exist! ";
136 return false;
137 }
138 // Try the binary parser first if it's much likely a binary proto.
139 static const std::string kBinExt = ".bin";
140 if (std::equal(kBinExt.rbegin(), kBinExt.rend(), file_name.rbegin())) {
141 return GetProtoFromBinaryFile(file_name, message) ||
142 GetProtoFromASCIIFile(file_name, message);
143 }
144
145 return GetProtoFromASCIIFile(file_name, message) ||
146 GetProtoFromBinaryFile(file_name, message);
147}

◆ GetProtoFromJsonFile()

bool apollo::cyber::common::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, and merges the parsed content to the proto.

参数
file_nameThe name of the file to parse whose content.
messageThe proto to carry the parsed content in the specified file.
返回
If the action is successful.

在文件 file.cc149 行定义.

150 {
151 using google::protobuf::util::JsonParseOptions;
152 using google::protobuf::util::JsonStringToMessage;
153 std::ifstream ifs(file_name);
154 if (!ifs.is_open()) {
155 AERROR << "Failed to open file " << file_name;
156 return false;
157 }
158 nlohmann::json Json;
159 ifs >> Json;
160 ifs.close();
161 JsonParseOptions options;
162 options.ignore_unknown_fields = true;
163 google::protobuf::util::Status dump_status;
164 return (JsonStringToMessage(Json.dump(), message, options).ok());
165}
nlohmann::json Json

◆ GetType() [1/2]

bool apollo::cyber::common::GetType ( const std::string &  filename,
FileType type 
)

◆ GetType() [2/2]

bool apollo::cyber::common::GetType ( const string &  filename,
FileType type 
)

在文件 file.cc493 行定义.

493 {
494 struct stat stat_buf;
495 if (lstat(filename.c_str(), &stat_buf) != 0) {
496 return false;
497 }
498 if (S_ISDIR(stat_buf.st_mode) != 0) {
499 *type = TYPE_DIR;
500 } else if (S_ISREG(stat_buf.st_mode) != 0) {
501 *type = TYPE_FILE;
502 } else {
503 AWARN << "failed to get type: " << filename;
504 return false;
505 }
506 return true;
507}

◆ Glob()

std::vector< std::string > apollo::cyber::common::Glob ( const std::string &  pattern)

Expand path pattern to matched paths.

参数
patternPath pattern, which may contain wildcards [?*].
返回
Matched path list.

在文件 file.cc212 行定义.

212 {
213 glob_t globs = {};
214 std::vector<std::string> results;
215 if (glob(pattern.c_str(), GLOB_TILDE, nullptr, &globs) == 0) {
216 for (size_t i = 0; i < globs.gl_pathc; ++i) {
217 results.emplace_back(globs.gl_pathv[i]);
218 }
219 }
220 globfree(&globs);
221 return results;
222}

◆ GpsToUnixMicroseconds() [1/2]

int64_t apollo::cyber::common::GpsToUnixMicroseconds ( int64_t  gps_microseconds)
inline

在文件 time_conversion.h95 行定义.

95 {
96 return GpsToUnixSeconds(gps_microseconds / ONE_MILLION) * ONE_MILLION +
97 gps_microseconds % ONE_MILLION;
98}
constexpr int64_t ONE_MILLION
T GpsToUnixSeconds(T gps_seconds)

◆ GpsToUnixMicroseconds() [2/2]

uint64_t apollo::cyber::common::GpsToUnixMicroseconds ( uint64_t  gps_microseconds)
inline

在文件 time_conversion.h105 行定义.

105 {
106 return GpsToUnixSeconds(gps_microseconds / ONE_MILLION) * ONE_MILLION +
107 gps_microseconds % ONE_MILLION;
108}

◆ GpsToUnixNanoseconds() [1/2]

int64_t apollo::cyber::common::GpsToUnixNanoseconds ( int64_t  gps_nanoseconds)
inline

在文件 time_conversion.h100 行定义.

100 {
101 return GpsToUnixSeconds(gps_nanoseconds / ONE_BILLION) * ONE_BILLION +
102 gps_nanoseconds % ONE_BILLION;
103}
constexpr int64_t ONE_BILLION

◆ GpsToUnixNanoseconds() [2/2]

uint64_t apollo::cyber::common::GpsToUnixNanoseconds ( uint64_t  gps_nanoseconds)
inline

在文件 time_conversion.h110 行定义.

110 {
111 return GpsToUnixSeconds(gps_nanoseconds / ONE_BILLION) * ONE_BILLION +
112 gps_nanoseconds % ONE_BILLION;
113}

◆ GpsToUnixSeconds()

template<typename T >
T apollo::cyber::common::GpsToUnixSeconds ( gps_seconds)

在文件 time_conversion.h85 行定义.

85 {
86 for (size_t i = 0; i < LEAP_SECONDS.size(); ++i) {
87 T result = gps_seconds + (UNIX_GPS_DIFF - LEAP_SECONDS[i].second);
88 if ((int32_t)result >= LEAP_SECONDS[i].first) {
89 return result;
90 }
91 }
92 return static_cast<T>(0);
93}
constexpr int32_t UNIX_GPS_DIFF

◆ Hash()

std::size_t apollo::cyber::common::Hash ( const std::string &  key)
inline

在文件 util.h27 行定义.

27 {
28 return std::hash<std::string>{}(key);
29}

◆ IsValidPath()

bool apollo::cyber::common::IsValidPath ( const std::string &  path)

在文件 file.cc250 行定义.

250 {
251 const std::string illegal_chars = "\0<>:\"|?*;";
252 for (char ch : path) {
253 if (illegal_chars.find(ch) != std::string::npos) {
254 return false;
255 }
256 }
257 return true;
258}

◆ ListSubPaths()

std::vector< std::string > apollo::cyber::common::ListSubPaths ( const std::string &  directory_path,
const unsigned char  d_type = DT_DIR 
)

List sub-paths.

参数
directory_pathDirectory path.
d_typeSub-path type, DT_DIR for directory, or DT_REG for file.
返回
A vector of sub-paths, without the directory_path prefix.

在文件 file.cc353 行定义.

354 {
355 std::vector<std::string> result;
356 DIR *directory = opendir(directory_path.c_str());
357 if (directory == nullptr) {
358 AERROR << "Cannot open directory " << directory_path;
359 return result;
360 }
361
362 struct dirent *entry;
363 while ((entry = readdir(directory)) != nullptr) {
364 // Skip "." and "..".
365 if (entry->d_type == d_type && strcmp(entry->d_name, ".") != 0 &&
366 strcmp(entry->d_name, "..") != 0) {
367 result.emplace_back(entry->d_name);
368 }
369 }
370 closedir(directory);
371 return result;
372}

◆ LoadConfig()

template<typename T >
bool apollo::cyber::common::LoadConfig ( const std::string &  relative_path,
T *  config 
)

在文件 file.h265 行定义.

265 {
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}
#define AINFO
Definition log.h:42
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
Definition file.cc:436

◆ PathExists()

bool apollo::cyber::common::PathExists ( const std::string &  path)

Check if the path exists.

参数
patha file name, such as /a/b/c.txt
返回
If the path exists.

在文件 file.cc195 行定义.

195 {
196 struct stat info;
197 return stat(path.c_str(), &info) == 0;
198}

◆ PathIsAbsolute()

bool apollo::cyber::common::PathIsAbsolute ( const std::string &  path)

在文件 file.cc200 行定义.

200 {
201 if (path.empty()) {
202 return false;
203 }
204 return path.front() == '/';
205}

◆ RemoveAllFiles()

bool apollo::cyber::common::RemoveAllFiles ( const std::string &  directory_path)

Remove all the files under a specified directory.

Note that sub-directories are NOT affected.

参数
directory_pathDirectory path.
返回
If the action is successful.

在文件 file.cc328 行定义.

328 {
329 DIR *directory = opendir(directory_path.c_str());
330 if (directory == nullptr) {
331 AERROR << "Cannot open directory " << directory_path;
332 return false;
333 }
334
335 struct dirent *file;
336 while ((file = readdir(directory)) != nullptr) {
337 // skip directory_path/. and directory_path/..
338 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) {
339 continue;
340 }
341 // build the path for each file in the folder
342 std::string file_path = directory_path + "/" + file->d_name;
343 if (unlink(file_path.c_str()) < 0) {
344 AERROR << "Fail to remove file " << file_path << ": " << strerror(errno);
345 closedir(directory);
346 return false;
347 }
348 }
349 closedir(directory);
350 return true;
351}

◆ SetProtoToASCIIFile() [1/2]

bool apollo::cyber::common::SetProtoToASCIIFile ( const google::protobuf::Message &  message,
const std::string &  file_name 
)

Sets the content of the file specified by the file_name to be the ascii representation of the input protobuf.

参数
messageThe proto to output to the specified file.
file_nameThe name of the target file to set the content.
返回
If the action is successful.

在文件 file.cc59 行定义.

60 {
61 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
62 if (fd < 0) {
63 AERROR << "Unable to open file " << file_name << " to write.";
64 return false;
65 }
66 return SetProtoToASCIIFile(message, fd);
67}

◆ SetProtoToASCIIFile() [2/2]

bool apollo::cyber::common::SetProtoToASCIIFile ( const google::protobuf::Message &  message,
int  file_descriptor 
)

在文件 file.cc43 行定义.

44 {
45 using google::protobuf::TextFormat;
46 using google::protobuf::io::FileOutputStream;
47 using google::protobuf::io::ZeroCopyOutputStream;
48 if (file_descriptor < 0) {
49 AERROR << "Invalid file descriptor.";
50 return false;
51 }
52 ZeroCopyOutputStream *output = new FileOutputStream(file_descriptor);
53 bool success = TextFormat::Print(message, output);
54 delete output;
55 close(file_descriptor);
56 return success;
57}

◆ SetProtoToBinaryFile()

bool apollo::cyber::common::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 protobuf.

参数
messageThe proto to output to the specified file.
file_nameThe name of the target file to set the content.
返回
If the action is successful.

在文件 file.cc111 行定义.

112 {
113 std::fstream output(file_name,
114 std::ios::out | std::ios::trunc | std::ios::binary);
115 return message.SerializeToOstream(&output);
116}

◆ SetStringToASCIIFile()

bool apollo::cyber::common::SetStringToASCIIFile ( const std::string &  content,
const std::string &  file_name 
)

Sets the content of the file specified by the file_name to be the ascii representation of the input string.

参数
contentThe string to output to the specified file.
file_nameThe name of the target file to set the content.
返回
If the action is successful.

在文件 file.cc69 行定义.

70 {
71 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
72 if (fd < 0) {
73 AERROR << "Unable to open file " << file_name << " to write.";
74 return false;
75 }
76 // Write the string data to the file
77 ssize_t bytes_written = write(fd, content.c_str(), content.size());
78 if (bytes_written < 0) {
79 AERROR << "Failed to write to file.";
80 close(fd); // Ensure the file descriptor is closed even on error
81 return false;
82 }
83
84 close(fd); // Close the file descriptor
85
86 return true;
87}

◆ StringToUnixSeconds()

uint64_t apollo::cyber::common::StringToUnixSeconds ( const std::string &  time_str,
const std::string &  format_str = "%Y-%m-%d %H:%M:%S" 
)
inline

在文件 time_conversion.h115 行定义.

117 :%M:%S") {
118 tm tmp_time;
119 strptime(time_str.c_str(), format_str.c_str(), &tmp_time);
120 tmp_time.tm_isdst = -1;
121 time_t time = mktime(&tmp_time);
122 return static_cast<uint64_t>(time);
123}

◆ ToInt()

template<typename Enum >
auto apollo::cyber::common::ToInt ( Enum const  value) -> typename std::underlying_type<Enum>::type

在文件 util.h32 行定义.

32 {
33 return static_cast<typename std::underlying_type<Enum>::type>(value);
34}

◆ UnixSecondsToString()

std::string apollo::cyber::common::UnixSecondsToString ( uint64_t  unix_seconds,
const std::string &  format_str = "%Y-%m-%d-%H:%M:%S" 
)
inline

在文件 time_conversion.h125 行定义.

127 :%M:%S") {
128 std::time_t t = unix_seconds;
129 struct tm ptm;
130 struct tm* ret = localtime_r(&t, &ptm);
131 if (ret == nullptr) {
132 return std::string("");
133 }
134 uint32_t length = 64;
135 std::vector<char> buff(length, '\0');
136 strftime(buff.data(), length, format_str.c_str(), ret);
137 return std::string(buff.data());
138}

◆ UnixToGpsMicroseconds()

int64_t apollo::cyber::common::UnixToGpsMicroseconds ( int64_t  unix_microseconds)
inline

在文件 time_conversion.h74 行定义.

74 {
75 return UnixToGpsSeconds(unix_microseconds / ONE_MILLION) * ONE_MILLION +
76 unix_microseconds % ONE_MILLION;
77}
T UnixToGpsSeconds(T unix_seconds)

◆ UnixToGpsNanoseconds()

int64_t apollo::cyber::common::UnixToGpsNanoseconds ( int64_t  unix_nanoseconds)
inline

在文件 time_conversion.h79 行定义.

79 {
80 return UnixToGpsSeconds(unix_nanoseconds / ONE_BILLION) * ONE_BILLION +
81 unix_nanoseconds % ONE_BILLION;
82}

◆ UnixToGpsSeconds()

template<typename T >
T apollo::cyber::common::UnixToGpsSeconds ( unix_seconds)

在文件 time_conversion.h65 行定义.

65 {
66 for (size_t i = 0; i < LEAP_SECONDS.size(); ++i) {
67 if (unix_seconds >= LEAP_SECONDS[i].first) {
68 return unix_seconds - (UNIX_GPS_DIFF - LEAP_SECONDS[i].second);
69 }
70 }
71 return static_cast<T>(0);
72}

◆ WorkRoot()

const std::string apollo::cyber::common::WorkRoot ( )
inline

在文件 environment.h40 行定义.

40 {
41 std::string work_root = GetEnv("CYBER_PATH");
42 if (work_root.empty()) {
43 work_root = "/apollo/cyber";
44 }
45 return work_root;
46}
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29

变量说明

◆ ONE_BILLION

constexpr int64_t apollo::cyber::common::ONE_BILLION = 1000000000L
constexpr

在文件 time_conversion.h62 行定义.

◆ ONE_MILLION

constexpr int64_t apollo::cyber::common::ONE_MILLION = 1000000L
constexpr

在文件 time_conversion.h60 行定义.

◆ UNIX_GPS_DIFF

constexpr int32_t apollo::cyber::common::UNIX_GPS_DIFF = 315964800
constexpr

在文件 time_conversion.h58 行定义.