| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  |  * @file utilities.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |  * @author Andrew Melim | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  |  * @author Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2012-04-21 09:45:04 +08:00
										 |  |  | #include <cstdlib>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-10 04:29:47 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | #include <boost/filesystem.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | namespace wrap { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | string file_contents(const string& filename, bool skipheader) { | 
					
						
							| 
									
										
										
										
											2012-07-24 03:29:57 +08:00
										 |  |  |   ifstream ifs(filename.c_str()); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   if(!ifs) throw CantOpenFile(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // read file into stringstream
 | 
					
						
							|  |  |  |   stringstream ss; | 
					
						
							|  |  |  |   if (skipheader) ifs.ignore(256,'\n'); | 
					
						
							|  |  |  |   ss << ifs.rdbuf(); | 
					
						
							|  |  |  |   ifs.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // return string
 | 
					
						
							|  |  |  |   return ss.str(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 02:57:32 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-10 04:29:47 +08:00
										 |  |  | bool assert_equal(const string& expected, const string& actual) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   if (expected == actual) | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   printf("Not equal:\n"); | 
					
						
							|  |  |  |   cout << "expected: [" << expected << "]\n"; | 
					
						
							|  |  |  |   cout << "actual: [" << actual << "]" << endl; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2011-12-02 02:57:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:11 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool assert_equal(const vector<string>& expected, const vector<string>& actual) { | 
					
						
							|  |  |  |   bool match = true; | 
					
						
							|  |  |  |   if (expected.size() != actual.size()) | 
					
						
							|  |  |  |     match = false; | 
					
						
							|  |  |  |   vector<string>::const_iterator | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     itExp = expected.begin(), | 
					
						
							|  |  |  |     itAct = actual.begin(); | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:11 +08:00
										 |  |  |   if(match) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) { | 
					
						
							|  |  |  |       if (*itExp != *itAct) { | 
					
						
							|  |  |  |         match = false; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if(!match) { | 
					
						
							|  |  |  |     cout << "expected: " << endl; | 
					
						
							|  |  |  |     BOOST_FOREACH(const vector<string>::value_type& a, expected) { cout << "["  << a << "] "; } | 
					
						
							|  |  |  |     cout << "\nactual: " << endl; | 
					
						
							|  |  |  |     BOOST_FOREACH(const vector<string>::value_type& a, actual) { cout << "["  << a << "] "; } | 
					
						
							|  |  |  |     cout << endl; | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:11 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool files_equal(const string& expected, const string& actual, bool skipheader) { | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:50:38 +08:00
										 |  |  |     string expected_contents = file_contents(expected, skipheader); | 
					
						
							|  |  |  |     string actual_contents   = file_contents(actual, skipheader); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     bool equal = actual_contents == expected_contents; | 
					
						
							|  |  |  |     if (!equal) { | 
					
						
							| 
									
										
										
										
											2014-05-26 04:27:29 +08:00
										 |  |  |       cerr << "<<< DIFF OUTPUT (if none, white-space differences only):\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |       stringstream command; | 
					
						
							| 
									
										
										
										
											2014-05-26 04:27:29 +08:00
										 |  |  |       command << "diff --ignore-all-space " << expected << " " << actual << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |       system(command.str().c_str()); | 
					
						
							| 
									
										
										
										
											2014-05-26 04:27:29 +08:00
										 |  |  |       cerr << ">>>\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return equal; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   catch (const string& reason) { | 
					
						
							|  |  |  |     cerr << "expection: " << reason << endl; | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-12-09 04:50:38 +08:00
										 |  |  |   catch (CantOpenFile& e) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     cerr << "file opening error: " << e.what() << endl; | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:50:38 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | string maybe_shared_ptr(bool add, const string& qtype, const string& type) { | 
					
						
							|  |  |  |   string str = add? "Shared" : ""; | 
					
						
							|  |  |  |   if (add) str += type;  | 
					
						
							|  |  |  |   else str += qtype; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | string qualifiedName(const string& separator, const vector<string>& names) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string result; | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   if (!names.empty()) { | 
					
						
							|  |  |  |     for (size_t i = 0; i < names.size() - 1; ++i) | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       result += (names[i] + separator); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |     result += names.back(); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2012-07-08 20:27:39 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | void createNamespaceStructure(const std::vector<std::string>& namespaces, | 
					
						
							|  |  |  |     const std::string& toolboxPath) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   using namespace boost::filesystem; | 
					
						
							|  |  |  |   path curPath = toolboxPath; | 
					
						
							|  |  |  |   BOOST_FOREACH(const string& subdir, namespaces) { | 
					
						
							| 
									
										
										
										
											2012-11-28 03:03:19 +08:00
										 |  |  | //    curPath /= "+" + subdir; // original - resulted in valgrind error
 | 
					
						
							|  |  |  |     curPath = curPath / string(string("+") + subdir); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     if(!is_directory(curPath)) { | 
					
						
							|  |  |  |       if(exists("+" + subdir)) | 
					
						
							|  |  |  |         throw OutputError("Need to write files to directory " + curPath.string() + ", which already exists as a file but is not a directory"); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         boost::filesystem::create_directory(curPath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-08 20:27:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | } // \namespace wrap
 |