From 9392ca2afe6bf6b543f0c08ec137d8404580b634 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 14 Feb 2014 20:07:39 -0500 Subject: [PATCH 1/6] target_compile_options attempts to apply c++ flags to compile c objects --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f4567b0..e82ea52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,7 +175,7 @@ ELSE() SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall" ) IF(APPLE) - target_compile_options(fc PUBLIC -std=c++11 -stdlib=libc++ -Wall) + SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -stdlib=libc++ -Wall" ) ELSE() target_compile_options(fc PUBLIC -std=c++11 -Wall -fnon-call-exceptions -Wno-unused-local-typedefs -fmax-errors=3) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-local-typedefs -fmax-errors=3 ") From a80164645fdbc96e7f0cf88e52be5f8067f6b7ae Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 14 Feb 2014 20:32:23 -0500 Subject: [PATCH 2/6] Update tcp_socket listen on single endpoint --- include/fc/network/tcp_socket.hpp | 7 ++++--- src/network/tcp_socket.cpp | 14 +++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/fc/network/tcp_socket.hpp b/include/fc/network/tcp_socket.hpp index 6f58905..e66c1a5 100644 --- a/include/fc/network/tcp_socket.hpp +++ b/include/fc/network/tcp_socket.hpp @@ -52,9 +52,10 @@ namespace fc { tcp_server(); ~tcp_server(); - void close(); - void accept( tcp_socket& s ); - void listen( uint16_t port ); + void close(); + void accept( tcp_socket& s ); + void listen( uint16_t port ); + void listen( const fc::ip::endpoint& ep ); uint16_t get_port()const; private: diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index 4ca10b5..74297b6 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -59,9 +59,12 @@ namespace fc { class tcp_server::impl { public: - impl(uint16_t port): - _accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ){ - } + impl(uint16_t port) + :_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ){} + + impl(const fc::ip::endpoint& ep ) + :_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4( ep.get_address()), ep.port()) ){} + ~impl(){ try { _accept.close(); @@ -100,6 +103,11 @@ namespace fc { if( my ) delete my; my = new impl(port); } + void tcp_server::listen( const fc::ip::endpoint& ep ) + { + if( my ) delete my; + my = new impl(ep); + } uint16_t tcp_server::get_port()const { From 0ecd66fa736e66c3000f7b9b5bccd417d728ae99 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 15 Feb 2014 01:22:59 -0500 Subject: [PATCH 3/6] adding utility methods for getting home dir and app dir --- include/fc/filesystem.hpp | 12 +++++++-- src/filesystem.cpp | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 71c04eb..bd091b5 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -135,8 +135,16 @@ namespace fc { void create_hard_link( const path& from, const path& to ); - path unique_path(); - path temp_directory_path(); + path unique_path(); + path temp_directory_path(); + + /** @return the home directory on Linux and OS X and the Profile directory on Windows */ + const path& home_path(); + + /** @return the home_path() on Linux, home_path()/Library/Application Support/ on OS X, + * and APPDATA on windows + */ + const path& app_path(); class variant; void to_variant( const fc::path&, fc::variant& ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index de160f6..9448016 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -13,6 +13,10 @@ #ifdef WIN32 #include +#else + #include + #include + #include #endif namespace fc { @@ -365,4 +369,51 @@ namespace fc { { _path = fc::optional(); } + + const fc::path& home_path() + { + static fc::path p = []() + { + #ifdef WIN32 + char* home = getenv( "USERPROFILE" ); + if( nullptr == home ) + { + FC_ASSERT( home != nullptr, "The USERPROFILE environment variable is not set" ); + } + return fc::path( home ); + #else + char* home = getenv( "HOME" ); + if( nullptr == home ) + { + struct passwd* pwd = getpwuid(getuid()); + if( pwd ) + { + return fc::path( std::string( pwd->pw_dir ) ); + } + FC_ASSERT( home != nullptr, "The HOME environment variable is not set" ); + } + return fc::path( std::string(home) ); + #endif + }(); + return p; + } + + const fc::path& app_dir() + { + #ifdef __APPLE__ + static fc::path appdir = [](){ return home_path() / "Library" / "Application Support"; }(); + #elif defined( WIN32 ) + static fc::path appdir = [](){ + char* appdata = getenv( "APPDATA" ); + if( nullptr == appdata ) + { + FC_ASSERT( appdata != nullptr, "The APPDATA environment variable is not set" ); + } + return fc::path( std::string(appdata) ); + }(); + #else + static fc::path appdir = home_dir(); + #endif + return appdir; + } } From 00907d8f5779ae638b9b67628258797dc9216ef6 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 15 Feb 2014 01:29:21 -0500 Subject: [PATCH 4/6] fix app_path --- src/filesystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 9448016..9d33849 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -398,7 +398,7 @@ namespace fc { return p; } - const fc::path& app_dir() + const fc::path& app_path() { #ifdef __APPLE__ static fc::path appdir = [](){ return home_path() / "Library" / "Application Support"; }(); From ded475f45ab7ced1fa49d1904ab3ec63a9a342f6 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 15 Feb 2014 01:52:19 -0500 Subject: [PATCH 5/6] upgraded to boost::signals2 because signals is deprecated --- include/fc/signals.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fc/signals.hpp b/include/fc/signals.hpp index 53a7692..82f8a04 100644 --- a/include/fc/signals.hpp +++ b/include/fc/signals.hpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -6,20 +6,20 @@ namespace fc { #if !defined(BOOST_NO_TEMPLATE_ALIASES) template - using signal = boost::signal; + using signal = boost::signals2::signal; #else #endif template - inline T wait( boost::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { + inline T wait( boost::signals2::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { typename promise::ptr p(new promise()); - boost::signals::scoped_connection c = sig.connect( [=]( T t ) { p->set_value(t); } ); + boost::signals2::scoped_connection c( sig.connect( [=]( T t ) { p->set_value(t); } )); return p->wait( timeout_us ); } - inline void wait( boost::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { + inline void wait( boost::signals2::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { promise::ptr p(new promise()); - boost::signals::scoped_connection c = sig.connect( [=]() { p->set_value(); } ); + boost::signals2::scoped_connection c( sig.connect( [=]() { p->set_value(); } )); p->wait( timeout_us ); } } From 2743b56b56e24d953272d5e2b5cef3cf266cf7a6 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 15 Feb 2014 14:06:35 -0500 Subject: [PATCH 6/6] added utility to hide console echo for entering passwords --- include/fc/io/console.hpp | 8 +++++++ src/io/console.cpp | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 include/fc/io/console.hpp create mode 100644 src/io/console.cpp diff --git a/include/fc/io/console.hpp b/include/fc/io/console.hpp new file mode 100644 index 0000000..11a9d47 --- /dev/null +++ b/include/fc/io/console.hpp @@ -0,0 +1,8 @@ +#pragma once +namespace fc +{ + /** enables / disables echoing of console input, useful for + * entering passwords on the console. + */ + void set_console_echo( bool enable_echo ); +} // namespace fc diff --git a/src/io/console.cpp b/src/io/console.cpp new file mode 100644 index 0000000..74e8d42 --- /dev/null +++ b/src/io/console.cpp @@ -0,0 +1,46 @@ +#include +#include + +namespace fc { + +#ifdef WIN32 +#include + +void set_console_echo( bool enable_echo ) +{ + auto stdin_handle = GetStdHandle( STD_INPUT_HANDLE ); + DWORD mode = 0; + GetConsoleMode( stdin_handle, &mode ); + if( enable_echo ) + { + SetConsoleMode( stdin_handle, mode | ENABLE_ECHO_INPUT ); + } + else + { + SetConsoleMode( stdin_handle, mode & (~ENABLE_ECHO_INPUT) ); + } +} + +#else // NOT WIN32 +#include +#include + +void set_console_echo( bool enable_echo ) +{ + termios oldt; + tcgetattr(STDIN_FILENO, &oldt); + termios newt = oldt; + if( enable_echo ) + { + newt.c_lflag |= ~ECHO; + } + else + { + newt.c_lflag &= ~ECHO; + } + tcsetattr(STDIN_FILENO, TCSANOW, &newt); +} + +#endif // WIN32 + +} // namespace fc