diff options
-rw-r--r-- | examples/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/libsshpp_noexcept.cpp | 41 | ||||
-rw-r--r-- | include/libssh/libsshpp.hpp | 35 |
3 files changed, 69 insertions, 9 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 867869c8..ad614294 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(senddata senddata.c ${examples_SRCS}) add_executable(sshnetcat sshnetcat.c ${examples_SRCS}) add_executable(libsshpp libsshpp.cpp) +add_executable(libsshpp_noexcept libsshpp_noexcept.cpp) target_link_libraries(libssh_scp ${LIBSSH_SHARED_LIBRARY}) target_link_libraries(scp_download ${LIBSSH_SHARED_LIBRARY}) @@ -26,6 +27,7 @@ target_link_libraries(samplessh ${LIBSSH_SHARED_LIBRARY}) target_link_libraries(exec ${LIBSSH_SHARED_LIBRARY}) target_link_libraries(senddata ${LIBSSH_SHARED_LIBRARY}) target_link_libraries(libsshpp ${LIBSSH_SHARED_LIBRARY}) +target_link_libraries(libsshpp_noexcept ${LIBSSH_SHARED_LIBRARY}) target_link_libraries(sshnetcat ${LIBSSH_SHARED_LIBRARY}) diff --git a/examples/libsshpp_noexcept.cpp b/examples/libsshpp_noexcept.cpp new file mode 100644 index 00000000..624cfe9f --- /dev/null +++ b/examples/libsshpp_noexcept.cpp @@ -0,0 +1,41 @@ +/* +Copyright 2010 Aris Adamantiadis + +This file is part of the SSH Library + +You are free to copy this file, modify it in any way, consider it being public +domain. This does not apply to the rest of the library though, but it is +allowed to cut-and-paste working code from this file to any license of +program. +*/ + +/* This file demonstrates the use of the C++ wrapper to libssh + * specifically, without C++ exceptions + */ + +#include <iostream> +#define SSH_NO_CPP_EXCEPTIONS +#include <libssh/libsshpp.hpp> + +int main(int argc, const char **argv){ + ssh::Session session,s2; + int err; + if(argc>1) + err=session.setOption(SSH_OPTIONS_HOST,argv[1]); + else + err=session.setOption(SSH_OPTIONS_HOST,"localhost"); + if(err==SSH_ERROR) + goto error; + err=session.connect(); + if(err==SSH_ERROR) + goto error; + err=session.userauthAutopubkey(); + if(err==SSH_ERROR) + goto error; + + return 0; + error: + std::cout << "Error during connection : "; + std::cout << session.getError() << std::endl; + return 1; +} diff --git a/include/libssh/libsshpp.hpp b/include/libssh/libsshpp.hpp index 5250057e..99ceebd7 100644 --- a/include/libssh/libsshpp.hpp +++ b/include/libssh/libsshpp.hpp @@ -47,7 +47,13 @@ #include <stdlib.h> namespace ssh { -/** @brief This class describes a SSH Exception object. This object can be throwed +/** Some people do not like C++ exceptions. With this define, we give + * the choice to use or not exceptions. + * @brief if defined, disable C++ exceptions for libssh c++ wrapper + */ +#ifndef SSH_NO_CPP_EXCEPTIONS + +/** @brief This class describes a SSH Exception object. This object can be thrown * by several SSH functions that interact with the network, and may fail because of * socket, protocol or memory errors. */ @@ -84,7 +90,18 @@ private: /** @internal * @brief Macro to throw exception if there was an error */ -#define ssh_throw(x) if(x==SSH_ERROR) throw SshException(getCSession()); +#define ssh_throw(x) if((x)==SSH_ERROR) throw SshException(getCSession()) +#define void_throwable void + +#else + +/* No exception at all. All functions will return an error code instead + * of an exception + */ +#define ssh_throw(x) if((x)==SSH_ERROR) return SSH_ERROR +#define void_throwable int + +#endif /** * The ssh::Session class contains the state of a SSH connection. @@ -105,7 +122,7 @@ public: * @throws SshException on error * @see ssh_options_set */ - void setOption(enum ssh_options_e type, const char *option){ + void_throwable setOption(enum ssh_options_e type, const char *option){ ssh_throw(ssh_options_set(c_session,type,option)); } /** @brief sets an SSH session options @@ -114,7 +131,7 @@ public: * @throws SshException on error * @see ssh_options_set */ - void setOption(enum ssh_options_e type, long int option){ + void_throwable setOption(enum ssh_options_e type, long int option){ ssh_throw(ssh_options_set(c_session,type,&option)); } /** @brief sets an SSH session options @@ -123,14 +140,14 @@ public: * @throws SshException on error * @see ssh_options_set */ - void setOption(enum ssh_options_e type, void *option){ + void_throwable setOption(enum ssh_options_e type, void *option){ ssh_throw(ssh_options_set(c_session,type,option)); } /** @brief connects to the remote host * @throws SshException on error * @see ssh_connect */ - void connect(){ + void_throwable connect(){ int ret=ssh_connect(c_session); ssh_throw(ret); } @@ -279,7 +296,7 @@ public: * @throws SshException on error * @see ssh_options_copy */ - void optionsCopy(const Session &source){ + void_throwable optionsCopy(const Session &source){ ssh_throw(ssh_options_copy(source.c_session,&c_session)); } /** @brief parses a configuration file for options @@ -287,7 +304,7 @@ public: * @param[in] file configuration file name * @see ssh_options_parse_config */ - void optionsParseConfig(const char *file){ + void_throwable optionsParseConfig(const char *file){ ssh_throw(ssh_options_parse_config(c_session,file)); } /** @brief silently disconnect from remote host @@ -335,7 +352,7 @@ public: * @throws SshException on error * @see ssh_channel_close */ - void close(){ + void_throwable close(){ ssh_throw(ssh_channel_close(channel)); } int cancelForward(const char *address, int port); |