Apollo 11.0
自动驾驶开放平台
system_utility.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
18
19#include <algorithm>
20
21#define BOOST_NO_CXX11_SCOPED_ENUMS
22#include <boost/filesystem.hpp>
23#undef BOOST_NO_CXX11_SCOPED_ENUMS
24
25namespace apollo {
26namespace localization {
27namespace msf {
28
29bool system::IsExists(const std::string& path) {
30 boost::filesystem::path p(path);
31 return boost::filesystem::exists(p);
32}
33
34bool system::IsDirectory(const std::string& path) {
35 boost::filesystem::path p(path);
36 return boost::filesystem::is_directory(p);
37}
38
39bool system::CreateDirectory(const std::string& path) {
40 boost::filesystem::path p(path);
41 return boost::filesystem::create_directory(p);
42}
43
44bool system::GetFileSize(const std::string& path, unsigned int* size) {
45 boost::filesystem::path p(path);
46 if (boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) {
47 *size = static_cast<unsigned int>(boost::filesystem::file_size(p));
48 return true;
49 }
50
51 return false;
52}
53
54bool system::CopyFile(const std::string& src, const std::string& dst,
55 bool is_overwrite) {
56 boost::filesystem::path path_src(src);
57 boost::filesystem::path path_dst(dst);
58 boost::system::error_code error;
59 if (is_overwrite) {
60 boost::filesystem::copy_file(path_src, path_dst,
61 boost::filesystem::copy_option::fail_if_exists,
62 error);
63 } else {
64 boost::filesystem::copy_file(
65 path_src, path_dst, boost::filesystem::copy_option::overwrite_if_exists,
66 error);
67 }
68
69 if (error) {
70 return false;
71 }
72
73 return true;
74}
75
76void system::GetFilesInFolderRecursive(const std::string& folder,
77 const std::string& ext,
78 std::vector<std::string>* ret) {
79 ret->clear();
80 namespace fs = boost::filesystem;
81 if (!fs::exists(folder) || !fs::is_directory(folder)) {
82 return;
83 }
84
85 fs::recursive_directory_iterator it(folder);
86 fs::recursive_directory_iterator endit;
87
88 while (it != endit) {
89 if (fs::is_regular_file(*it) && it->path().extension() == ext) {
90 ret->push_back(it->path().string());
91 }
92 ++it;
93 }
94 std::sort(ret->begin(), ret->end());
95}
96
97void system::GetFilesInFolder(const std::string& folder, const std::string& ext,
98 std::vector<std::string>* ret) {
99 ret->clear();
100 namespace fs = boost::filesystem;
101 if (!fs::exists(folder) || !fs::is_directory(folder)) {
102 return;
103 }
104
105 fs::directory_iterator it(folder);
106 fs::directory_iterator endit;
107
108 while (it != endit) {
109 if (fs::is_regular_file(*it) && it->path().extension() == ext) {
110 ret->push_back(it->path().string());
111 }
112 ++it;
113 }
114 std::sort(ret->begin(), ret->end());
115}
116
117void system::GetFoldersInFolder(const std::string& folder,
118 std::vector<std::string>* ret) {
119 ret->clear();
120 namespace fs = boost::filesystem;
121 if (!fs::exists(folder) || !fs::is_directory(folder)) {
122 return;
123 }
124
125 fs::directory_iterator it(folder);
126 fs::directory_iterator endit;
127
128 while (it != endit) {
129 if (fs::is_directory(*it)) {
130 ret->push_back(it->path().string());
131 }
132 ++it;
133 }
134 std::sort(ret->begin(), ret->end());
135}
136
137} // namespace msf
138} // namespace localization
139} // namespace apollo
static bool GetFileSize(const std::string &path, unsigned int *size)
Get the size of a file.
static bool CreateDirectory(const std::string &path)
Try to create a directory.
static bool CopyFile(const std::string &src, const std::string &dst, bool is_overwrite=true)
Copy the file.
static void GetFilesInFolder(const std::string &folder, const std::string &ext, std::vector< std::string > *ret)
get list of files end with ext in folder.
static void GetFoldersInFolder(const std::string &folder, std::vector< std::string > *ret)
Get list of folders in folder.
static bool IsExists(const std::string &path)
Determine if the file or directory exists.
static bool IsDirectory(const std::string &path)
Determine if the path is a directory.
static void GetFilesInFolderRecursive(const std::string &folder, const std::string &ext, std::vector< std::string > *ret)
get list of files end with ext in folder.
class register implement
Definition arena_queue.h:37