Apollo 11.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 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 >
T UnixToGpsSeconds (T unix_seconds)
 
int64_t UnixToGpsMicroseconds (int64_t unix_microseconds)
 
int64_t UnixToGpsNanoseconds (int64_t unix_nanoseconds)
 
template<typename T >
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.h 第 49 行定义.

函数说明

◆ 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.cc 第 281 行定义.

281 {
282 return DirectoryExists(from) ? CopyDir(from, to) : CopyFile(from, to);
283}
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:250
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.cc 第 250 行定义.

250 {
251 DIR *directory = opendir(from.c_str());
252 if (directory == nullptr) {
253 AERROR << "Cannot open directory " << from;
254 return false;
255 }
256
257 bool ret = true;
258 if (EnsureDirectory(to)) {
259 struct dirent *entry;
260 while ((entry = readdir(directory)) != nullptr) {
261 // skip directory_path/. and directory_path/..
262 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
263 continue;
264 }
265 const std::string sub_path_from = from + "/" + entry->d_name;
266 const std::string sub_path_to = to + "/" + entry->d_name;
267 if (entry->d_type == DT_DIR) {
268 ret &= CopyDir(sub_path_from, sub_path_to);
269 } else {
270 ret &= CopyFile(sub_path_from, sub_path_to);
271 }
272 }
273 } else {
274 AERROR << "Cannot create target directory " << to;
275 ret = false;
276 }
277 closedir(directory);
278 return ret;
279}
#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.cc 第 224 行定义.

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.cc 第 538 行定义.

538 {
539 int ret = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
540 if (ret != 0) {
541 AWARN << "failed to create dir. [dir: " << dir
542 << "] [err: " << strerror(errno) << "]";
543 return false;
544 }
545 return true;
546}

◆ DeleteFile() [1/2]

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

◆ DeleteFile() [2/2]

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

在文件 file.cc 第 495 行定义.

495 {
496 if (!PathExists(filename)) {
497 return true;
498 }
499 FileType type;
500 if (!GetType(filename, &type)) {
501 return false;
502 }
503 if (type == TYPE_FILE) {
504 if (remove(filename.c_str()) != 0) {
505 AERROR << "failed to remove file: " << filename;
506 return false;
507 }
508 return true;
509 }
510 DIR *dir = opendir(filename.c_str());
511 if (dir == nullptr) {
512 AWARN << "failed to opendir: " << filename;
513 return false;
514 }
515 dirent *dir_info = nullptr;
516 while ((dir_info = readdir(dir)) != nullptr) {
517 if (strcmp(dir_info->d_name, ".") == 0 ||
518 strcmp(dir_info->d_name, "..") == 0) {
519 continue;
520 }
521 string temp_file = filename + "/" + string(dir_info->d_name);
522 FileType temp_type;
523 if (!GetType(temp_file, &temp_type)) {
524 AWARN << "failed to get file type: " << temp_file;
525 closedir(dir);
526 return false;
527 }
528 if (type == TYPE_DIR) {
529 DeleteFile(temp_file);
530 }
531 remove(temp_file.c_str());
532 }
533 closedir(dir);
534 remove(filename.c_str());
535 return true;
536}
bool DeleteFile(const string &filename)
Definition file.cc:495
bool GetType(const string &filename, FileType *type)
Definition file.cc:479
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.cc 第 207 行定义.

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.cc 第 285 行定义.

285 {
286 std::string path = directory_path;
287 for (size_t i = 1; i < directory_path.size(); ++i) {
288 if (directory_path[i] == '/') {
289 // Whenever a '/' is encountered, create a temporary view from
290 // the start of the path to the character right before this.
291 path[i] = 0;
292
293 if (mkdir(path.c_str(), S_IRWXU) != 0) {
294 if (errno != EEXIST) {
295 return false;
296 }
297 }
298
299 // Revert the temporary view back to the original.
300 path[i] = '/';
301 }
302 }
303
304 // Make the final (full) directory.
305 if (mkdir(path.c_str(), S_IRWXU) != 0) {
306 if (errno != EEXIST) {
307 return false;
308 }
309 }
310
311 return true;
312}

◆ 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.cc 第 360 行定义.

362 {
363 DIR *directory = opendir(base_path.c_str());
364 size_t result_cnt = 0;
365 if (directory == nullptr) {
366 AWARN << "cannot open directory " << base_path;
367 return result_cnt;
368 }
369 struct dirent *entry;
370 for (entry = readdir(directory); entry != nullptr;
371 entry = readdir(directory)) {
372 std::string entry_path = base_path + "/" + std::string(entry->d_name);
373 // skip `.` and `..`
374 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
375 // TODO(liangjinping): support regex or glob or other pattern mode
376 if ((patt == "" || strcmp(entry->d_name, patt.c_str()) == 0) &&
377 entry->d_type == d_type) {
378 // found
379 result_list->emplace_back(entry_path);
380 ++result_cnt;
381 }
382 if (recursive && (entry->d_type == DT_DIR)) {
383 result_cnt +=
384 FindPathByPattern(entry_path, patt, d_type, recursive, result_list);
385 }
386 }
387 }
388 closedir(directory);
389 return result_cnt;
390}

◆ 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.cc 第 179 行定义.

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.cc 第 167 行定义.

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.cc 第 474 行定义.

474 {
475 char tmp[PATH_MAX];
476 return getcwd(tmp, sizeof(tmp)) ? std::string(tmp) : std::string("");
477}
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.cc 第 392 行定义.

392 {
393 std::string::size_type end = path.rfind('/');
394 if (end == std::string::npos) {
395 // not found, return current dir
396 return ".";
397 }
398 return path.substr(0, end);
399}

◆ GetEnv()

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

在文件 environment.h 第 29 行定义.

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.cc 第 401 行定义.

401 {
402 std::string::size_type start = path.rfind('/');
403 if (start == std::string::npos) {
404 start = 0;
405 } else {
406 // Move to the next char after '/'.
407 ++start;
408 }
409
410 std::string::size_type end = std::string::npos;
411 if (remove_extension) {
412 end = path.rfind('.');
413 // The last '.' is found before last '/', ignore.
414 if (end != std::string::npos && end < start) {
415 end = std::string::npos;
416 }
417 }
418 const auto len = (end != std::string::npos) ? end - start : end;
419 return path.substr(start, len);
420}

◆ 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.cc 第 422 行定义.

423 {
424 if (path.empty()) {
425 return false;
426 }
427 if (PathIsAbsolute(path)) {
428 // an absolute path
429 *file_path = path;
430 return PathExists(path);
431 }
432
433 bool relative_path_exists = false;
434 if (PathExists(path)) {
435 // relative path exists
436 *file_path = path;
437 relative_path_exists = true;
438 }
439 if (path.front() == '.') {
440 // relative path but not exist.
441 return relative_path_exists;
442 }
443
444 const char *var = std::getenv(env_var.c_str());
445 if (var == nullptr) {
446 AWARN << "GetFilePathWithEnv: env " << env_var << " not found.";
447 return relative_path_exists;
448 }
449 std::string env_path = std::string(var);
450
451 // search by environment variable
452 size_t begin = 0;
453 size_t index;
454 do {
455 index = env_path.find(':', begin);
456 auto p = env_path.substr(begin, index - begin);
457 if (p.empty()) {
458 continue;
459 }
460 if (p.back() != '/') {
461 p += '/' + path;
462 } else {
463 p += path;
464 }
465 if (PathExists(p)) {
466 *file_path = p;
467 return true;
468 }
469 begin = index + 1;
470 } while (index != std::string::npos);
471 return relative_path_exists;
472}

◆ 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.cc 第 89 行定义.

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.cc 第 118 行定义.

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.cc 第 132 行定义.

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.cc 第 149 行定义.

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.cc 第 479 行定义.

479 {
480 struct stat stat_buf;
481 if (lstat(filename.c_str(), &stat_buf) != 0) {
482 return false;
483 }
484 if (S_ISDIR(stat_buf.st_mode) != 0) {
485 *type = TYPE_DIR;
486 } else if (S_ISREG(stat_buf.st_mode) != 0) {
487 *type = TYPE_FILE;
488 } else {
489 AWARN << "failed to get type: " << filename;
490 return false;
491 }
492 return true;
493}

◆ 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.cc 第 212 行定义.

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.h 第 95 行定义.

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.h 第 105 行定义.

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.h 第 100 行定义.

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.h 第 110 行定义.

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 ( T  gps_seconds)

在文件 time_conversion.h 第 85 行定义.

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.h 第 27 行定义.

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

◆ 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.cc 第 339 行定义.

340 {
341 std::vector<std::string> result;
342 DIR *directory = opendir(directory_path.c_str());
343 if (directory == nullptr) {
344 AERROR << "Cannot open directory " << directory_path;
345 return result;
346 }
347
348 struct dirent *entry;
349 while ((entry = readdir(directory)) != nullptr) {
350 // Skip "." and "..".
351 if (entry->d_type == d_type && strcmp(entry->d_name, ".") != 0 &&
352 strcmp(entry->d_name, "..") != 0) {
353 result.emplace_back(entry->d_name);
354 }
355 }
356 closedir(directory);
357 return result;
358}

◆ LoadConfig()

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

在文件 file.h 第 265 行定义.

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:422

◆ 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.cc 第 195 行定义.

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.cc 第 200 行定义.

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.cc 第 314 行定义.

314 {
315 DIR *directory = opendir(directory_path.c_str());
316 if (directory == nullptr) {
317 AERROR << "Cannot open directory " << directory_path;
318 return false;
319 }
320
321 struct dirent *file;
322 while ((file = readdir(directory)) != nullptr) {
323 // skip directory_path/. and directory_path/..
324 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) {
325 continue;
326 }
327 // build the path for each file in the folder
328 std::string file_path = directory_path + "/" + file->d_name;
329 if (unlink(file_path.c_str()) < 0) {
330 AERROR << "Fail to remove file " << file_path << ": " << strerror(errno);
331 closedir(directory);
332 return false;
333 }
334 }
335 closedir(directory);
336 return true;
337}

◆ 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.cc 第 59 行定义.

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.cc 第 43 行定义.

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.cc 第 111 行定义.

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.cc 第 69 行定义.

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.h 第 115 行定义.

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.h 第 32 行定义.

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.h 第 125 行定义.

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.h 第 74 行定义.

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.h 第 79 行定义.

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 ( T  unix_seconds)

在文件 time_conversion.h 第 65 行定义.

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.h 第 40 行定义.

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.h 第 62 行定义.

◆ ONE_MILLION

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

在文件 time_conversion.h 第 60 行定义.

◆ UNIX_GPS_DIFF

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

在文件 time_conversion.h 第 58 行定义.