WriteToFile.h
Go to the documentation of this file.
00001 #ifndef GRASP_PLANNING_GRASPIT_ROS_WRITETOFILE_H
00002 #define GRASP_PLANNING_GRASPIT_ROS_WRITETOFILE_H
00003 
00004 #include <grasp_planning_graspit_ros/LogBindingROS.h>
00005 #include <moveit_msgs/Grasp.h>
00006 #include <boost/filesystem.hpp>
00007 #include <string>
00008 #include <fstream>
00009 
00010 namespace grasp_planning_graspit_ros
00011 {
00012 
00014 bool makeDirectoryIfNeeded(const std::string& dPath);
00015 
00017 // Creates the directory (if required) and saves the grasp message
00018 // \param mode binary if true, saves the file as binary.
00019 template<typename ROSMessage>
00020 bool saveToFile(const ROSMessage& msg, const std::string& filename,
00021                 bool asBinary)
00022 {
00023   boost::filesystem::path p(filename);
00024   boost::filesystem::path dir = p.parent_path();
00025   if (!makeDirectoryIfNeeded(dir.string()))
00026   {
00027     ROS_ERROR_STREAM("Could not create directory " << dir);
00028     return false;
00029   }
00030 
00031 
00032   std::ios_base::openmode mode;
00033   if (asBinary) mode = std::ios::out | std::ios::binary;
00034   else mode = std::ios::out;
00035 
00036   std::ofstream ofs(filename.c_str(), mode);
00037 
00038   if (!ofs.is_open())
00039   {
00040       ROS_ERROR("File %s cannot be opened.", filename.c_str());
00041       return false;
00042   }
00043 
00044   if (asBinary)
00045   {
00046       uint32_t serial_size = ros::serialization::serializationLength(msg);
00047       boost::shared_array<uint8_t> obuffer(new uint8_t[serial_size]);
00048       ros::serialization::OStream ostream(obuffer.get(), serial_size);
00049       ros::serialization::serialize(ostream, msg);
00050       ofs.write((char*) obuffer.get(), serial_size);
00051   }
00052   else
00053   {
00054       ofs<<msg;
00055   }
00056   ofs.close();
00057   return true;
00058 }
00059 
00061 // Writes the grasp message once as binary and once as text.
00062 // @param filenamePrefix the prefix to the file. For the binary version,
00063 //  this will be
00064 //  outputDir + '/' + filename + . + \e ext.
00065 //  For the string version, this will be
00066 //  outputDir + '/' + filename + _string + . + \e ext.
00067 bool writeGraspMessage(
00068   const moveit_msgs::Grasp &grasp,
00069   const std::string &outputDirectory,
00070   const std::string &filenamePrefix = "Grasp",
00071   const std::string &ext= "msg");
00072 
00073 }  // namespace
00074 
00075 #endif  // GRASP_PLANNING_GRASPIT_ROS_WRITETOFILE_H


grasp_planning_graspit_ros
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:42