file.cpp
Go to the documentation of this file.
1 #include <cerrno>
2 #include <cstdlib>
3 #include <iostream>
4 #include <string>
5 
6 #include <boost/filesystem/fstream.hpp>
7 #include <boost/filesystem/path.hpp>
8 #include <boost/format.hpp>
9 
10 #include <ros/ros.h>
11 
12 #include "file.hh"
13 #include "names.hh"
14 
15 
16 std::string
17 getInitFileFromModelName (const std::string& modelName,
18  const std::string& defaultPath)
19 {
20  boost::format fmt("%1%/%2%/%2%.init");
21  fmt % defaultPath % modelName;
22  return fmt.str ();
23 }
24 
25 std::string
26 getHelpImageFileFromModelName (const std::string& modelName,
27  const std::string& defaultPath)
28 {
29  boost::format fmt("%1%/%2%/%2%.ppm");
30  fmt % defaultPath % modelName;
31  return fmt.str ();
32 }
33 
34 std::string
35 getModelFileFromModelName (const std::string& modelName,
36  const std::string& defaultPath)
37 {
38  boost::format fmt("%1%/%2%/%2%");
39  fmt % defaultPath % modelName;
40  return fmt.str ();
41 }
42 
43 std::string
44 getConfigurationFileFromModelName (const std::string& modelName,
45  const std::string& defaultPath)
46 {
47  boost::format fmt("%1%/%2%/%2%.xml");
48  fmt % defaultPath % modelName;
49  return fmt.str ();
50 }
51 
52 std::string
53 getInitialPoseFileFromModelName (const std::string& modelName,
54  const std::string& defaultPath)
55 {
56  boost::format fmt("%1%/%2%/%2%.0.pos");
57  fmt % defaultPath % modelName;
58  return fmt.str ();
59 }
60 
61 bool
62 makeModelFile(boost::filesystem::ofstream& modelStream,
63  std::string& fullModelPath)
64 {
65  std::string modelDescription;
67  {
68  ROS_ERROR_STREAM("Failed to initialize: no model is provided.");
69  return false;
70  }
71  ROS_DEBUG_STREAM("Trying to load the model from the parameter server.");
72 
74 
75  char* tmpname = strdup("/tmp/tmpXXXXXX");
76  if (mkdtemp(tmpname) == NULL)
77  {
79  ("Failed to create the temporary directory: " << strerror(errno));
80  return false;
81  }
82  // From the content of the model description check if the model is in vrml or in cao format
83  std::string vrml_header("#VRML #vrml");
84  std::string cao_header("V1");
85  boost::filesystem::path path(tmpname);
86  if (modelDescription.compare(0, 5, vrml_header, 0, 5) == 0) {
87  path /= "model.wrl";
88  }
89  else if (modelDescription.compare(0, 5, vrml_header, 6, 5) == 0) {
90  path /= "model.wrl";
91  }
92  else if (modelDescription.compare(0, 2, cao_header) == 0) {
93  path /= "model.cao";
94  }
95  else {
96  ROS_ERROR_STREAM("Failed to create the temporary model file: " << path);
97  free(tmpname);
98  return false;
99  }
100  free(tmpname);
101 
102  fullModelPath = path.native();
103 
104  modelStream.open(path);
105  if (!modelStream.good())
106  {
107  ROS_ERROR_STREAM("Failed to create the temporary file: " << path);
108  return false;
109  }
110  modelStream << modelDescription;
111  modelStream.flush();
112  return true;
113 }
std::string getHelpImageFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:26
std::string getModelFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:35
std::string getInitFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:17
ROSCPP_DECL bool get(const std::string &key, std::string &s)
std::string getInitialPoseFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:53
std::string model_description_param
ROSCPP_DECL bool has(const std::string &key)
std::string getConfigurationFileFromModelName(const std::string &modelName, const std::string &defaultPath)
Definition: file.cpp:44
#define ROS_DEBUG_STREAM(args)
#define ROS_ERROR_STREAM(args)
bool makeModelFile(boost::filesystem::ofstream &modelStream, std::string &fullModelPath)
Definition: file.cpp:62


visp_tracker
Author(s): Thomas Moulard
autogenerated on Wed Jul 3 2019 19:48:07