From 3879087c4afbcfc4b20a78a748c865dcb5c78ac8 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Mon, 14 Jul 2014 18:10:24 +0200 Subject: [PATCH] Add YAML to QVariant conversion to YamlUtils --- src/libcalamaresui/utils/YamlUtils.cpp | 76 ++++++++++++++++++++++++++ src/libcalamaresui/utils/YamlUtils.h | 10 ++++ 2 files changed, 86 insertions(+) diff --git a/src/libcalamaresui/utils/YamlUtils.cpp b/src/libcalamaresui/utils/YamlUtils.cpp index 2bd89a082..3a56e7b00 100644 --- a/src/libcalamaresui/utils/YamlUtils.cpp +++ b/src/libcalamaresui/utils/YamlUtils.cpp @@ -19,6 +19,9 @@ #include +#include +#include + void operator>>( const YAML::Node& node, QStringList& v ) { @@ -28,3 +31,76 @@ operator>>( const YAML::Node& node, QStringList& v ) } } +namespace CalamaresUtils +{ + +const QRegExp _yamlScalarTrueValues = QRegExp( "true|True|TRUE|on|On|ON" ); +const QRegExp _yamlScalarFalseValues = QRegExp( "false|False|FALSE|off|Off|OFF" ); + +QVariant +yamlToVariant( const YAML::Node& node ) +{ + switch ( node.Type() ) + { + case YAML::NodeType::Scalar: + return yamlScalarToVariant( node ); + + case YAML::NodeType::Sequence: + return yamlSequenceToVariant( node ); + + case YAML::NodeType::Map: + return yamlMapToVariant( node ); + + case YAML::NodeType::Null: + return QVariant(); + } + + return QVariant(); +} + + +QVariant +yamlScalarToVariant( const YAML::Node& scalarNode ) +{ + std::string stdScalar = scalarNode.as< std::string >(); + QString scalarString = QString::fromStdString( stdScalar ); + if ( _yamlScalarTrueValues.exactMatch( scalarString ) ) + return QVariant( true ); + if ( _yamlScalarFalseValues.exactMatch( scalarString ) ) + return QVariant( false ); + if ( QRegExp( "[-+]?\\d+" ).exactMatch( scalarString ) ) + return QVariant( scalarString.toInt() ); + if ( QRegExp( "[-+]?\\d*\\.?\\d+" ).exactMatch( scalarString ) ) + return QVariant( scalarString.toDouble() ); + return QVariant( scalarString ); +} + + +QVariant +yamlSequenceToVariant( const YAML::Node& sequenceNode ) +{ + QVariantList vl; + for ( YAML::const_iterator it = sequenceNode.begin(); + it != sequenceNode.end(); ++it ) + { + vl << yamlToVariant( *it ); + } + return vl; +} + + +QVariant +yamlMapToVariant( const YAML::Node& mapNode ) +{ + QVariantMap vm; + for ( YAML::const_iterator it = mapNode.begin(); + it != mapNode.end(); ++it ) + { + vm.insert( QString::fromStdString( it->first.as< std::string >() ), + yamlToVariant( it->second ) ); + } + return vm; +} + + +} diff --git a/src/libcalamaresui/utils/YamlUtils.h b/src/libcalamaresui/utils/YamlUtils.h index 173d76f76..0d2561369 100644 --- a/src/libcalamaresui/utils/YamlUtils.h +++ b/src/libcalamaresui/utils/YamlUtils.h @@ -28,4 +28,14 @@ class Node; void operator>>( const YAML::Node& node, QStringList& v ); +namespace CalamaresUtils +{ + +QVariant yamlToVariant( const YAML::Node& node ); +QVariant yamlScalarToVariant( const YAML::Node& scalarNode ); +QVariant yamlSequenceToVariant( const YAML::Node& sequenceNode ); +QVariant yamlMapToVariant( const YAML::Node& mapNode ); + +} //ns + #endif // YAMLUTILS_H