Go to the documentation of this file.00001 #include <cerrno>
00002 #include <cstdlib>
00003 #include <iostream>
00004 #include <string>
00005
00006 #include <boost/filesystem/fstream.hpp>
00007 #include <boost/filesystem/path.hpp>
00008 #include <boost/format.hpp>
00009
00010 #include <ros/ros.h>
00011
00012 #include "file.hh"
00013 #include "names.hh"
00014
00015
00016 std::string
00017 getInitFileFromModelName (const std::string& modelName,
00018 const std::string& defaultPath)
00019 {
00020 boost::format fmt("%1%/%2%/%2%.init");
00021 fmt % defaultPath % modelName;
00022 return fmt.str ();
00023 }
00024
00025 std::string
00026 getHelpImageFileFromModelName (const std::string& modelName,
00027 const std::string& defaultPath)
00028 {
00029 boost::format fmt("%1%/%2%/%2%.ppm");
00030 fmt % defaultPath % modelName;
00031 return fmt.str ();
00032 }
00033
00034 std::string
00035 getModelFileFromModelName (const std::string& modelName,
00036 const std::string& defaultPath)
00037 {
00038 boost::format fmt("%1%/%2%/%2%");
00039 fmt % defaultPath % modelName;
00040 return fmt.str ();
00041 }
00042
00043 std::string
00044 getConfigurationFileFromModelName (const std::string& modelName,
00045 const std::string& defaultPath)
00046 {
00047 boost::format fmt("%1%/%2%/%2%.xml");
00048 fmt % defaultPath % modelName;
00049 return fmt.str ();
00050 }
00051
00052 std::string
00053 getInitialPoseFileFromModelName (const std::string& modelName,
00054 const std::string& defaultPath)
00055 {
00056 boost::format fmt("%1%/%2%/%2%.0.pos");
00057 fmt % defaultPath % modelName;
00058 return fmt.str ();
00059 }
00060
00061 bool
00062 makeModelFile(boost::filesystem::ofstream& modelStream,
00063 std::string& fullModelPath)
00064 {
00065 std::string modelDescription;
00066 if (!ros::param::has(visp_tracker::model_description_param))
00067 {
00068 ROS_ERROR_STREAM("Failed to initialize: no model is provided.");
00069 return false;
00070 }
00071 ROS_DEBUG_STREAM("Trying to load the model from the parameter server.");
00072
00073 ros::param::get(visp_tracker::model_description_param, modelDescription);
00074
00075 char* tmpname = strdup("/tmp/tmpXXXXXX");
00076 if (mkdtemp(tmpname) == NULL)
00077 {
00078 ROS_ERROR_STREAM
00079 ("Failed to create the temporary directory: " << strerror(errno));
00080 return false;
00081 }
00082
00083 std::string vrml_header("#VRML #vrml");
00084 std::string cao_header("V1");
00085 boost::filesystem::path path(tmpname);
00086 if (modelDescription.compare(0, 5, vrml_header, 0, 5) == 0) {
00087 path /= "model.wrl";
00088 }
00089 else if (modelDescription.compare(0, 5, vrml_header, 6, 5) == 0) {
00090 path /= "model.wrl";
00091 }
00092 else if (modelDescription.compare(0, 2, cao_header) == 0) {
00093 path /= "model.cao";
00094 }
00095 else {
00096 ROS_ERROR_STREAM("Failed to create the temporary model file: " << path);
00097 free(tmpname);
00098 return false;
00099 }
00100 free(tmpname);
00101
00102 fullModelPath = path.native();
00103
00104 modelStream.open(path);
00105 if (!modelStream.good())
00106 {
00107 ROS_ERROR_STREAM("Failed to create the temporary file: " << path);
00108 return false;
00109 }
00110 modelStream << modelDescription;
00111 modelStream.flush();
00112 return true;
00113 }