filesystem_helpers.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
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 */
17 
18 
19 #ifndef FILESYSTEM_HELPERS_HPP
20 #define FILESYSTEM_HELPERS_HPP
21 
22 #include <iostream>
23 #include <qi/session.hpp>
24 
25 #include <boost/filesystem.hpp>
26 #include <boost/filesystem/operations.hpp>
27 #include <boost/algorithm/string/replace.hpp>
28 
29 #ifdef CATKIN_BUILD
30 #include <ros/package.h>
31 #endif
32 
33 namespace naoqi
34 {
35 namespace helpers
36 {
37 namespace filesystem
38 {
39 
40 static const long folderMaximumSize = 2000000000;
41 
42 inline void getFoldersize(std::string rootFolder, long& file_size){
43  boost::algorithm::replace_all(rootFolder, "\\\\", "\\");
44  boost::filesystem::path folderPath(rootFolder);
45  if (boost::filesystem::exists(folderPath)){
46  boost::filesystem::directory_iterator end_itr;
47 
48  for (boost::filesystem::directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte )
49  {
50  boost::filesystem::path filePath(dirIte->path());
51  try{
52  if (!boost::filesystem::is_directory(dirIte->status()) )
53  {
54  file_size = file_size + boost::filesystem::file_size(filePath);
55  }else{
56  getFoldersize(filePath.string(),file_size);
57  }
58  }catch(std::exception& e){
59  std::cout << e.what() << std::endl;
60  }
61  }
62  }
63 }
64 
65 inline void getFiles(const boost::filesystem::path& root, const std::string& ext, std::vector<boost::filesystem::path>& ret)
66 {
67  if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return;
68 
69  boost::filesystem::recursive_directory_iterator it(root);
70  boost::filesystem::recursive_directory_iterator endit;
71 
72  while(it != endit)
73  {
74  if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext)
75  {
76  ret.push_back(it->path().filename());
77  }
78  ++it;
79  }
80 }
81 
82 inline void getFilesSize(const boost::filesystem::path& root, long& file_size)
83 {
84  std::vector<boost::filesystem::path> files_path;
85  getFiles(root, ".bag", files_path);
86  for (std::vector<boost::filesystem::path>::const_iterator it=files_path.begin();
87  it!=files_path.end(); it++)
88  {
89  try{
90  file_size = file_size + boost::filesystem::file_size(*it);
91  }catch(std::exception& e){
92  std::cout << e.what() << std::endl;
93  }
94  }
95 }
96 
98 static const std::string boot_config_file_name = "boot_config.json";
99 inline std::string& getBootConfigFile()
100 {
101 #ifdef CATKIN_BUILD
102  static std::string path = ros::package::getPath("naoqi_driver")+"/share/"+boot_config_file_name;
103  std::cout << "found a catkin prefix " << path << std::endl;
104  return path;
105 #else
106  static std::string path = qi::path::findData( "/", boot_config_file_name );
107  std::cout << "found a qibuild path " << path << std::endl;
108  return path;
109 #endif
110 }
111 
112 /* URDF loader */
113 inline std::string& getURDF( std::string filename )
114 {
115 #ifdef CATKIN_BUILD
116  static std::string path = ros::package::getPath("naoqi_driver")+"/share/urdf/"+filename;
117  std::cout << "found a catkin URDF " << path << std::endl;
118  return path;
119 #else
120  static std::string path = qi::path::findData( "/urdf/", filename );
121  std::cout << "found a qibuild URDF " << path << std::endl;
122  return path;
123 #endif
124 }
125 
126 } // filesystem
127 } //helpers
128 } // naoqi
129 
130 #endif
std::string & getURDF(std::string filename)
void getFiles(const boost::filesystem::path &root, const std::string &ext, std::vector< boost::filesystem::path > &ret)
static const long folderMaximumSize
ROSLIB_DECL std::string getPath(const std::string &package_name)
void getFoldersize(std::string rootFolder, long &file_size)
static const std::string boot_config_file_name
void getFilesSize(const boost::filesystem::path &root, long &file_size)


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26