diff --git a/include/dynamic-graph/fwd.hh b/include/dynamic-graph/fwd.hh index fe34dc2802c794ce49e215745d7987ef36d274e9..b1ee9e5648bcfe33daf737038168cadd0f6ac6e5 100644 --- a/include/dynamic-graph/fwd.hh +++ b/include/dynamic-graph/fwd.hh @@ -37,6 +37,8 @@ namespace dynamicgraph template<class Time> class SignalArray; + + class Interpreter; } // end of namespace dynamicgraph. #endif //! DYNAMIC_GRAPH_FWD_HH diff --git a/include/dynamic-graph/import.h b/include/dynamic-graph/import.h index 8566df45b4bef5e74c586ba77f653f97f4932a9f..a194be76152406880e77627e42214d85f43e908d 100644 --- a/include/dynamic-graph/import.h +++ b/include/dynamic-graph/import.h @@ -23,9 +23,10 @@ # include <boost/filesystem/path.hpp> +# include <dynamic-graph/fwd.hh> + namespace dynamicgraph { - class Interpreter; namespace command { namespace @@ -36,7 +37,26 @@ namespace dynamicgraph /// will look for scripts or plug-ins. typedef std::vector<boost::filesystem::path> paths_t; + /// \brief Import paths list. + /// + /// This vector of string is similar to Unix variables such as + /// PATH. It contains all paths that are used to search when + /// importing a script. + /// + /// The look-up is made from right to left: + /// + /// On Unix: + /// importPaths = A:B:C + /// On Microsoft Windows: + /// importPaths = A;B;C + /// + /// When typing "import foo", C will be searched first then B + /// and A. The search stops when the file is found. extern paths_t importPaths; + + /// \brief Already imported paths to avoid multiple inclusion. + extern paths_t alreadyImportedPaths; + } // end of anonymous namespace. /// \brief Implement sot interpretor import command. diff --git a/src/dgraph/import.cpp b/src/dgraph/import.cpp index e345057a8598aff5062bbfbe8633850bc451f589..9f93fb73ea0d5c30f560f3c94f5295fda4fa402e 100644 --- a/src/dgraph/import.cpp +++ b/src/dgraph/import.cpp @@ -68,22 +68,8 @@ namespace dynamicgraph /// Initialize import paths list (called during static initialization). paths_t initializePaths (); - /// \brief Import paths list. - /// - /// This vector of string is similar to Unix variables such as - /// PATH. It contains all paths that are used to search when - /// importing a script. - /// - /// The look-up is made from right to left: - /// - /// On Unix: - /// importPaths = A:B:C - /// On Microsoft Windows: - /// importPaths = A;B;C - /// - /// When typing ``import foo'', C will be searched first then B - /// and A. The search stops when the file is found. paths_t importPaths = initializePaths (); + paths_t alreadyImportedPaths; /// Search for a module. /// @@ -268,6 +254,7 @@ namespace dynamicgraph // Get the absolute path of the module. boost::filesystem::path path = searchModule (module); + // Check that the module can be opened. std::ifstream file (path.file_string ().c_str ()); if (!boost::filesystem::is_regular_file (path) || !file.is_open () || !file.good ()) @@ -296,11 +283,20 @@ namespace dynamicgraph return; } + // If the module has already been imported, do not import it + // again. + if (std::find (alreadyImportedPaths.begin (), + alreadyImportedPaths.end (), + path) != alreadyImportedPaths.end ()) + return; + if (path.extension () != SHARED_LIBRARY_EXT) importScript (interpreter, path, file, os); else importPlugin (interpreter, path, os); + alreadyImportedPaths.push_back (path); + dgDEBUGOUT(15); }