WriteToFile.cpp
Go to the documentation of this file.
00001 #include <grasp_planning_graspit_ros/WriteToFile.h>
00002 #include <sstream>
00003 
00005 bool grasp_planning_graspit_ros::makeDirectoryIfNeeded(const std::string& dPath)
00006 {
00007     try
00008     {
00009         boost::filesystem::path dir(dPath);
00010         boost::filesystem::path buildPath;
00011 
00012         for (boost::filesystem::path::iterator it(dir.begin()), it_end(dir.end());
00013              it != it_end; ++it)
00014         {
00015             buildPath /= *it;
00016             // std::cout << buildPath << std::endl;
00017 
00018             if (!boost::filesystem::exists(buildPath) &&
00019                     !boost::filesystem::create_directory(buildPath))
00020             {
00021                 PRINTERROR("Could not create directory " << buildPath);
00022                 return false;
00023             }
00024         }
00025     }
00026     catch (const boost::filesystem::filesystem_error& ex)
00027     {
00028         PRINTERROR(ex.what());
00029         return false;
00030     }
00031     return true;
00032 }
00033 
00035 /*bool grasp_planning_graspit_ros::saveToFile(const moveit_msgs::Grasp& msg,
00036   const std::string& filename, bool asBinary)
00037 {
00038     boost::filesystem::path p(filename);
00039     boost::filesystem::path dir = p.parent_path();
00040     if (!makeDirectoryIfNeeded(dir.string()))
00041     {
00042       ROS_ERROR_STREAM("Could not create directory " << dir);
00043       return false;
00044     }
00045 
00046     std::ios_base::openmode mode;
00047     if (asBinary) mode = std::ios::out | std::ios::binary;
00048     else mode = std::ios::out;
00049 
00050     std::ofstream ofs(filename.c_str(), mode);
00051 
00052     if (!ofs.is_open())
00053     {
00054         ROS_ERROR("File %s cannot be opened.", filename.c_str());
00055         return false;
00056     }
00057 
00058     if (asBinary)
00059     {
00060         uint32_t serial_size = ros::serialization::serializationLength(msg);
00061         boost::shared_array<uint8_t> obuffer(new uint8_t[serial_size]);
00062         ros::serialization::OStream ostream(obuffer.get(), serial_size);
00063         ros::serialization::serialize(ostream, msg);
00064         ofs.write((char*) obuffer.get(), serial_size);
00065     }
00066     else
00067     {
00068         ofs<<msg; 
00069     }
00070     ofs.close();
00071     return true;
00072 }*/
00073 
00075 bool grasp_planning_graspit_ros::writeGraspMessage(
00076   const moveit_msgs::Grasp &grasp,
00077   const std::string &outputDirectory,
00078   const std::string &filenamePrefix,
00079   const std::string &ext)
00080 {
00081   std::stringstream filename;
00082   filename << outputDirectory << "/" << filenamePrefix << "." << ext;
00083   std::stringstream filename_txt;
00084   filename_txt << outputDirectory << "/" << filenamePrefix << "_string." << ext;
00085   if (!saveToFile(grasp, filename.str(), true))
00086   {
00087       PRINTERROR("Could not save grasp to file " << filename.str());
00088       return false;
00089   }
00090   if (!saveToFile(grasp, filename_txt.str(), false))
00091   {
00092       PRINTERROR("Could not save grasp to text file " << filename_txt.str());
00093       return false;
00094   }
00095   return true;
00096 }
00097 


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