From e0fd43f6fd14a4fbe93121f41948ea04fa411dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 6 Oct 2020 22:41:11 +0200 Subject: [PATCH] Trade: extract Implementation:setOptions() to core lib. Will be needed by magnum-shaderconverter, which isn't depending on Trade. --- src/Magnum/CMakeLists.txt | 1 + .../Implementation/converterUtilities.h | 82 +++++++++++++++++++ src/Magnum/MeshTools/sceneconverter.cpp | 5 +- .../Trade/Implementation/converterUtilities.h | 42 ---------- src/Magnum/Trade/imageconverter.cpp | 5 +- 5 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 src/Magnum/Implementation/converterUtilities.h diff --git a/src/Magnum/CMakeLists.txt b/src/Magnum/CMakeLists.txt index 177f5a226..207ae657e 100644 --- a/src/Magnum/CMakeLists.txt +++ b/src/Magnum/CMakeLists.txt @@ -97,6 +97,7 @@ set(Magnum_HEADERS set(Magnum_PRIVATE_HEADERS Implementation/ImageProperties.h + Implementation/converterUtilities.h Implementation/meshIndexTypeMapping.hpp Implementation/meshPrimitiveMapping.hpp Implementation/compressedPixelFormatMapping.hpp diff --git a/src/Magnum/Implementation/converterUtilities.h b/src/Magnum/Implementation/converterUtilities.h new file mode 100644 index 000000000..3b2331513 --- /dev/null +++ b/src/Magnum/Implementation/converterUtilities.h @@ -0,0 +1,82 @@ +#ifndef Magnum_Implementation_converterUtilities_h +#define Magnum_Implementation_converterUtilities_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, + 2020 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +#include "Magnum/Magnum.h" + +namespace Magnum { namespace Implementation { + +/* Used only in executables where we don't want it to be exported */ +namespace { + +void setOptions(PluginManager::AbstractPlugin& plugin, const std::string& options) { + for(const std::string& option: Utility::String::splitWithoutEmptyParts(options, ',')) { + auto keyValue = Utility::String::partition(option, '='); + Utility::String::trimInPlace(keyValue[0]); + Utility::String::trimInPlace(keyValue[2]); + + std::vector keyParts = Utility::String::split(keyValue[0], '/'); + CORRADE_INTERNAL_ASSERT(!keyParts.empty()); + Utility::ConfigurationGroup* group = &plugin.configuration(); + bool groupNotRecognized = false; + for(std::size_t i = 0; i != keyParts.size() - 1; ++i) { + Utility::ConfigurationGroup* subgroup = group->group(keyParts[i]); + if(!subgroup) { + groupNotRecognized = true; + subgroup = group->addGroup(keyParts[i]); + } + group = subgroup; + } + + /* Provide a warning message in case the plugin doesn't define given + option in its default config. The plugin is not *required* to have + those tho (could be backward compatibility entries, for example), so + not an error. */ + if(groupNotRecognized || !group->hasValue(keyParts.back())) { + Warning{} << "Option" << keyValue[0] << "not recognized by" << plugin.plugin(); + } + + /* If the option doesn't have an =, treat it as a boolean flag that's + set to true. While there's no similar way to do an inverse, it's + still nicer than causing a fatal error with those. */ + if(keyValue[1].empty()) + group->setValue(keyParts.back(), true); + else + group->setValue(keyParts.back(), keyValue[2]); + } +} + +} + +}} + +#endif diff --git a/src/Magnum/MeshTools/sceneconverter.cpp b/src/Magnum/MeshTools/sceneconverter.cpp index 63a396373..27e01ce40 100644 --- a/src/Magnum/MeshTools/sceneconverter.cpp +++ b/src/Magnum/MeshTools/sceneconverter.cpp @@ -35,6 +35,7 @@ #include #include "Magnum/PixelFormat.h" +#include "Magnum/Implementation/converterUtilities.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/FunctionsBatch.h" #include "Magnum/MeshTools/RemoveDuplicates.h" @@ -251,7 +252,7 @@ save its output; if no --converter is specified, AnySceneConverter is used.)") /* Set options, if passed */ if(args.isSet("verbose")) importer->setFlags(Trade::ImporterFlag::Verbose); - Trade::Implementation::setOptions(*importer, args.value("importer-options")); + Implementation::setOptions(*importer, args.value("importer-options")); std::chrono::high_resolution_clock::duration importTime; @@ -757,7 +758,7 @@ save its output; if no --converter is specified, AnySceneConverter is used.)") /* Set options, if passed */ if(args.isSet("verbose")) converter->setFlags(Trade::SceneConverterFlag::Verbose); if(i < args.arrayValueCount("converter-options")) - Trade::Implementation::setOptions(*converter, args.arrayValue("converter-options", i)); + Implementation::setOptions(*converter, args.arrayValue("converter-options", i)); /* This is the last --converter (or the implicit AnySceneConverter at the end), output to a file and exit the loop */ diff --git a/src/Magnum/Trade/Implementation/converterUtilities.h b/src/Magnum/Trade/Implementation/converterUtilities.h index fe15b1cf1..4faebbe3e 100644 --- a/src/Magnum/Trade/Implementation/converterUtilities.h +++ b/src/Magnum/Trade/Implementation/converterUtilities.h @@ -26,11 +26,6 @@ */ #include -#include -#include -#include -#include -#include #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" @@ -40,43 +35,6 @@ namespace Magnum { namespace Trade { namespace Implementation { /* Used only in executables where we don't want it to be exported */ namespace { -void setOptions(PluginManager::AbstractPlugin& plugin, const std::string& options) { - for(const std::string& option: Utility::String::splitWithoutEmptyParts(options, ',')) { - auto keyValue = Utility::String::partition(option, '='); - Utility::String::trimInPlace(keyValue[0]); - Utility::String::trimInPlace(keyValue[2]); - - std::vector keyParts = Utility::String::split(keyValue[0], '/'); - CORRADE_INTERNAL_ASSERT(!keyParts.empty()); - Utility::ConfigurationGroup* group = &plugin.configuration(); - bool groupNotRecognized = false; - for(std::size_t i = 0; i != keyParts.size() - 1; ++i) { - Utility::ConfigurationGroup* subgroup = group->group(keyParts[i]); - if(!subgroup) { - groupNotRecognized = true; - subgroup = group->addGroup(keyParts[i]); - } - group = subgroup; - } - - /* Provide a warning message in case the plugin doesn't define given - option in its default config. The plugin is not *required* to have - those tho (could be backward compatibility entries, for example), so - not an error. */ - if(groupNotRecognized || !group->hasValue(keyParts.back())) { - Warning{} << "Option" << keyValue[0] << "not recognized by" << plugin.plugin(); - } - - /* If the option doesn't have an =, treat it as a boolean flag that's - set to true. While there's no similar way to do an inverse, it's - still nicer than causing a fatal error with those. */ - if(keyValue[1].empty()) - group->setValue(keyParts.back(), true); - else - group->setValue(keyParts.back(), keyValue[2]); - } -} - struct ImageInfo { UnsignedInt image, level; bool compressed; diff --git a/src/Magnum/Trade/imageconverter.cpp b/src/Magnum/Trade/imageconverter.cpp index c3858311e..6611d1f36 100644 --- a/src/Magnum/Trade/imageconverter.cpp +++ b/src/Magnum/Trade/imageconverter.cpp @@ -34,6 +34,7 @@ #include #include "Magnum/PixelFormat.h" +#include "Magnum/Implementation/converterUtilities.h" #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/AbstractImageConverter.h" #include "Magnum/Trade/ImageData.h" @@ -229,7 +230,7 @@ key=true; configuration subgroups are delimited with /.)") /* Set options, if passed */ if(args.isSet("verbose")) importer->setFlags(Trade::ImporterFlag::Verbose); - Trade::Implementation::setOptions(*importer, args.value("importer-options")); + Implementation::setOptions(*importer, args.value("importer-options")); /* Print image info, if requested */ if(args.isSet("info")) { @@ -313,7 +314,7 @@ key=true; configuration subgroups are delimited with /.)") /* Set options, if passed */ if(args.isSet("verbose")) converter->setFlags(Trade::ImageConverterFlag::Verbose); - Trade::Implementation::setOptions(*converter, args.value("converter-options")); + Implementation::setOptions(*converter, args.value("converter-options")); /* Save output file */ if(!converter->exportToFile(*image, output)) {