From 6a1b7b42deaebdb56e549793f3774323a1cb61b4 Mon Sep 17 00:00:00 2001
From: Thomas Moulard <thomas.moulard@gmail.com>
Date: Tue, 11 Jan 2011 12:01:33 +0100
Subject: [PATCH] Check properly that input files exist before parsing a model.

---
 src/dynamic.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 8 deletions(-)

diff --git a/src/dynamic.cpp b/src/dynamic.cpp
index bac7b63..2c02568 100644
--- a/src/dynamic.cpp
+++ b/src/dynamic.cpp
@@ -18,6 +18,9 @@
  * with sot-dynamic.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <boost/filesystem.hpp>
+#include <boost/format.hpp>
+
 #include <jrl/mal/matrixabstractlayer.hh>
 
 #include <sot-dynamic/dynamic.h>
@@ -413,24 +416,67 @@ setXmlRankFile( const std::string& filename )
 {
   xmlRankFile = filename;
 }
-void Dynamic::
-parseConfigFiles( void )
+
+
+// Helper macro for Dynamic::parseConfigFiles().
+// Check that all required files exist or throw an exception
+// otherwise.
+#define CHECK_FILE(PATH, FILE_DESCRIPTION)				\
+  do									\
+    {									\
+      if (!boost::filesystem::exists (PATH)				\
+	  || boost::filesystem::is_directory (PATH))			\
+	{								\
+	  boost::format fmt ("Failed to open the %s (%s).");		\
+	  fmt % (FILE_DESCRIPTION) % robotModelPath.file_string ();	\
+	  								\
+	  SOT_THROW ExceptionDynamic					\
+	    (ExceptionDynamic::DYNAMIC_JRL,				\
+	     fmt.str ());						\
+	}								\
+    }									\
+  while (0)
+
+void Dynamic::parseConfigFiles()
 {
   sotDEBUGIN(15);
+
+  // Construct the full path to the robot model.
+  boost::filesystem::path robotModelPath (vrmlDirectory);
+  robotModelPath /= vrmlMainFile;
+
+  boost::filesystem::path xmlRankPath (xmlRankFile);
+  boost::filesystem::path xmlSpecificityPath (xmlSpecificityFile);
+
+  CHECK_FILE (robotModelPath, "HRP-2 model");
+  CHECK_FILE (xmlRankPath, "XML rank file");
+  CHECK_FILE (xmlSpecificityFile, "XML specificity file");
+
   try
     {
       sotDEBUG(35) << "Parse the vrml."<<endl;
-      string RobotFileName = vrmlDirectory+vrmlMainFile;
-      djj::parseOpenHRPVRMLFile(*m_HDR,RobotFileName,xmlRankFile,xmlSpecificityFile);
+
+      std::string robotModelPathStr (robotModelPath.file_string());
+      std::string xmlRankPathStr (xmlRankPath.file_string());
+      std::string xmlSpecificityPathStr (xmlSpecificityPath.file_string());
+
+      djj::parseOpenHRPVRMLFile (*m_HDR,
+				 robotModelPathStr,
+				 xmlRankPathStr,
+				 xmlSpecificityPathStr);
     }
-  catch( ... )
-    { SOT_THROW ExceptionDynamic( ExceptionDynamic::DYNAMIC_JRL,
-				     "Error while parsing." ); }
-  //inertia.init( aHDMB );
+  catch (...)
+    {
+      SOT_THROW ExceptionDynamic( ExceptionDynamic::DYNAMIC_JRL,
+				  "Error while parsing." );
+    }
+
   init = true;
   sotDEBUGOUT(15);
 }
 
+#undef CHECK_FILE
+
 /* --- SIGNAL ACTIVATION ---------------------------------------------------- */
 /* --- SIGNAL ACTIVATION ---------------------------------------------------- */
 /* --- SIGNAL ACTIVATION ---------------------------------------------------- */
-- 
GitLab