From ec073ee188c40b7430955fb4fc4213a75480b2d9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 8 Aug 2019 12:32:21 +0200 Subject: [PATCH] [libcalamares] Apply coding style to geoip/ --- src/libcalamares/geoip/GeoIPJSON.cpp | 28 +++++++++------ src/libcalamares/geoip/GeoIPJSON.h | 8 ++--- src/libcalamares/geoip/GeoIPXML.cpp | 14 ++++++-- src/libcalamares/geoip/GeoIPXML.h | 8 ++--- src/libcalamares/geoip/Handler.cpp | 50 +++++++++++++-------------- src/libcalamares/geoip/Handler.h | 9 +++-- src/libcalamares/geoip/Interface.cpp | 10 +++--- src/libcalamares/geoip/Interface.h | 30 ++++++++++------ src/libcalamares/geoip/test_geoip.cpp | 29 ++++++++++------ 9 files changed, 106 insertions(+), 80 deletions(-) diff --git a/src/libcalamares/geoip/GeoIPJSON.cpp b/src/libcalamares/geoip/GeoIPJSON.cpp index 4b7562d7e..85dc79619 100644 --- a/src/libcalamares/geoip/GeoIPJSON.cpp +++ b/src/libcalamares/geoip/GeoIPJSON.cpp @@ -30,7 +30,7 @@ namespace CalamaresUtils namespace GeoIP { -GeoIPJSON::GeoIPJSON(const QString& attribute) +GeoIPJSON::GeoIPJSON( const QString& attribute ) : Interface( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute ) { } @@ -42,19 +42,25 @@ GeoIPJSON::GeoIPJSON(const QString& attribute) * "foo" of @p m, like a regular JSON lookup would. */ static QString -selectMap( const QVariantMap& m, const QStringList& l, int index) +selectMap( const QVariantMap& m, const QStringList& l, int index ) { if ( index >= l.count() ) + { return QString(); + } - QString attributeName = l[index]; + QString attributeName = l[ index ]; if ( index == l.count() - 1 ) + { return CalamaresUtils::getString( m, attributeName ); + } else { bool success = false; // bogus if ( m.contains( attributeName ) ) - return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index+1 ); + { + return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index + 1 ); + } return QString(); } } @@ -67,18 +73,18 @@ GeoIPJSON::rawReply( const QByteArray& data ) YAML::Node doc = YAML::Load( data ); QVariant var = CalamaresUtils::yamlToVariant( doc ); - if ( !var.isNull() && - var.isValid() && - var.type() == QVariant::Map ) + if ( !var.isNull() && var.isValid() && var.type() == QVariant::Map ) { - return selectMap( var.toMap(), m_element.split('.'), 0 ); + return selectMap( var.toMap(), m_element.split( '.' ), 0 ); } else + { cWarning() << "Invalid YAML data for GeoIPJSON"; + } } catch ( YAML::Exception& e ) { - CalamaresUtils::explainYamlException( e, data, "GeoIP data"); + CalamaresUtils::explainYamlException( e, data, "GeoIP data" ); } return QString(); @@ -90,5 +96,5 @@ GeoIPJSON::processReply( const QByteArray& data ) return splitTZString( rawReply( data ) ); } -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils diff --git a/src/libcalamares/geoip/GeoIPJSON.h b/src/libcalamares/geoip/GeoIPJSON.h index 4d7ded631..3f7756dd8 100644 --- a/src/libcalamares/geoip/GeoIPJSON.h +++ b/src/libcalamares/geoip/GeoIPJSON.h @@ -45,9 +45,9 @@ public: explicit GeoIPJSON( const QString& attribute = QString() ); virtual RegionZonePair processReply( const QByteArray& ) override; - virtual QString rawReply(const QByteArray & ) override; -} ; + virtual QString rawReply( const QByteArray& ) override; +}; -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils #endif diff --git a/src/libcalamares/geoip/GeoIPXML.cpp b/src/libcalamares/geoip/GeoIPXML.cpp index b658042bf..d84eb3b21 100644 --- a/src/libcalamares/geoip/GeoIPXML.cpp +++ b/src/libcalamares/geoip/GeoIPXML.cpp @@ -48,10 +48,12 @@ getElementTexts( const QByteArray& data, const QString& tag ) cDebug() << "GeoIP found" << tzElements.length() << "elements"; for ( int it = 0; it < tzElements.length(); ++it ) { - auto e = tzElements.at(it).toElement(); + auto e = tzElements.at( it ).toElement(); auto e_text = e.text(); if ( !e_text.isEmpty() ) + { elements.append( e_text ); + } } } else @@ -60,7 +62,9 @@ getElementTexts( const QByteArray& data, const QString& tag ) } if ( elements.count() < 1 ) + { cWarning() << "GeopIP XML had no non-empty elements" << tag; + } return elements; } @@ -71,7 +75,9 @@ GeoIPXML::rawReply( const QByteArray& data ) { for ( const auto& e : getElementTexts( data, m_element ) ) if ( !e.isEmpty() ) + { return e; + } return QString(); } @@ -83,11 +89,13 @@ GeoIPXML::processReply( const QByteArray& data ) { auto tz = splitTZString( e ); if ( !tz.first.isEmpty() ) + { return tz; + } } return RegionZonePair(); } -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils diff --git a/src/libcalamares/geoip/GeoIPXML.h b/src/libcalamares/geoip/GeoIPXML.h index 356e88b12..73147ff91 100644 --- a/src/libcalamares/geoip/GeoIPXML.h +++ b/src/libcalamares/geoip/GeoIPXML.h @@ -45,9 +45,9 @@ public: explicit GeoIPXML( const QString& element = QString() ); virtual RegionZonePair processReply( const QByteArray& ) override; - virtual QString rawReply(const QByteArray & ) override; -} ; + virtual QString rawReply( const QByteArray& ) override; +}; -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils #endif diff --git a/src/libcalamares/geoip/Handler.cpp b/src/libcalamares/geoip/Handler.cpp index 21b88dd4b..6017c404f 100644 --- a/src/libcalamares/geoip/Handler.cpp +++ b/src/libcalamares/geoip/Handler.cpp @@ -19,7 +19,7 @@ #include "Handler.h" #include "GeoIPJSON.h" -#if defined(QT_XML_LIB) +#if defined( QT_XML_LIB ) #include "GeoIPXML.h" #endif @@ -28,8 +28,8 @@ #include "utils/Variant.h" #include -#include #include +#include #include @@ -76,7 +76,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri { cWarning() << "GeoIP style *none* does not do anything."; } -#if !defined(QT_XML_LIB) +#if !defined( QT_XML_LIB ) else if ( m_type == Type::XML ) { m_type = Type::None; @@ -85,9 +85,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri #endif } -Handler::~Handler() -{ -} +Handler::~Handler() {} static QByteArray synchronous_get( const QString& urlstring ) @@ -108,17 +106,17 @@ synchronous_get( const QString& urlstring ) static std::unique_ptr< Interface > create_interface( Handler::Type t, const QString& selector ) { - switch( t ) + switch ( t ) { - case Handler::Type::None: - return nullptr; - case Handler::Type::JSON: - return std::make_unique< GeoIPJSON >( selector ); - case Handler::Type::XML: -#if defined(QT_XML_LIB) - return std::make_unique< GeoIPXML >( selector ); + case Handler::Type::None: + return nullptr; + case Handler::Type::JSON: + return std::make_unique< GeoIPJSON >( selector ); + case Handler::Type::XML: +#if defined( QT_XML_LIB ) + return std::make_unique< GeoIPXML >( selector ); #else - return nullptr; + return nullptr; #endif } NOTREACHED return nullptr; @@ -129,7 +127,9 @@ do_query( Handler::Type type, const QString& url, const QString& selector ) { const auto interface = create_interface( type, selector ); if ( !interface ) + { return RegionZonePair(); + } return interface->processReply( synchronous_get( url ) ); } @@ -139,7 +139,9 @@ do_raw_query( Handler::Type type, const QString& url, const QString& selector ) { const auto interface = create_interface( type, selector ); if ( !interface ) + { return QString(); + } return interface->rawReply( synchronous_get( url ) ); } @@ -148,7 +150,9 @@ RegionZonePair Handler::get() const { if ( !isValid() ) + { return RegionZonePair(); + } return do_query( m_type, m_url, m_selector ); } @@ -160,17 +164,16 @@ Handler::query() const QString url = m_url; QString selector = m_selector; - return QtConcurrent::run( [=] - { - return do_query( type, url, selector ); - } ); + return QtConcurrent::run( [=] { return do_query( type, url, selector ); } ); } QString Handler::getRaw() const { if ( !isValid() ) + { return QString(); + } return do_raw_query( m_type, m_url, m_selector ); } @@ -182,11 +185,8 @@ Handler::queryRaw() const QString url = m_url; QString selector = m_selector; - return QtConcurrent::run( [=] - { - return do_raw_query( type, url, selector ); - } ); + return QtConcurrent::run( [=] { return do_raw_query( type, url, selector ); } ); } -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils diff --git a/src/libcalamares/geoip/Handler.h b/src/libcalamares/geoip/Handler.h index 8e435b5b7..518964caf 100644 --- a/src/libcalamares/geoip/Handler.h +++ b/src/libcalamares/geoip/Handler.h @@ -21,9 +21,9 @@ #include "Interface.h" -#include #include #include +#include namespace CalamaresUtils { @@ -46,7 +46,7 @@ public: None, JSON, XML - } ; + }; /** @brief An unconfigured handler; this always returns errors. */ Handler(); @@ -89,7 +89,6 @@ private: const QString m_selector; }; -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils #endif - diff --git a/src/libcalamares/geoip/Interface.cpp b/src/libcalamares/geoip/Interface.cpp index 36e680aab..2cecb63c5 100644 --- a/src/libcalamares/geoip/Interface.cpp +++ b/src/libcalamares/geoip/Interface.cpp @@ -25,14 +25,12 @@ namespace CalamaresUtils namespace GeoIP { -Interface::Interface(const QString& e) +Interface::Interface( const QString& e ) : m_element( e ) { } -Interface::~Interface() -{ -} +Interface::~Interface() {} RegionZonePair splitTZString( const QString& tz ) @@ -53,5 +51,5 @@ splitTZString( const QString& tz ) return RegionZonePair( QString(), QString() ); } -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils diff --git a/src/libcalamares/geoip/Interface.h b/src/libcalamares/geoip/Interface.h index 7db8c4c91..1a9beaa41 100644 --- a/src/libcalamares/geoip/Interface.h +++ b/src/libcalamares/geoip/Interface.h @@ -27,7 +27,7 @@ class QByteArray; -namespace CalamaresUtils +namespace CalamaresUtils { namespace GeoIP { @@ -38,18 +38,27 @@ namespace GeoIP * pasting the strings back together with a "/" is the right thing to * do. The Zone **may** contain a "/" (e.g. "Kentucky/Monticello"). */ -class DLLEXPORT RegionZonePair : public QPair +class DLLEXPORT RegionZonePair : public QPair< QString, QString > { public: /** @brief Construct from an existing pair. */ - explicit RegionZonePair( const QPair& p ) : QPair(p) { } + explicit RegionZonePair( const QPair& p ) + : QPair( p ) + { + } /** @brief Construct from two strings, like qMakePair(). */ - RegionZonePair( const QString& region, const QString& zone ) : QPair( region, zone ) { } + RegionZonePair( const QString& region, const QString& zone ) + : QPair( region, zone ) + { + } /** @brief An invalid zone pair (empty strings). */ - RegionZonePair() : QPair( QString(), QString() ) { } + RegionZonePair() + : QPair( QString(), QString() ) + { + } bool isValid() const { return !first.isEmpty(); } -} ; +}; /** @brief Splits a region/zone string into a pair. * @@ -60,8 +69,7 @@ public: * pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah * will return "America", "North_Dakota/Beulah"). */ -DLLEXPORT RegionZonePair -splitTZString( const QString& s ); +DLLEXPORT RegionZonePair splitTZString( const QString& s ); /** * @brief Interface for GeoIP retrievers. @@ -93,8 +101,8 @@ protected: Interface( const QString& e = QString() ); QString m_element; // string for selecting from data -} ; +}; -} -} // namespace +} // namespace GeoIP +} // namespace CalamaresUtils #endif diff --git a/src/libcalamares/geoip/test_geoip.cpp b/src/libcalamares/geoip/test_geoip.cpp index 5f7ab935c..32c6f4e24 100644 --- a/src/libcalamares/geoip/test_geoip.cpp +++ b/src/libcalamares/geoip/test_geoip.cpp @@ -30,34 +30,40 @@ using std::cerr; using namespace CalamaresUtils::GeoIP; -int main(int argc, char** argv) +int +main( int argc, char** argv ) { - if (argc != 2) + if ( argc != 2 ) { cerr << "Usage: curl url | test_geoip \n"; return 1; } Interface* handler = nullptr; - if ( QStringLiteral( "json" ) == argv[1] ) + if ( QStringLiteral( "json" ) == argv[ 1 ] ) + { handler = new GeoIPJSON; + } #ifdef QT_XML_LIB - else if ( QStringLiteral( "xml" ) == argv[1] ) + else if ( QStringLiteral( "xml" ) == argv[ 1 ] ) + { handler = new GeoIPXML; + } #endif if ( !handler ) { - cerr << "Unknown format '" << argv[1] << "'\n"; + cerr << "Unknown format '" << argv[ 1 ] << "'\n"; return 1; } QByteArray ba; - while( !std::cin.eof() ) { - char arr[1024]; - std::cin.read(arr, sizeof(arr)); - int s = static_cast( std::cin.gcount() ); - ba.append(arr, s); + while ( !std::cin.eof() ) + { + char arr[ 1024 ]; + std::cin.read( arr, sizeof( arr ) ); + int s = static_cast< int >( std::cin.gcount() ); + ba.append( arr, s ); } auto tz = handler->processReply( ba ); @@ -67,7 +73,8 @@ int main(int argc, char** argv) } else { - std::cout << "TimeZone Region=" << tz.first.toLatin1().constData() << "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n'; + std::cout << "TimeZone Region=" << tz.first.toLatin1().constData() + << "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n'; } return 0;