filesystem_helpers.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Aldebaran
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 
00019 #ifndef FILESYSTEM_HELPERS_HPP
00020 #define FILESYSTEM_HELPERS_HPP
00021 
00022 #include <qi/session.hpp>
00023 
00024 #include <boost/filesystem.hpp>
00025 #include <boost/filesystem/operations.hpp>
00026 #include <boost/algorithm/string/replace.hpp>
00027 
00028 #ifdef CATKIN_BUILD
00029 #include <ros/package.h>
00030 #endif
00031 
00032 namespace naoqi
00033 {
00034 namespace helpers
00035 {
00036 namespace filesystem
00037 {
00038 
00039 static const long folderMaximumSize = 2000000000;
00040 
00041 inline void getFoldersize(std::string rootFolder, long& file_size){
00042   boost::algorithm::replace_all(rootFolder, "\\\\", "\\");
00043   boost::filesystem::path folderPath(rootFolder);
00044   if (boost::filesystem::exists(folderPath)){
00045     boost::filesystem::directory_iterator end_itr;
00046 
00047     for (boost::filesystem::directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte )
00048     {
00049       boost::filesystem::path filePath(dirIte->path());
00050       try{
00051         if (!boost::filesystem::is_directory(dirIte->status()) )
00052         {
00053           file_size = file_size + boost::filesystem::file_size(filePath);
00054         }else{
00055           getFoldersize(filePath.string(),file_size);
00056         }
00057       }catch(std::exception& e){
00058         std::cout << e.what() << std::endl;
00059       }
00060     }
00061   }
00062 }
00063 
00064 inline void getFiles(const boost::filesystem::path& root, const std::string& ext, std::vector<boost::filesystem::path>& ret)
00065 {
00066   if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return;
00067 
00068   boost::filesystem::recursive_directory_iterator it(root);
00069   boost::filesystem::recursive_directory_iterator endit;
00070 
00071   while(it != endit)
00072   {
00073     if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext)
00074     {
00075       ret.push_back(it->path().filename());
00076     }
00077     ++it;
00078   }
00079 }
00080 
00081 inline void getFilesSize(const boost::filesystem::path& root, long& file_size)
00082 {
00083   std::vector<boost::filesystem::path> files_path;
00084   getFiles(root, ".bag", files_path);
00085   for (std::vector<boost::filesystem::path>::const_iterator it=files_path.begin();
00086        it!=files_path.end(); it++)
00087   {
00088     try{
00089       file_size = file_size + boost::filesystem::file_size(*it);
00090     }catch(std::exception& e){
00091       std::cout << e.what() << std::endl;
00092     }
00093   }
00094 }
00095 
00097 static const std::string boot_config_file_name = "boot_config.json";
00098 inline std::string& getBootConfigFile()
00099 {
00100 #ifdef CATKIN_BUILD
00101   static std::string path = ros::package::getPath("naoqi_driver")+"/share/"+boot_config_file_name;
00102   std::cout << "found a catkin prefix " << path << std::endl;
00103   return path;
00104 #else
00105   static std::string path = qi::path::findData( "/", boot_config_file_name );
00106   std::cout << "found a qibuild path " << path << std::endl;
00107   return path;
00108 #endif
00109 }
00110 
00111 /* URDF loader */
00112 inline std::string& getURDF( std::string filename )
00113 {
00114 #ifdef CATKIN_BUILD
00115   static std::string path = ros::package::getPath("naoqi_driver")+"/share/urdf/"+filename;
00116   std::cout << "found a catkin URDF " << path << std::endl;
00117   return path;
00118 #else
00119   static std::string path = qi::path::findData( "/urdf/", filename );
00120   std::cout << "found a qibuild URDF " << path << std::endl;
00121   return path;
00122 #endif
00123 }
00124 
00125 } // filesystem
00126 } //helpers
00127 } // naoqi
00128 
00129 #endif


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sun Sep 17 2017 02:57:14