From 8c70681b31bd8d5372102aee6011a8e4c0aaa337 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Mon, 4 Sep 2017 15:42:56 +0200 Subject: [PATCH] Remove useless includes and more fixes to avoid including curl.h in files, when not mandatory. --- common/kicad_curl/kicad_curl.cpp | 11 +--- common/kicad_curl/kicad_curl_easy.cpp | 56 ++++++++++++++++++-- include/kicad_curl/kicad_curl_easy.h | 73 +++++++++------------------ 3 files changed, 79 insertions(+), 61 deletions(-) diff --git a/common/kicad_curl/kicad_curl.cpp b/common/kicad_curl/kicad_curl.cpp index 79f29a8e3f..8d75df886c 100644 --- a/common/kicad_curl/kicad_curl.cpp +++ b/common/kicad_curl/kicad_curl.cpp @@ -27,13 +27,8 @@ // conflicts for some defines, at least on Windows #include -#include -#include - -#include -#include #include // MUTEX and MUTLOCK -#include +#include // THROW_IO_ERROR @@ -58,8 +53,6 @@ static void lock_callback( int mode, int type, const char* file, int line ) wxASSERT( s_crypto_locks && unsigned( type ) < unsigned( CRYPTO_num_locks() ) ); - //DBG( printf( "%s: mode=0x%x type=%d file=%s line=%d\n", __func__, mode, type, file, line );) - if( mode & CRYPTO_LOCK ) { s_crypto_locks[ type ].lock(); @@ -150,7 +143,7 @@ void KICAD_CURL::Init() init_locks(); - wxLogDebug( "Using %s", GetVersion() ); + //wxLogDebug( "Using %s", GetVersion() ); s_initialized = true; } diff --git a/common/kicad_curl/kicad_curl_easy.cpp b/common/kicad_curl/kicad_curl_easy.cpp index 1e84afedf5..18c6f96e55 100644 --- a/common/kicad_curl/kicad_curl_easy.cpp +++ b/common/kicad_curl/kicad_curl_easy.cpp @@ -22,13 +22,14 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ +#include #include #include #include #include #include -#include +#include // THROW_IO_ERROR static size_t write_callback( void* contents, size_t size, size_t nmemb, void* userp ) @@ -87,8 +88,57 @@ void KICAD_CURL_EASY::Perform() if( res != CURLE_OK ) { - std::string msg = StrPrintf( "curl_easy_perform()=%d: %s", - res, GetErrorText( res ).c_str() ); + std::string msg = "curl_easy_perform()="; + msg += (int)res; msg += " "; msg += GetErrorText( res ).c_str(); THROW_IO_ERROR( msg ); } } + + +void KICAD_CURL_EASY::SetHeader( const std::string& aName, const std::string& aValue ) +{ + std::string header = aName + ':' + aValue; + m_headers = curl_slist_append( m_headers, header.c_str() ); +} + + +template int KICAD_CURL_EASY::setOption( int aOption, T aArg ) +{ + return curl_easy_setopt( m_CURL, (CURLoption)aOption, aArg ); +} + + +const std::string KICAD_CURL_EASY::GetErrorText( int aCode ) +{ + return curl_easy_strerror( (CURLcode)aCode ); +} + + +bool KICAD_CURL_EASY::SetUserAgent( const std::string& aAgent ) +{ + if( setOption( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK ) + { + return true; + } + return false; +} + + +bool KICAD_CURL_EASY::SetURL( const std::string& aURL ) +{ + if( setOption( CURLOPT_URL, aURL.c_str() ) == CURLE_OK ) + { + return true; + } + return false; +} + + +bool KICAD_CURL_EASY::SetFollowRedirects( bool aFollow ) +{ + if( setOption( CURLOPT_FOLLOWLOCATION , (aFollow ? 1 : 0) ) == CURLE_OK ) + { + return true; + } + return false; +} diff --git a/include/kicad_curl/kicad_curl_easy.h b/include/kicad_curl/kicad_curl_easy.h index 245db8f09a..b5bb134168 100644 --- a/include/kicad_curl/kicad_curl_easy.h +++ b/include/kicad_curl/kicad_curl_easy.h @@ -26,13 +26,16 @@ /* * KICAD_CURL_EASY.h must included before wxWidgets because on Windows, - * because kicad_curl.h includes curl.h, wxWidgets ends up including windows.h - * before winsocks2.h inside curl and this causes build warnings + * kicad_curl.h does not include curl.h, because wxWidgets ends up including windows.h + * before winsocks2.h inside curl and curl.h causes build warnings if included before and wxxxx.h + * + * so including kicad_curl.h could be needed in a few sources */ -#include #include +typedef void CURL; +struct curl_slist; /** * Class KICAD_CURL_EASY @@ -51,6 +54,8 @@ * curl.Perform(); * @endcode */ + + class KICAD_CURL_EASY { public: @@ -73,11 +78,7 @@ public: * @param aName is the left hand side of the header, i.e. Accept without the colon * @param aValue is the right hand side of the header, i.e. application/json */ - void SetHeader( const std::string& aName, const std::string& aValue ) - { - std::string header = aName + ':' + aValue; - m_headers = curl_slist_append( m_headers, header.c_str() ); - } + void SetHeader( const std::string& aName, const std::string& aValue ); /** * Function SetUserAgent @@ -86,14 +87,7 @@ public: * @param aAgent is the string to set for the user agent * @return bool - True if successful, false if not */ - bool SetUserAgent( const std::string& aAgent ) - { - if( SetOption( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK ) - { - return true; - } - return false; - } + bool SetUserAgent( const std::string& aAgent ); /** * Function SetURL @@ -102,14 +96,7 @@ public: * @param aURL is the URL * @return bool - True if successful, false if not */ - bool SetURL( const std::string& aURL ) - { - if( SetOption( CURLOPT_URL, aURL.c_str() ) == CURLE_OK ) - { - return true; - } - return false; - } + bool SetURL( const std::string& aURL ); /** * Function SetFollowRedirects @@ -119,14 +106,7 @@ public: * @param aFollow is a boolean where true will enable following redirects * @return bool - True if successful, false if not */ - bool SetFollowRedirects( bool aFollow ) - { - if( SetOption( CURLOPT_FOLLOWLOCATION , (aFollow ? 1 : 0) ) == CURLE_OK ) - { - return true; - } - return false; - } + bool SetFollowRedirects( bool aFollow ); /** * Function GetErrorText @@ -135,23 +115,7 @@ public: * @param aCode is CURL error code * @return const std::string - the corresponding error string for the given code */ - const std::string GetErrorText( CURLcode aCode ) - { - return curl_easy_strerror( aCode ); - } - - /** - * Function SetOption - * sets a curl option, only supports single parameter curl options - * - * @param aOption is CURL option, see CURL manual for options - * @param aArg is the argument being passed to CURL, ensure it is the right type per manual - * @return CURLcode - CURL error code, will return CURLE_OK unless a problem was encountered - */ - template CURLcode SetOption( CURLoption aOption, T aArg ) - { - return curl_easy_setopt( m_CURL, aOption, aArg ); - } + const std::string GetErrorText( int aCode ); /** * Function GetBuffer @@ -163,6 +127,17 @@ public: } private: + /** + * Function setOption + * sets a curl option, only supports single parameter curl options + * + * @param aOption is CURL option, see CURL manual for options + * @param aArg is the argument being passed to CURL, ensure it is the right type per manual + * @return int - a CURL error code, will return CURLE_OK unless a problem was encountered + */ + template int setOption( int aOption, T aArg ); + + CURL* m_CURL; curl_slist* m_headers; std::string m_buffer;