From be91c51975bed74327d548b98e943f942dd19f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 30 Mar 2022 22:00:19 +0200 Subject: [PATCH] Any*{Importer,Converter}: forgot to port these away from std::string. Somehow. Heh. This causes a non-deprecated build on MSVC to break due to a similar reason as in 54394e2c2f0029aa1f767ac895829d4070616328. And, similarly to 3717043ae292991d4cccf9fecc1d05a24c7c2f78, *again* discovered while building bindings. --- .../AnyImageConverter/AnyImageConverter.cpp | 2 +- .../AnyImageConverter/AnyImageConverter.h | 2 +- .../AnyImageImporter/AnyImageImporter.cpp | 30 ++--- .../AnyImageImporter/AnyImageImporter.h | 2 +- .../Test/AnyImageImporterTest.cpp | 1 + .../AnySceneConverter/AnySceneConverter.cpp | 2 +- .../AnySceneConverter/AnySceneConverter.h | 2 +- .../AnySceneImporter/AnySceneImporter.cpp | 125 +++++++++--------- .../AnySceneImporter/AnySceneImporter.h | 2 +- .../AnyShaderConverter/AnyConverter.cpp | 14 +- .../AnyShaderConverter/AnyConverter.h | 2 +- 11 files changed, 95 insertions(+), 89 deletions(-) diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp index 99ace0b80..05007dc5f 100644 --- a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp @@ -43,7 +43,7 @@ using namespace Containers::Literals; AnyImageConverter::AnyImageConverter(PluginManager::Manager& manager): AbstractImageConverter{manager} {} -AnyImageConverter::AnyImageConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractImageConverter{manager, plugin} {} +AnyImageConverter::AnyImageConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractImageConverter{manager, plugin} {} AnyImageConverter::~AnyImageConverter() = default; diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h index 61a8ba417..33139fd84 100644 --- a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h @@ -128,7 +128,7 @@ class MAGNUM_ANYIMAGECONVERTER_EXPORT AnyImageConverter: public AbstractImageCon explicit AnyImageConverter(PluginManager::Manager& manager); /** @brief Plugin manager constructor */ - explicit AnyImageConverter(PluginManager::AbstractManager& manager, const std::string& plugin); + explicit AnyImageConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin); ~AnyImageConverter(); diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp index 4e473141f..9655dedce 100644 --- a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp +++ b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp @@ -31,8 +31,8 @@ #include #include #include -#include -#include +#include /* for PluginMetadata::name() */ +#include #include #include @@ -45,7 +45,7 @@ using namespace Containers::Literals; AnyImageImporter::AnyImageImporter(PluginManager::Manager& manager): AbstractImporter{manager} {} -AnyImageImporter::AnyImageImporter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractImporter{manager, plugin} {} +AnyImageImporter::AnyImageImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractImporter{manager, plugin} {} AnyImageImporter::AnyImageImporter(AnyImageImporter&&) noexcept = default; @@ -170,36 +170,36 @@ void AnyImageImporter::doOpenData(Containers::Array&& data, DataFlags) { const Containers::ArrayView dataView = data; const Containers::StringView dataString = dataView; - std::string plugin; + Containers::StringView plugin; /* https://github.com/BinomialLLC/basis_universal/blob/7d784c728844c007d8c95d63231f7adcc0f65364/transcoder/basisu_file_headers.h#L78 */ if(dataString.hasPrefix("sB"_s)) - plugin = "BasisImporter"; + plugin = "BasisImporter"_s; /* https://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header */ else if(dataString.hasPrefix("BM"_s)) - plugin = "BmpImporter"; + plugin = "BmpImporter"_s; /* https://docs.microsoft.com/cs-cz/windows/desktop/direct3ddds/dx-graphics-dds-pguide */ else if(dataString.hasPrefix("DDS "_s)) - plugin = "DdsImporter"; + plugin = "DdsImporter"_s; /* http://www.openexr.com/openexrfilelayout.pdf */ else if(dataString.hasPrefix("\x76\x2f\x31\x01"_s)) - plugin = "OpenExrImporter"; + plugin = "OpenExrImporter"_s; /* https://en.wikipedia.org/wiki/Radiance_(software)#HDR_image_format */ else if(dataString.hasPrefix("#?RADIANCE"_s)) - plugin = "HdrImporter"; + plugin = "HdrImporter"_s; /* https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure */ else if(dataString.hasPrefix("\xff\xd8\xff"_s)) - plugin = "JpegImporter"; + plugin = "JpegImporter"_s; /* https://github.khronos.org/KTX-Specification/#_identifier */ else if(dataString.hasPrefix("\xabKTX 20\xbb\r\n\x1a\n"_s)) - plugin = "KtxImporter"; + plugin = "KtxImporter"_s; /* https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header */ else if(dataString.hasPrefix("\x89PNG\x0d\x0a\x1a\x0a"_s)) - plugin = "PngImporter"; + plugin = "PngImporter"_s; /* http://paulbourke.net/dataformats/tiff/, http://paulbourke.net/dataformats/tiff/tiff_summary.pdf */ else if(dataString.hasPrefix("II\x2a\x00"_s) || dataString.hasPrefix("MM\x00\x2a"_s)) - plugin = "TiffImporter"; + plugin = "TiffImporter"_s; /* https://github.com/file/file/blob/d04de269e0b06ccd0a7d1bf4974fed1d75be7d9e/magic/Magdir/images#L18-L22 TGAs are a complete guesswork, so try after everything else fails. */ else if([dataView]() { @@ -221,7 +221,7 @@ void AnyImageImporter::doOpenData(Containers::Array&& data, DataFlags) { /* Probably TGA, heh. Or random memory. */ return true; - }()) plugin = "TgaImporter"; + }()) plugin = "TgaImporter"_s; else if(!data.size()) { Error{} << "Trade::AnyImageImporter::openData(): file is empty"; return; @@ -233,7 +233,7 @@ void AnyImageImporter::doOpenData(Containers::Array&& data, DataFlags) { if(data.size() > 2) signature |= UnsignedInt(UnsignedByte(data[2])) << 8; if(data.size() > 3) signature |= UnsignedInt(UnsignedByte(data[3])); /* If there's less than four bytes, cut the rest away */ - Error{} << "Trade::AnyImageImporter::openData(): cannot determine the format from signature 0x" << Debug::nospace << Utility::formatString("{:.8x}", signature).substr(0, data.size() < 4 ? data.size()*2 : std::string::npos); + Error{} << "Trade::AnyImageImporter::openData(): cannot determine the format from signature 0x" << Debug::nospace << Utility::format("{:.8x}", signature).prefix(Math::min(data.size(), std::size_t{4})*2); return; } diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h index 5d47b6097..048e0257d 100644 --- a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h +++ b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h @@ -154,7 +154,7 @@ class MAGNUM_ANYIMAGEIMPORTER_EXPORT AnyImageImporter: public AbstractImporter { explicit AnyImageImporter(PluginManager::Manager& manager); /** @brief Plugin manager constructor */ - explicit AnyImageImporter(PluginManager::AbstractManager& manager, const std::string& plugin); + explicit AnyImageImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin); /** @brief Copying is not allowed */ AnyImageImporter(const AnyImageImporter&) = delete; diff --git a/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp b/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp index 947b56777..286f5f40f 100644 --- a/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp +++ b/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp @@ -137,6 +137,7 @@ const struct { /* There was a bug where the error message shifted a signed value, poisoning the output. It also was throwing away leading zero bytes. */ {"leading zeros, negative char", "\x00\xff\x00\xff"_s, "00ff00ff"}, + {"lots zero bytes", "\x00\x00\x00\x00"_s, "00000000"}, {"just one byte", "\x33"_s, "33"}, {"just one zero byte", "\x00"_s, "00"}, {"DDS, but no space", "DDS!"_s, "44445321"}, diff --git a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp index 69037b707..3a93b4f6e 100644 --- a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp +++ b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp @@ -43,7 +43,7 @@ using namespace Containers::Literals; AnySceneConverter::AnySceneConverter(PluginManager::Manager& manager): AbstractSceneConverter{manager} {} -AnySceneConverter::AnySceneConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractSceneConverter{manager, plugin} {} +AnySceneConverter::AnySceneConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractSceneConverter{manager, plugin} {} AnySceneConverter::~AnySceneConverter() = default; diff --git a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.h b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.h index 220a39f41..2b187258f 100644 --- a/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.h +++ b/src/MagnumPlugins/AnySceneConverter/AnySceneConverter.h @@ -111,7 +111,7 @@ class MAGNUM_ANYSCENECONVERTER_EXPORT AnySceneConverter: public AbstractSceneCon explicit AnySceneConverter(PluginManager::Manager& manager); /** @brief Plugin manager constructor */ - explicit AnySceneConverter(PluginManager::AbstractManager& manager, const std::string& plugin); + explicit AnySceneConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin); ~AnySceneConverter(); diff --git a/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.cpp b/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.cpp index 14f19d533..d78111279 100644 --- a/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.cpp +++ b/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.cpp @@ -29,7 +29,8 @@ #include #include #include -#include +#include /* for PluginMetadata::name() */ +#include #include #include "Magnum/Trade/AnimationData.h" @@ -55,9 +56,11 @@ namespace Magnum { namespace Trade { +using namespace Containers::Literals; + AnySceneImporter::AnySceneImporter(PluginManager::Manager& manager): AbstractImporter{manager} {} -AnySceneImporter::AnySceneImporter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractImporter{manager, plugin} {} +AnySceneImporter::AnySceneImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractImporter{manager, plugin} {} AnySceneImporter::~AnySceneImporter() = default; @@ -74,66 +77,68 @@ void AnySceneImporter::doClose() { void AnySceneImporter::doOpenFile(const Containers::StringView filename) { CORRADE_INTERNAL_ASSERT(manager()); - /** @todo lowercase only the extension, once Directory::split() is done */ - const std::string normalized = Utility::String::lowercase(filename); + /* We don't detect any double extensions yet, so we can normalize just the + extension. In case we eventually might, it'd have to be split() instead + to save at least by normalizing just the filename and not the path. */ + const Containers::String normalizedExtension = Utility::String::lowercase(Utility::Path::splitExtension(filename).second()); /* Detect the plugin from extension */ - std::string plugin; - if(Utility::String::endsWith(normalized, ".3ds") || - Utility::String::endsWith(normalized, ".ase")) - plugin = "3dsImporter"; - else if(Utility::String::endsWith(normalized, ".ac")) - plugin = "Ac3dImporter"; - else if(Utility::String::endsWith(normalized, ".blend")) - plugin = "BlenderImporter"; - else if(Utility::String::endsWith(normalized, ".bvh")) - plugin = "BvhImporter"; - else if(Utility::String::endsWith(normalized, ".csm")) - plugin = "CsmImporter"; - else if(Utility::String::endsWith(normalized, ".dae")) - plugin = "ColladaImporter"; - else if(Utility::String::endsWith(normalized, ".x")) - plugin = "DirectXImporter"; - else if(Utility::String::endsWith(normalized, ".dxf")) - plugin = "DxfImporter"; - else if(Utility::String::endsWith(normalized, ".fbx")) - plugin = "FbxImporter"; - else if(Utility::String::endsWith(normalized, ".gltf") || - Utility::String::endsWith(normalized, ".glb")) - plugin = "GltfImporter"; - else if(Utility::String::endsWith(normalized, ".ifc")) - plugin = "IfcImporter"; - else if(Utility::String::endsWith(normalized, ".irrmesh") || - Utility::String::endsWith(normalized, ".irr")) - plugin = "IrrlichtImporter"; - else if(Utility::String::endsWith(normalized, ".lwo") || - Utility::String::endsWith(normalized, ".lws")) - plugin = "LightWaveImporter"; - else if(Utility::String::endsWith(normalized, ".lxo")) - plugin = "ModoImporter"; - else if(Utility::String::endsWith(normalized, ".ms3d")) - plugin = "MilkshapeImporter"; - else if(Utility::String::endsWith(normalized, ".obj")) - plugin = "ObjImporter"; - else if(Utility::String::endsWith(normalized, ".xml")) - plugin = "OgreImporter"; - else if(Utility::String::endsWith(normalized, ".ogex")) - plugin = "OpenGexImporter"; - else if(Utility::String::endsWith(normalized, ".ply")) - plugin = "StanfordImporter"; - else if(Utility::String::endsWith(normalized, ".stl")) - plugin = "StlImporter"; - else if(Utility::String::endsWith(normalized, ".cob") || - Utility::String::endsWith(normalized, ".scn")) - plugin = "TrueSpaceImporter"; - else if(Utility::String::endsWith(normalized, ".3d")) - plugin = "UnrealImporter"; - else if(Utility::String::endsWith(normalized, ".smd") || - Utility::String::endsWith(normalized, ".vta")) - plugin = "ValveImporter"; - else if(Utility::String::endsWith(normalized, ".xgl") || - Utility::String::endsWith(normalized, ".zgl")) - plugin = "XglImporter"; + Containers::StringView plugin; + if(normalizedExtension == ".3ds"_s || + normalizedExtension == ".ase"_s) + plugin = "3dsImporter"_s; + else if(normalizedExtension == ".ac"_s) + plugin = "Ac3dImporter"_s; + else if(normalizedExtension == ".blend"_s) + plugin = "BlenderImporter"_s; + else if(normalizedExtension == ".bvh"_s) + plugin = "BvhImporter"_s; + else if(normalizedExtension == ".csm"_s) + plugin = "CsmImporter"_s; + else if(normalizedExtension == ".dae"_s) + plugin = "ColladaImporter"_s; + else if(normalizedExtension == ".x"_s) + plugin = "DirectXImporter"_s; + else if(normalizedExtension == ".dxf"_s) + plugin = "DxfImporter"_s; + else if(normalizedExtension == ".fbx"_s) + plugin = "FbxImporter"_s; + else if(normalizedExtension == ".gltf"_s || + normalizedExtension == ".glb"_s) + plugin = "GltfImporter"_s; + else if(normalizedExtension == ".ifc"_s) + plugin = "IfcImporter"_s; + else if(normalizedExtension == ".irrmesh"_s || + normalizedExtension == ".irr"_s) + plugin = "IrrlichtImporter"_s; + else if(normalizedExtension == ".lwo"_s || + normalizedExtension == ".lws"_s) + plugin = "LightWaveImporter"_s; + else if(normalizedExtension == ".lxo"_s) + plugin = "ModoImporter"_s; + else if(normalizedExtension == ".ms3d"_s) + plugin = "MilkshapeImporter"_s; + else if(normalizedExtension == ".obj"_s) + plugin = "ObjImporter"_s; + else if(normalizedExtension == ".xml"_s) + plugin = "OgreImporter"_s; + else if(normalizedExtension == ".ogex"_s) + plugin = "OpenGexImporter"_s; + else if(normalizedExtension == ".ply"_s) + plugin = "StanfordImporter"_s; + else if(normalizedExtension == ".stl"_s) + plugin = "StlImporter"_s; + else if(normalizedExtension == ".cob"_s || + normalizedExtension == ".scn"_s) + plugin = "TrueSpaceImporter"_s; + else if(normalizedExtension == ".3d"_s) + plugin = "UnrealImporter"_s; + else if(normalizedExtension == ".smd"_s || + normalizedExtension == ".vta"_s) + plugin = "ValveImporter"_s; + else if(normalizedExtension == ".xgl"_s || + normalizedExtension == ".zgl"_s) + plugin = "XglImporter"_s; else { Error{} << "Trade::AnySceneImporter::openFile(): cannot determine the format of" << filename; return; diff --git a/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.h b/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.h index 33a5a88cb..330a6cfb1 100644 --- a/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.h +++ b/src/MagnumPlugins/AnySceneImporter/AnySceneImporter.h @@ -151,7 +151,7 @@ class MAGNUM_ANYSCENEIMPORTER_EXPORT AnySceneImporter: public AbstractImporter { explicit AnySceneImporter(PluginManager::Manager& manager); /** @brief Plugin manager constructor */ - explicit AnySceneImporter(PluginManager::AbstractManager& manager, const std::string& plugin); + explicit AnySceneImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin); ~AnySceneImporter(); diff --git a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp index 21936b569..6b664000b 100644 --- a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp +++ b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp @@ -31,7 +31,7 @@ #include #include #include /* for PluginMetadata::name() */ -#include +#include #include #include @@ -51,7 +51,7 @@ struct AnyConverter::State { AnyConverter::AnyConverter(PluginManager::Manager& manager): AbstractConverter{manager} {} -AnyConverter::AnyConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractConverter{manager, plugin}, _state{InPlaceInit} {} +AnyConverter::AnyConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): AbstractConverter{manager, plugin}, _state{InPlaceInit} {} AnyConverter::~AnyConverter() = default; @@ -186,7 +186,7 @@ std::pair AnyConverter::doValidateFile(const Stage sta if(format.isEmpty()) return {}; /* Decide on a plugin name based on the format */ - const std::string plugin = Utility::formatString("{}ShaderConverter", format); + const Containers::String plugin = Utility::format("{}ShaderConverter", format); /* Try to load the plugin */ if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { @@ -242,7 +242,7 @@ std::pair AnyConverter::doValidateData(const Stage sta Error{} << "ShaderTools::AnyConverter::validateData(): no input format specified"; return {}; } - const std::string plugin = Utility::formatString("{}ShaderConverter", stringForFormat(_state->inputFormat)); + const Containers::String plugin = Utility::format("{}ShaderConverter", stringForFormat(_state->inputFormat)); /* Try to load the plugin */ if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { @@ -308,7 +308,7 @@ bool AnyConverter::doConvertFileToFile(const Stage stage, const Containers::Stri /* Decide on a plugin name based on the format. This might result in invalid combinations such as SpirvToGlslShaderConverter which can't be really handled yet but I think that's okay for now. */ - const std::string plugin = Utility::formatString( + const Containers::String plugin = Utility::format( formatFrom == formatTo ? "{}ShaderConverter" : "{}To{}ShaderConverter", formatFrom, formatTo); @@ -393,7 +393,7 @@ Containers::Array AnyConverter::doConvertFileToData(const Stage stage, con /* Decide on a plugin name based on the format. This might result in invalid combinations such as SpirvToGlslShaderConverter which can't be really handled yet but I think that's okay for now. */ - const std::string plugin = Utility::formatString( + const Containers::String plugin = Utility::format( formatFrom == formatTo ? "{}ShaderConverter" : "{}To{}ShaderConverter", formatFrom, formatTo); @@ -476,7 +476,7 @@ Containers::Array AnyConverter::doConvertDataToData(const Stage stage, con } const Containers::StringView formatFrom = stringForFormat(_state->inputFormat); const Containers::StringView formatTo = stringForFormat(_state->outputFormat); - const std::string plugin = Utility::formatString( + const Containers::String plugin = Utility::format( formatFrom == formatTo ? "{}ShaderConverter" : "{}To{}ShaderConverter", formatFrom, formatTo); diff --git a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.h b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.h index 43e1a504e..a2040c31c 100644 --- a/src/MagnumPlugins/AnyShaderConverter/AnyConverter.h +++ b/src/MagnumPlugins/AnyShaderConverter/AnyConverter.h @@ -146,7 +146,7 @@ class MAGNUM_ANYSHADERCONVERTER_EXPORT AnyConverter: public AbstractConverter { explicit AnyConverter(PluginManager::Manager& manager); /** @brief Plugin manager constructor */ - explicit AnyConverter(PluginManager::AbstractManager& manager, const std::string& plugin); + explicit AnyConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin); ~AnyConverter();