SAGA Adaptor CPI v.1.0
|
00001 // Copyright (c) 2005-2009 Hartmut Kaiser 00002 // 00003 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00005 00006 #include <saga/saga-defs.hpp> 00007 00008 // #define USE_SPIRIT 00009 00010 #ifdef USE_SPIRIT 00011 #include <boost/version.hpp> 00012 00013 #if BOOST_VERSION >= 103800 00014 #include <boost/spirit/include/classic_symbols.hpp> 00015 #else 00016 #include <boost/spirit/symbols.hpp> 00017 #endif 00018 00019 #else // USE_SPIRIT 00020 00021 # include <saga/impl/exception.hpp> 00022 # include <saga/saga/adaptors/utils.hpp> 00023 00024 #endif // USE_SPIRIT 00025 00026 #include <saga/impl/config.hpp> 00027 #include <saga/impl/engine/url_grammar.hpp> 00028 #include <saga/impl/url.hpp> 00029 00030 #include <saga/saga/url.hpp> 00031 #include <saga/impl/runtime.hpp> 00032 #include <saga/saga/adaptors/file_transfer_spec.hpp> 00033 00035 namespace saga { namespace adaptors 00036 { 00037 // parse the given string for a file transfer specification and return true 00038 // if successful 00039 bool 00040 parse_file_transfer_specification(std::string spec, 00041 std::string& left_url, file_transfer_operator& mode, 00042 std::string& right_url) 00043 { 00044 #ifdef USE_SPIRIT 00045 using namespace boost::spirit; 00046 using namespace phoenix; 00047 00048 saga::url url_left, url_right; 00049 impl::url* impl_left(impl::runtime::get_impl(url_left)); 00050 impl::url* impl_right(impl::runtime::get_impl(url_right)); 00051 00052 symbols<file_transfer_operator> ops; 00053 ops.add (">" , copy_local_remote) 00054 (">>", append_local_remote) 00055 ("<" , copy_remote_local) 00056 ("<<", append_remote_local) 00057 ; 00058 00059 file_transfer_operator parsed_mode = unknown_mode; 00060 parse_info<> pi = parse( 00061 spec.c_str(), 00062 lexeme_d[impl::url_grammar<impl::url>(*impl_left)] 00063 >> ops [var(parsed_mode) = arg1] 00064 >> lexeme_d[impl::url_grammar<impl::url>(*impl_right)] 00065 >> !end_p, 00066 space_p 00067 ); 00068 00069 if (!pi.full) 00070 return false; 00071 00072 impl_left->set_checked(); 00073 impl_right->set_checked(); 00074 00075 left_url = url_left.get_url(); 00076 right_url = url_right.get_url(); 00077 mode = parsed_mode; 00078 return true; 00079 00080 #else // USE_SPIRIT 00081 00082 // for split to work correctly, we first collect superflous spaces 00083 size_t pos = spec.find (" "); 00084 while ( std::string::npos != pos ) 00085 { 00086 spec.erase (pos, 1); 00087 pos = spec.find (" "); 00088 } 00089 00090 std::vector <std::string> items = saga::adaptors::utils::split (spec, ' '); 00091 00092 if ( 3 != items.size () ) 00093 { 00094 SAGA_THROW_NO_OBJECT ("Invalid file transfer specification", 00095 saga::BadParameter); 00096 } 00097 00098 saga::url url_left = items[0]; 00099 std::string string_op = items[1]; 00100 saga::url url_right = items[2]; 00101 00102 if ( string_op == ">" ) mode = copy_local_remote; 00103 else if ( string_op == ">>" ) mode = append_local_remote; 00104 else if ( string_op == "<" ) mode = copy_remote_local; 00105 else if ( string_op == "<<" ) mode = append_remote_local; 00106 else SAGA_THROW_NO_OBJECT ("Invalid mode in file transfer specification", 00107 saga::BadParameter); 00108 00109 left_url = url_left.get_url(); 00110 right_url = url_right.get_url(); 00111 00112 return true; 00113 #endif // USE_SPIRIT 00114 } 00115 00117 }}