// Copyright (c) 2006 Ole Christian Weidner (oweidner@cct.lsu.edu) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #if !defined(SAGA_TEST_HELPER_UTILS_HPP) #define SAGA_TEST_HELPER_UTILS_HPP #if defined(BOOST_WINDOWS) # include #else # include #endif #include #include #include #include #include #include #include #include #ifndef MAX_PATH # define MAX_PATH _POSIX_PATH_MAX #endif /////////////////////////////////////////////////////////////////////////////// namespace saga { namespace test { /** * This class is an interface for the test helper library, every adaptor * should implement. It's basically a collection of methods used by the * various unit test cases. * */ class test_helper_utils { private: saga::ini::section * properties_; public: /** * constructor - does nothing right now. */ test_helper_utils() {} /** * destructor */ virtual ~test_helper_utils() {} void set_properties(saga::ini::section * properties) { properties_ = properties; } /* * the implementation of this interface should generate a file... ;-) * * TODO: Detailed description * */ virtual std::string create_temp_file_for_exception( const saga::error & e ) = 0; /** * the implementation of this interface should create a unique temporary * local/remote file if the create flag is set to TRUE and return the * path as a string. If the create flag is set to FASLE, only the unique * name is returned but no file is created. * * @param create * if set to TRUE unique file should be created, otherwise not * * @return * a unique file name */ virtual std::string create_temp_file_name(bool create = true) = 0; /** * the implementation of this interface should delete the given file * * @param path * path to the file to delete * */ virtual void delete_temp_file(std::string path) = 0; /** * the implementation of this interface should delete the given directory * * @param path * path to the directory to delete * */ virtual void delete_temp_dir(std::string path) = 0; /** * the implementation of this interface should create a unique temporary * local/remote directory if the create flag is set to TRUE and return the * path as a string. If the create flasg is set to FASLE, only the unique * name is returned but no file is created. * * @param create * if set to TRUE unique directory should be created, otherwise not * * @return * a unique directory name */ virtual std::string create_temp_dir_name(bool create = true) = 0; protected: /** * creates a unique file or directory name * * @return * a unique name */ inline std::string get_unique_path_name() { char tmp_name [MAX_PATH + 1]; #if defined(BOOST_WINDOWS) char tmp_dir_path[MAX_PATH + 1]; if ( 0 == GetTempPathA (sizeof (tmp_dir_path), tmp_dir_path) ) { #if BOOST_VERSION < 103400 boost::throw_exception ( boost::filesystem::filesystem_error("boost::filesystem", "unable to generate path for temporary file", boost::filesystem::system_error)); #else boost::throw_exception( boost::filesystem::filesystem_path_error( "unable to generate path for temporary file", 0)); #endif return std::string(); } strcat (tmp_dir_path, "SAGA\\"); DWORD cchBuff = MAX_PATH; // size of user name char chBuffer[MAX_PATH + 1]; // buffer for expanded string GetUserName (chBuffer, &cchBuff); // Get and display the user name. strcat (tmp_dir_path, chBuffer); // create directory boost::filesystem::path tempdir (tmp_dir_path, boost::filesystem::native); boost::filesystem::create_directories (tempdir); // create file name if ( 0 == GetTempFileNameA (tmp_dir_path, "tmp", 0, tmp_name) ) { #if BOOST_VERSION < 103400 boost::throw_exception (boost::filesystem:: filesystem_error("boost::filesystem", "unable to generate path for temporary file", boost::filesystem::system_error)); #else boost::throw_exception (boost::filesystem:: filesystem_path_error("unable to generate path for temporary file", 0)); #endif return std::string(); } #else snprintf (tmp_name, MAX_PATH, "/tmp/saga_XXXXXXXX"); int fd = mkstemp (tmp_name); if ( fd <= 0 ) { // FIXME: actually, the error has nothing to do with boost, so it should // not be a boost exception I guess? -- AM #if BOOST_VERSION < 103400 boost::throw_exception (boost::filesystem:: filesystem_error("boost::filesystem", "unable to generate path for temporary file", boost::filesystem::system_error)); #else boost::throw_exception (boost::filesystem:: filesystem_path_error("unable to generate path for temporary file", 0)); #endif return (boost::filesystem::path().string()); } close (fd); #endif boost::filesystem::remove (tmp_name); boost::filesystem::path retval (tmp_name, boost::filesystem::native); return (retval.string()); } }; /////////////////////////////////////////////////////////////////////////////// }} // saga::test /////////////////////////////////////////////////////////////////////////////// #define SAGA_TEST_HELPER_REGISTER(ActualType) \ BOOST_PLUGIN_EXPORT(SAGA_ADAPTOR_LIB_NAME, saga::test::test_helper_utils, \ ActualType, "test_helper_utils"); \ /**/ #endif //SAGA_TEST_HELPER_UTILS_HPP