[libcalamares] Apply coding style to geoip/

main
Adriaan de Groot 6 years ago
parent a2ab91474f
commit ec073ee188

@ -30,7 +30,7 @@ namespace CalamaresUtils
namespace GeoIP namespace GeoIP
{ {
GeoIPJSON::GeoIPJSON(const QString& attribute) GeoIPJSON::GeoIPJSON( const QString& attribute )
: Interface( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : 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. * "foo" of @p m, like a regular JSON lookup would.
*/ */
static QString static QString
selectMap( const QVariantMap& m, const QStringList& l, int index) selectMap( const QVariantMap& m, const QStringList& l, int index )
{ {
if ( index >= l.count() ) if ( index >= l.count() )
{
return QString(); return QString();
}
QString attributeName = l[index]; QString attributeName = l[ index ];
if ( index == l.count() - 1 ) if ( index == l.count() - 1 )
{
return CalamaresUtils::getString( m, attributeName ); return CalamaresUtils::getString( m, attributeName );
}
else else
{ {
bool success = false; // bogus bool success = false; // bogus
if ( m.contains( attributeName ) ) 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(); return QString();
} }
} }
@ -67,18 +73,18 @@ GeoIPJSON::rawReply( const QByteArray& data )
YAML::Node doc = YAML::Load( data ); YAML::Node doc = YAML::Load( data );
QVariant var = CalamaresUtils::yamlToVariant( doc ); QVariant var = CalamaresUtils::yamlToVariant( doc );
if ( !var.isNull() && if ( !var.isNull() && var.isValid() && var.type() == QVariant::Map )
var.isValid() &&
var.type() == QVariant::Map )
{ {
return selectMap( var.toMap(), m_element.split('.'), 0 ); return selectMap( var.toMap(), m_element.split( '.' ), 0 );
} }
else else
{
cWarning() << "Invalid YAML data for GeoIPJSON"; cWarning() << "Invalid YAML data for GeoIPJSON";
}
} }
catch ( YAML::Exception& e ) catch ( YAML::Exception& e )
{ {
CalamaresUtils::explainYamlException( e, data, "GeoIP data"); CalamaresUtils::explainYamlException( e, data, "GeoIP data" );
} }
return QString(); return QString();
@ -90,5 +96,5 @@ GeoIPJSON::processReply( const QByteArray& data )
return splitTZString( rawReply( data ) ); return splitTZString( rawReply( data ) );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -45,9 +45,9 @@ public:
explicit GeoIPJSON( const QString& attribute = QString() ); explicit GeoIPJSON( const QString& attribute = QString() );
virtual RegionZonePair processReply( const QByteArray& ) override; virtual RegionZonePair processReply( const QByteArray& ) override;
virtual QString rawReply(const QByteArray & ) override; virtual QString rawReply( const QByteArray& ) override;
} ; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -48,10 +48,12 @@ getElementTexts( const QByteArray& data, const QString& tag )
cDebug() << "GeoIP found" << tzElements.length() << "elements"; cDebug() << "GeoIP found" << tzElements.length() << "elements";
for ( int it = 0; it < tzElements.length(); ++it ) 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(); auto e_text = e.text();
if ( !e_text.isEmpty() ) if ( !e_text.isEmpty() )
{
elements.append( e_text ); elements.append( e_text );
}
} }
} }
else else
@ -60,7 +62,9 @@ getElementTexts( const QByteArray& data, const QString& tag )
} }
if ( elements.count() < 1 ) if ( elements.count() < 1 )
{
cWarning() << "GeopIP XML had no non-empty elements" << tag; cWarning() << "GeopIP XML had no non-empty elements" << tag;
}
return elements; return elements;
} }
@ -71,7 +75,9 @@ GeoIPXML::rawReply( const QByteArray& data )
{ {
for ( const auto& e : getElementTexts( data, m_element ) ) for ( const auto& e : getElementTexts( data, m_element ) )
if ( !e.isEmpty() ) if ( !e.isEmpty() )
{
return e; return e;
}
return QString(); return QString();
} }
@ -83,11 +89,13 @@ GeoIPXML::processReply( const QByteArray& data )
{ {
auto tz = splitTZString( e ); auto tz = splitTZString( e );
if ( !tz.first.isEmpty() ) if ( !tz.first.isEmpty() )
{
return tz; return tz;
}
} }
return RegionZonePair(); return RegionZonePair();
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -45,9 +45,9 @@ public:
explicit GeoIPXML( const QString& element = QString() ); explicit GeoIPXML( const QString& element = QString() );
virtual RegionZonePair processReply( const QByteArray& ) override; virtual RegionZonePair processReply( const QByteArray& ) override;
virtual QString rawReply(const QByteArray & ) override; virtual QString rawReply( const QByteArray& ) override;
} ; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -19,7 +19,7 @@
#include "Handler.h" #include "Handler.h"
#include "GeoIPJSON.h" #include "GeoIPJSON.h"
#if defined(QT_XML_LIB) #if defined( QT_XML_LIB )
#include "GeoIPXML.h" #include "GeoIPXML.h"
#endif #endif
@ -28,8 +28,8 @@
#include "utils/Variant.h" #include "utils/Variant.h"
#include <QEventLoop> #include <QEventLoop>
#include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest>
#include <memory> #include <memory>
@ -76,7 +76,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
{ {
cWarning() << "GeoIP style *none* does not do anything."; cWarning() << "GeoIP style *none* does not do anything.";
} }
#if !defined(QT_XML_LIB) #if !defined( QT_XML_LIB )
else if ( m_type == Type::XML ) else if ( m_type == Type::XML )
{ {
m_type = Type::None; m_type = Type::None;
@ -85,9 +85,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
#endif #endif
} }
Handler::~Handler() Handler::~Handler() {}
{
}
static QByteArray static QByteArray
synchronous_get( const QString& urlstring ) synchronous_get( const QString& urlstring )
@ -108,17 +106,17 @@ synchronous_get( const QString& urlstring )
static std::unique_ptr< Interface > static std::unique_ptr< Interface >
create_interface( Handler::Type t, const QString& selector ) create_interface( Handler::Type t, const QString& selector )
{ {
switch( t ) switch ( t )
{ {
case Handler::Type::None: case Handler::Type::None:
return nullptr; return nullptr;
case Handler::Type::JSON: case Handler::Type::JSON:
return std::make_unique< GeoIPJSON >( selector ); return std::make_unique< GeoIPJSON >( selector );
case Handler::Type::XML: case Handler::Type::XML:
#if defined(QT_XML_LIB) #if defined( QT_XML_LIB )
return std::make_unique< GeoIPXML >( selector ); return std::make_unique< GeoIPXML >( selector );
#else #else
return nullptr; return nullptr;
#endif #endif
} }
NOTREACHED return nullptr; 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 ); const auto interface = create_interface( type, selector );
if ( !interface ) if ( !interface )
{
return RegionZonePair(); return RegionZonePair();
}
return interface->processReply( synchronous_get( url ) ); 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 ); const auto interface = create_interface( type, selector );
if ( !interface ) if ( !interface )
{
return QString(); return QString();
}
return interface->rawReply( synchronous_get( url ) ); return interface->rawReply( synchronous_get( url ) );
} }
@ -148,7 +150,9 @@ RegionZonePair
Handler::get() const Handler::get() const
{ {
if ( !isValid() ) if ( !isValid() )
{
return RegionZonePair(); return RegionZonePair();
}
return do_query( m_type, m_url, m_selector ); return do_query( m_type, m_url, m_selector );
} }
@ -160,17 +164,16 @@ Handler::query() const
QString url = m_url; QString url = m_url;
QString selector = m_selector; QString selector = m_selector;
return QtConcurrent::run( [=] return QtConcurrent::run( [=] { return do_query( type, url, selector ); } );
{
return do_query( type, url, selector );
} );
} }
QString QString
Handler::getRaw() const Handler::getRaw() const
{ {
if ( !isValid() ) if ( !isValid() )
{
return QString(); return QString();
}
return do_raw_query( m_type, m_url, m_selector ); return do_raw_query( m_type, m_url, m_selector );
} }
@ -182,11 +185,8 @@ Handler::queryRaw() const
QString url = m_url; QString url = m_url;
QString selector = m_selector; QString selector = m_selector;
return QtConcurrent::run( [=] return QtConcurrent::run( [=] { return do_raw_query( type, url, selector ); } );
{
return do_raw_query( type, url, selector );
} );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -21,9 +21,9 @@
#include "Interface.h" #include "Interface.h"
#include <QtConcurrent/QtConcurrentRun>
#include <QString> #include <QString>
#include <QVariantMap> #include <QVariantMap>
#include <QtConcurrent/QtConcurrentRun>
namespace CalamaresUtils namespace CalamaresUtils
{ {
@ -46,7 +46,7 @@ public:
None, None,
JSON, JSON,
XML XML
} ; };
/** @brief An unconfigured handler; this always returns errors. */ /** @brief An unconfigured handler; this always returns errors. */
Handler(); Handler();
@ -89,7 +89,6 @@ private:
const QString m_selector; const QString m_selector;
}; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -25,14 +25,12 @@ namespace CalamaresUtils
namespace GeoIP namespace GeoIP
{ {
Interface::Interface(const QString& e) Interface::Interface( const QString& e )
: m_element( e ) : m_element( e )
{ {
} }
Interface::~Interface() Interface::~Interface() {}
{
}
RegionZonePair RegionZonePair
splitTZString( const QString& tz ) splitTZString( const QString& tz )
@ -53,5 +51,5 @@ splitTZString( const QString& tz )
return RegionZonePair( QString(), QString() ); return RegionZonePair( QString(), QString() );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -27,7 +27,7 @@
class QByteArray; class QByteArray;
namespace CalamaresUtils namespace CalamaresUtils
{ {
namespace GeoIP namespace GeoIP
{ {
@ -38,18 +38,27 @@ namespace GeoIP
* pasting the strings back together with a "/" is the right thing to * pasting the strings back together with a "/" is the right thing to
* do. The Zone **may** contain a "/" (e.g. "Kentucky/Monticello"). * do. The Zone **may** contain a "/" (e.g. "Kentucky/Monticello").
*/ */
class DLLEXPORT RegionZonePair : public QPair<QString, QString> class DLLEXPORT RegionZonePair : public QPair< QString, QString >
{ {
public: public:
/** @brief Construct from an existing pair. */ /** @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(). */ /** @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). */ /** @brief An invalid zone pair (empty strings). */
RegionZonePair() : QPair( QString(), QString() ) { } RegionZonePair()
: QPair( QString(), QString() )
{
}
bool isValid() const { return !first.isEmpty(); } bool isValid() const { return !first.isEmpty(); }
} ; };
/** @brief Splits a region/zone string into a pair. /** @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 * pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah
* will return "America", "North_Dakota/Beulah"). * will return "America", "North_Dakota/Beulah").
*/ */
DLLEXPORT RegionZonePair DLLEXPORT RegionZonePair splitTZString( const QString& s );
splitTZString( const QString& s );
/** /**
* @brief Interface for GeoIP retrievers. * @brief Interface for GeoIP retrievers.
@ -93,8 +101,8 @@ protected:
Interface( const QString& e = QString() ); Interface( const QString& e = QString() );
QString m_element; // string for selecting from data QString m_element; // string for selecting from data
} ; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -30,34 +30,40 @@
using std::cerr; using std::cerr;
using namespace CalamaresUtils::GeoIP; 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 <format>\n"; cerr << "Usage: curl url | test_geoip <format>\n";
return 1; return 1;
} }
Interface* handler = nullptr; Interface* handler = nullptr;
if ( QStringLiteral( "json" ) == argv[1] ) if ( QStringLiteral( "json" ) == argv[ 1 ] )
{
handler = new GeoIPJSON; handler = new GeoIPJSON;
}
#ifdef QT_XML_LIB #ifdef QT_XML_LIB
else if ( QStringLiteral( "xml" ) == argv[1] ) else if ( QStringLiteral( "xml" ) == argv[ 1 ] )
{
handler = new GeoIPXML; handler = new GeoIPXML;
}
#endif #endif
if ( !handler ) if ( !handler )
{ {
cerr << "Unknown format '" << argv[1] << "'\n"; cerr << "Unknown format '" << argv[ 1 ] << "'\n";
return 1; return 1;
} }
QByteArray ba; QByteArray ba;
while( !std::cin.eof() ) { while ( !std::cin.eof() )
char arr[1024]; {
std::cin.read(arr, sizeof(arr)); char arr[ 1024 ];
int s = static_cast<int>( std::cin.gcount() ); std::cin.read( arr, sizeof( arr ) );
ba.append(arr, s); int s = static_cast< int >( std::cin.gcount() );
ba.append( arr, s );
} }
auto tz = handler->processReply( ba ); auto tz = handler->processReply( ba );
@ -67,7 +73,8 @@ int main(int argc, char** argv)
} }
else 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; return 0;

Loading…
Cancel
Save