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