From ad1ee958b0f3b16f443c7f6170c26eaefe1861d4 Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Tue, 7 Sep 2021 15:11:28 -0400 Subject: [PATCH] Code cleaning. --- common/kicad_curl/kicad_curl.cpp | 4 +- common/kicad_curl/kicad_curl_easy.cpp | 7 ++-- include/kicad_curl/kicad_curl.h | 28 ++++++------- include/kicad_curl/kicad_curl_easy.h | 58 +++++++++++---------------- kicad/pcm/pcm.cpp | 7 +++- 5 files changed, 48 insertions(+), 56 deletions(-) diff --git a/common/kicad_curl/kicad_curl.cpp b/common/kicad_curl/kicad_curl.cpp index 5cc2843ce6..246d96c281 100644 --- a/common/kicad_curl/kicad_curl.cpp +++ b/common/kicad_curl/kicad_curl.cpp @@ -3,7 +3,7 @@ * * Copyright (C) 2015 Mark Roszko * Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck - * Copyright (C) 2015-2020 KiCad Developers, see CHANGELOG.TXT for contributors. + * Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -132,11 +132,13 @@ std::string KICAD_CURL::GetSimpleVersion() return res; } + std::string GetKicadCurlVersion() { return KICAD_CURL::GetVersion(); } + std::string GetCurlLibVersion() { return LIBCURL_VERSION; diff --git a/common/kicad_curl/kicad_curl_easy.cpp b/common/kicad_curl/kicad_curl_easy.cpp index a6175f009b..217c38ae55 100644 --- a/common/kicad_curl/kicad_curl_easy.cpp +++ b/common/kicad_curl/kicad_curl_easy.cpp @@ -63,6 +63,7 @@ static size_t write_callback( void* contents, size_t size, size_t nmemb, void* u return realsize; } + static size_t stream_write_callback( void* contents, size_t size, size_t nmemb, void* userp ) { size_t realsize = size * nmemb; @@ -76,7 +77,7 @@ static size_t stream_write_callback( void* contents, size_t size, size_t nmemb, static int xferinfo( void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, - curl_off_t ulnow ) + curl_off_t ulnow ) { CURL_PROGRESS* progress = (CURL_PROGRESS*) p; curl_off_t curtime = 0; @@ -92,9 +93,10 @@ static int xferinfo( void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t u return CURLE_OK; } + KICAD_CURL_EASY::KICAD_CURL_EASY() : m_headers( nullptr ) { - // Call KICAD_CURL::Init() from in here everytime, but only the first time + // Call KICAD_CURL::Init() from in here every time, but only the first time // will incur any overhead. This strategy ensures that libcurl is never loaded // unless it is needed. @@ -224,7 +226,6 @@ std::string KICAD_CURL_EASY::Escape( const std::string& aUrl ) } - bool KICAD_CURL_EASY::SetTransferCallback( const TRANSFER_CALLBACK& aCallback, size_t aInterval ) { progress = std::make_unique( this, aCallback, (curl_off_t) aInterval ); diff --git a/include/kicad_curl/kicad_curl.h b/include/kicad_curl/kicad_curl.h index 30d60bde68..d638572e15 100644 --- a/include/kicad_curl/kicad_curl.h +++ b/include/kicad_curl/kicad_curl.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2015 Mark Roszko - * Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors. + * Copyright (C) 2015, 2021 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -58,36 +58,30 @@ struct DYN_LOOKUP; /** - * KICAD_CURL - * simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad. + * Simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad. */ class KICAD_CURL { - friend class KICAD_CURL_EASY; - public: /** - * Function Init - * calls curl_global_init for the application. It must be used only once + * Call curl_global_init for the application. It must be used only once * and before any curl functions that perform requests. * - * @return bool - True if successful, false if CURL returned an error + * @return True if successful, false if CURL returned an error. * @throw IO_ERROR on failure, hopefully with helpful text in it. */ static void Init(); /** - * Function Cleanup - * calls curl_global_cleanup for the application. It must be used only after + * Call curl_global_cleanup for the application. It must be used only after * curl_global_init was called. */ static void Cleanup(); /** - * Function GetVersion - * wrapper for curl_version(). Reports back a short string of loaded libraries. + * Wrapper for curl_version(). Reports back a short string of loaded libraries. * - * @return const char* - String reported by libcurl and owned by it. + * @return String reported by libcurl and owned by it. * @throw IO_ERROR on failure, hopefully with helpful text in it. */ static const char* GetVersion() @@ -97,12 +91,14 @@ public: /** - * Function GetSimpleVersion - * Reports back curl version only and SSL library support + * Report back curl version only and SSL library support. * - * @return std::string - Generated version string + * @return Generated version string. */ static std::string GetSimpleVersion(); + +private: + friend class KICAD_CURL_EASY; }; #endif // KICAD_CURL_H_ diff --git a/include/kicad_curl/kicad_curl_easy.h b/include/kicad_curl/kicad_curl_easy.h index 7a87ca6c68..3c1d6bc86f 100644 --- a/include/kicad_curl/kicad_curl_easy.h +++ b/include/kicad_curl/kicad_curl_easy.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2015 Mark Roszko - * Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors. + * Copyright (C) 2015, 2021 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -42,8 +42,7 @@ typedef void CURL; struct curl_slist; /** - * KICAD_CURL_EASY - * wrapper interface around the curl_easy API + * Wrapper interface around the curl_easy API/ * * Handling of using the curl_easy API to make a request and save the response to * a memory buffer @@ -71,63 +70,55 @@ public: ~KICAD_CURL_EASY(); /** - * Function perform - * equivalent to curl_easy_perform. Executes the request - * that was previously setup. + * Equivalent to curl_easy_perform. Executes the request that was previously setup. */ int Perform(); /** - * Function SetHeader - * sets an arbitrary header for the HTTP(s) request. + * Set an arbitrary header for the HTTP(s) request. * - * @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 + * @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 ); /** - * Function SetUserAgent - * sets the request user agent + * Set the request user agent. * - * @param aAgent is the string to set for the user agent - * @return bool - True if successful, false if not + * @param aAgent is the string to set for the user agent. + * @return True if successful, false if not. */ bool SetUserAgent( const std::string& aAgent ); /** - * Function SetURL - * sets the request URL + * Set the request URL. * - * @param aURL is the URL - * @return bool - True if successful, false if not + * @param aURL is the URL. + * @return True if successful, false if not. */ bool SetURL( const std::string& aURL ); /** - * Function SetFollowRedirects - * enables the following of HTTP(s) and other redirects, by default curl + * Enable the following of HTTP(s) and other redirects, by default curl * does not follow redirects. * - * @param aFollow is a boolean where true will enable following redirects - * @return bool - True if successful, false if not + * @param aFollow is a boolean where true will enable following redirects. + * @return True if successful, false if not. */ bool SetFollowRedirects( bool aFollow ); /** - * Function GetErrorText - * fetches CURL's "friendly" error string for a given error code + * Fetch CURL's "friendly" error string for a given error code. * - * @param aCode is CURL error code - * @return const std::string - the corresponding error string for the given code + * @param aCode is CURL error code. + * @return The corresponding error string for the given code. */ const std::string GetErrorText( int aCode ); int GetTransferTotal( uint64_t& aDownloadedBytes ) const; /** - * Function GetBuffer - * returns a const reference to the received data buffer + * Return a reference to the received data buffer. */ const std::string& GetBuffer() { return m_buffer; } @@ -143,18 +134,15 @@ public: private: /** - * Function setOption - * sets a curl option, only supports single parameter curl options + * Set 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 + * @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 A CURL error code, will return CURLE_OK unless a problem was encountered. */ template int setOption( int aOption, T aArg ); - //static int xferinfo( void*, curl_off_t, curl_off_t, curl_off_t, curl_off_t ); - CURL* m_CURL; curl_slist* m_headers; std::string m_buffer; diff --git a/kicad/pcm/pcm.cpp b/kicad/pcm/pcm.cpp index 8ac6904f29..9e4afd9190 100644 --- a/kicad/pcm/pcm.cpp +++ b/kicad/pcm/pcm.cpp @@ -76,7 +76,8 @@ PLUGIN_CONTENT_MANAGER::PLUGIN_CONTENT_MANAGER( wxWindow* aParent ) : m_dialog( catch( std::exception& e ) { if( !schema_file.FileExists() ) - wxLogError( wxString::Format( _( "schema file '%s' not found" ), schema_file.GetFullPath() ) ); + wxLogError( wxString::Format( _( "schema file '%s' not found" ), + schema_file.GetFullPath() ) ); else wxLogError( wxString::Format( _( "Error loading schema: %s" ), e.what() ) ); } @@ -174,6 +175,7 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea if( aSizeLimit > 0 && ( dltotal > aSizeLimit || dlnow > aSizeLimit ) ) { size_exceeded = true; + // Non zero return means abort. return true; } @@ -188,6 +190,7 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea { reporter->SetCurrentProgress( 0.0 ); } + return !reporter->KeepRefreshing(); }; @@ -226,6 +229,7 @@ bool PLUGIN_CONTENT_MANAGER::FetchRepository( const wxString& aUrl, PCM_REPOSITO } nlohmann::json repository_json; + try { repository_stream >> repository_json; @@ -257,6 +261,7 @@ bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUr std::vector& aPackages ) { std::stringstream packages_stream; + if( !DownloadToStream( aUrl, &packages_stream, _( "Fetching repository packages" ) ) ) { wxLogError( _( "Unable to load repository packages url." ) );