From 7b306b1808aaccc3c473e6a1b3e5eeebd919ab77 Mon Sep 17 00:00:00 2001 From: basadi Date: Fri, 3 Jul 2015 14:53:18 +0200 Subject: [PATCH 1/4] I think it better to separate the task of reading and parsing them into separate function. --- src/SMURFParser.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/SMURFParser.cpp b/src/SMURFParser.cpp index 5dc20c5..e695203 100644 --- a/src/SMURFParser.cpp +++ b/src/SMURFParser.cpp @@ -60,4 +60,28 @@ namespace smurf_parser { return model; } +void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename, std::string &absolute_urdf_path,std::string absolute_srd_fpath, std::vector absolute_conf_yml_files) +{ + map->append(configmaps::ConfigMap::fromYamlFile(path+smurffilename, expandURIs)); + configmaps::ConfigVector::iterator it; + for(it = (*map)["files"].begin(); it!=(*map)["files"].end(); ++it) + { + boost::filesystem::path filepath((std::string)(*it)); + if(filepath.extension().generic_string() == ".urdf") + { + absolute_urdf_path = path + filepath.generic_string(); + } + if(filepath.extension().generic_string() == ".srdf") + { + absolute_srd_fpath=path + filepath.generic_string(); + + } + else if(filepath.extension() == ".yml") + { + absolute_conf_yml_files.push_back(path+filepath.generic_string()); + } + } + return; +} + } From 7ac110c9dd012cbeefd86f897c6bb5eeda389f9b Mon Sep 17 00:00:00 2001 From: "behnam.asadi@dfki.de" Date: Fri, 3 Jul 2015 15:33:23 +0200 Subject: [PATCH 2/4] I forgot & mark --- src/SMURFParser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SMURFParser.cpp b/src/SMURFParser.cpp index e695203..75c4897 100644 --- a/src/SMURFParser.cpp +++ b/src/SMURFParser.cpp @@ -60,7 +60,7 @@ namespace smurf_parser { return model; } -void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename, std::string &absolute_urdf_path,std::string absolute_srd_fpath, std::vector absolute_conf_yml_files) +void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename, std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files) { map->append(configmaps::ConfigMap::fromYamlFile(path+smurffilename, expandURIs)); configmaps::ConfigVector::iterator it; From 97c5c89aa894c0238df3c9bbdb7d04a52591855b Mon Sep 17 00:00:00 2001 From: "behnam.asadi@gmail.com" Date: Fri, 3 Jul 2015 16:38:38 +0200 Subject: [PATCH 3/4] bool expandURIs was missing --- src/SMURFParser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SMURFParser.cpp b/src/SMURFParser.cpp index 75c4897..716732b 100644 --- a/src/SMURFParser.cpp +++ b/src/SMURFParser.cpp @@ -60,7 +60,7 @@ namespace smurf_parser { return model; } -void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename, std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files) +void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename,bool expandURIs ,std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files) { map->append(configmaps::ConfigMap::fromYamlFile(path+smurffilename, expandURIs)); configmaps::ConfigVector::iterator it; From 32bc7eb8481853f0a3483db60cbd12436d23600f Mon Sep 17 00:00:00 2001 From: "behnam.asadi@gmail.com" Date: Mon, 20 Jul 2015 12:14:22 +0200 Subject: [PATCH 4/4] the function getFilesAbsolutePath has been clean up. The private function "resolve_path" has been added to translate relative addresses to absolute address without pain. A test file for test the code as been also added. --- CMakeLists.txt | 7 ++++++ src/SMURFParser.cpp | 51 +++++++++++++++++++++++++++++++++++---- src/SMURFParser.h | 2 ++ src/test_smurf_parser.cpp | 22 +++++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/test_smurf_parser.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a3320bd..5038184 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,6 @@ project(smurf_parser) +set(CMAKE_BUILD_TYPE Debug) + set(PROJECT_VERSION 1.0) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR} "${PROJECT_SOURCE_DIR}/cmake") cmake_minimum_required(VERSION 2.6) @@ -112,3 +114,8 @@ install(FILES ${SOURCES_H} DESTINATION include/smurf_parser) configure_file(${PROJECT_NAME}.pc.in ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc @ONLY) install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION lib/pkgconfig) + + +add_executable(test_smurf_parser src/test_smurf_parser.cpp ${SOURCES_H} ${TARGET_SRC} ) +target_link_libraries(test_smurf_parser ${PROJECT_NAME} ${QT_LIBRARIES} ${QT_QTXML_LIBRARY} ${PKGCONFIG_LIBRARIES} ${WIN_LIBS} ${Boost_LIBRARIES}) + diff --git a/src/SMURFParser.cpp b/src/SMURFParser.cpp index 716732b..04e9426 100644 --- a/src/SMURFParser.cpp +++ b/src/SMURFParser.cpp @@ -60,25 +60,66 @@ namespace smurf_parser { return model; } -void getFilePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename,bool expandURIs ,std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files) + + +boost::filesystem::path resolve_path( const boost::filesystem::path& p, const boost::filesystem::path& base ) +{ + boost::filesystem::path abs_p = boost::filesystem::absolute(p,base); + boost::filesystem::path result; + for(boost::filesystem::path::iterator it=abs_p.begin(); it!=abs_p.end(); ++it) + { + if(*it == "..") + { + // /a/b/.. is not necessarily /a if b is a symbolic link + if(boost::filesystem::is_symlink(result) ) + result /= *it; + // /a/b/../.. is not /a/b/.. under most circumstances + // We can end up with ..s in our result because of symbolic links + else if(result.filename() == "..") + result /= *it; + // Otherwise it should be safe to resolve the parent + else + result = result.parent_path(); + } + else if(*it == ".") + { + // Ignore + } + else + { + // Just cat other path entries + result /= *it; + } + } + return result; +} + + + +void getFilesAbsolutePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename,bool expandURIs ,std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files) { map->append(configmaps::ConfigMap::fromYamlFile(path+smurffilename, expandURIs)); configmaps::ConfigVector::iterator it; + boost::filesystem::path abs_path_of_file; + + for(it = (*map)["files"].begin(); it!=(*map)["files"].end(); ++it) { boost::filesystem::path filepath((std::string)(*it)); if(filepath.extension().generic_string() == ".urdf") { - absolute_urdf_path = path + filepath.generic_string(); + abs_path_of_file=resolve_path(filepath.generic_string(),path) ; + absolute_urdf_path=abs_path_of_file.string(); } if(filepath.extension().generic_string() == ".srdf") { - absolute_srd_fpath=path + filepath.generic_string(); - + abs_path_of_file=resolve_path(filepath.generic_string(),path) ; + absolute_srd_fpath=abs_path_of_file.string(); } else if(filepath.extension() == ".yml") { - absolute_conf_yml_files.push_back(path+filepath.generic_string()); + abs_path_of_file=resolve_path(filepath.generic_string(),path) ; + absolute_conf_yml_files.push_back(abs_path_of_file.string() ); } } return; diff --git a/src/SMURFParser.h b/src/SMURFParser.h index 8c8f056..2afc828 100644 --- a/src/SMURFParser.h +++ b/src/SMURFParser.h @@ -38,6 +38,8 @@ namespace smurf_parser { boost::shared_ptr parseFile(configmaps::ConfigMap* map, std::string path, std::string smurffilename, bool expandURIs); + void getFilesAbsolutePath(std::string path,configmaps::ConfigMap* map, std::string smurffilename,bool expandURIs ,std::string &absolute_urdf_path,std::string &absolute_srd_fpath, std::vector &absolute_conf_yml_files); + } // end of namespace smurf_parser #endif // SMURF_PARSER_H diff --git a/src/test_smurf_parser.cpp b/src/test_smurf_parser.cpp new file mode 100644 index 0000000..5d561e1 --- /dev/null +++ b/src/test_smurf_parser.cpp @@ -0,0 +1,22 @@ +#include "SMURFParser.h" +#include + +int main(int argc , char** argv) +{ + std::string path="/home/basadi/Desktop/mantis.git/smurf/"; + configmaps::ConfigMap* map=new configmaps::ConfigMap; + std::string smurffilename="mantis.smurf"; + bool expandURIs; + std::string absolute_urdf_path; + std::string absolute_srdf_path; + std::vector absolute_conf_yml_files; + + smurf_parser::getFilesAbsolutePath(path,map,smurffilename, expandURIs, absolute_urdf_path,absolute_srdf_path,absolute_conf_yml_files); + std::cout<<"absolute_urdf_path: " <