Browse Source

Any{Image,Scene,Shader}Converter: modernize the string code a bit.

Only those use StringViews in the plugin API so far, importers not yet.
pull/527/head
Vladimír Vondruš 5 years ago
parent
commit
416704fa47
  1. 51
      src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp
  2. 17
      src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp
  3. 70
      src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp

51
src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp

@ -26,11 +26,11 @@
#include "AnyImageConverter.h"
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/StringStl.h> /* for Directory */
#include <Corrade/Containers/StringStl.h> /* for PluginManager */
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/PluginManager/PluginMetadata.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/DebugStl.h> /* for PluginMetadata::name() */
#include <Corrade/Utility/String.h>
#include "Magnum/Trade/ImageData.h"
@ -38,6 +38,8 @@
namespace Magnum { namespace Trade {
using namespace Containers::Literals;
AnyImageConverter::AnyImageConverter(PluginManager::Manager<AbstractImageConverter>& manager): AbstractImageConverter{manager} {}
AnyImageConverter::AnyImageConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractImageConverter{manager, plugin} {}
@ -51,30 +53,31 @@ ImageConverterFeatures AnyImageConverter::doFeatures() const {
bool AnyImageConverter::doConvertToFile(const ImageView2D& image, 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);
/** @todo once Directory is std::string-free, use splitExtension(), but
only if we don't detect more than one extension yet */
const Containers::String normalized = Utility::String::lowercase(filename);
/* Detect the plugin from extension */
std::string plugin;
if(Utility::String::endsWith(normalized, ".bmp"))
plugin = "BmpImageConverter";
else if(Utility::String::endsWith(normalized, ".basis"))
plugin = "BasisImageConverter";
else if(Utility::String::endsWith(normalized, ".exr"))
plugin = "OpenExrImageConverter";
else if(Utility::String::endsWith(normalized, ".hdr"))
plugin = "HdrImageConverter";
else if(Utility::String::endsWith(normalized, ".jpg") ||
Utility::String::endsWith(normalized, ".jpeg") ||
Utility::String::endsWith(normalized, ".jpe"))
plugin = "JpegImageConverter";
else if(Utility::String::endsWith(normalized, ".png"))
plugin = "PngImageConverter";
else if(Utility::String::endsWith(normalized, ".tga") ||
Utility::String::endsWith(normalized, ".vda") ||
Utility::String::endsWith(normalized, ".icb") ||
Utility::String::endsWith(normalized, ".vst"))
plugin = "TgaImageConverter";
Containers::StringView plugin;
if(normalized.hasSuffix(".bmp"_s))
plugin = "BmpImageConverter"_s;
else if(normalized.hasSuffix(".basis"_s))
plugin = "BasisImageConverter"_s;
else if(normalized.hasSuffix(".exr"_s))
plugin = "OpenExrImageConverter"_s;
else if(normalized.hasSuffix(".hdr"_s))
plugin = "HdrImageConverter"_s;
else if(normalized.hasSuffix(".jpg"_s) ||
normalized.hasSuffix(".jpeg"_s) ||
normalized.hasSuffix(".jpe"_s))
plugin = "JpegImageConverter"_s;
else if(normalized.hasSuffix(".png"_s))
plugin = "PngImageConverter"_s;
else if(normalized.hasSuffix(".tga"_s) ||
normalized.hasSuffix(".vda"_s) ||
normalized.hasSuffix(".icb"_s) ||
normalized.hasSuffix( ".vst"_s))
plugin = "TgaImageConverter"_s;
else {
Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename;
return false;

17
src/MagnumPlugins/AnySceneConverter/AnySceneConverter.cpp

@ -26,11 +26,11 @@
#include "AnySceneConverter.h"
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/StringStl.h>
#include <Corrade/Containers/StringStl.h> /* for PluginManager */
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/PluginManager/PluginMetadata.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/DebugStl.h> /* for PluginMetadata::name() */
#include <Corrade/Utility/String.h>
#include "Magnum/Trade/ImageData.h"
@ -38,6 +38,8 @@
namespace Magnum { namespace Trade {
using namespace Containers::Literals;
AnySceneConverter::AnySceneConverter(PluginManager::Manager<AbstractSceneConverter>& manager): AbstractSceneConverter{manager} {}
AnySceneConverter::AnySceneConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractSceneConverter{manager, plugin} {}
@ -51,13 +53,14 @@ SceneConverterFeatures AnySceneConverter::doFeatures() const {
bool AnySceneConverter::doConvertToFile(const MeshData& mesh, 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);
/** @todo once Directory is std::string-free, use splitExtension(), but
only if we don't detect more than one extension yet */
const Containers::StringView normalized = Utility::String::lowercase(filename);
/* Detect the plugin from extension */
std::string plugin;
if(Utility::String::endsWith(normalized, ".ply"))
plugin = "StanfordSceneConverter";
Containers::StringView plugin;
if(normalized.hasSuffix(".ply"_s))
plugin = "StanfordSceneConverter"_s;
else {
Error{} << "Trade::AnySceneConverter::convertToFile(): cannot determine the format of" << filename;
return false;

70
src/MagnumPlugins/AnyShaderConverter/AnyConverter.cpp

@ -27,11 +27,10 @@
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringStl.h>
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/PluginManager/PluginMetadata.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/DebugStl.h> /* for PluginMetadata::name() */
#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/String.h>
@ -122,49 +121,50 @@ Containers::StringView stringForFormat(const Format format) {
}
Format formatForExtension(const char* prefix, const Containers::StringView filename) {
/** @todo lowercase only the extension, once Directory::split() is done */
const std::string normalized = Utility::String::lowercase(filename);
/* Can't reliably lowercase just the extension as we detect double
extensions as well */
const Containers::String normalized = Utility::String::lowercase(filename);
/* https://github.com/KhronosGroup/SPIRV-Tools/blob/a715b1b4053519ad0f2bdb2d22ace35d35867cff/README.md#command-line-tools
"It's a convention to name SPIR-V assembly and binary files with suffix
.spvasm and .spv, respectively." IT'S GREAT THAT I HAD TO SEARCH HALF
THE INTERNET TO FIND THIS CONVENTION. Especially when tests in the
SPIRV-Cross repo use `.asm.bla` instead, FFS. */
if(Utility::String::endsWith(normalized, ".spvasm") ||
if(normalized.hasSuffix(".spvasm"_s) ||
/* Not official, used by https://github.com/KhronosGroup/SPIRV-Cross */
Utility::String::endsWith(normalized, ".asm.vert") ||
Utility::String::endsWith(normalized, ".asm.frag") ||
Utility::String::endsWith(normalized, ".asm.geom") ||
Utility::String::endsWith(normalized, ".asm.comp") ||
Utility::String::endsWith(normalized, ".asm.tesc") ||
Utility::String::endsWith(normalized, ".asm.tese") ||
Utility::String::endsWith(normalized, ".asm.rgen") ||
Utility::String::endsWith(normalized, ".asm.rint") ||
Utility::String::endsWith(normalized, ".asm.rahit") ||
Utility::String::endsWith(normalized, ".asm.rchit") ||
Utility::String::endsWith(normalized, ".asm.rmiss") ||
Utility::String::endsWith(normalized, ".asm.rcall") ||
Utility::String::endsWith(normalized, ".asm.mesh") ||
Utility::String::endsWith(normalized, ".asm.task"))
normalized.hasSuffix(".asm.vert"_s) ||
normalized.hasSuffix(".asm.frag"_s) ||
normalized.hasSuffix(".asm.geom"_s) ||
normalized.hasSuffix(".asm.comp"_s) ||
normalized.hasSuffix(".asm.tesc"_s) ||
normalized.hasSuffix(".asm.tese"_s) ||
normalized.hasSuffix(".asm.rgen"_s) ||
normalized.hasSuffix(".asm.rint"_s) ||
normalized.hasSuffix(".asm.rahit"_s) ||
normalized.hasSuffix(".asm.rchit"_s) ||
normalized.hasSuffix(".asm.rmiss"_s) ||
normalized.hasSuffix(".asm.rcall"_s) ||
normalized.hasSuffix(".asm.mesh"_s) ||
normalized.hasSuffix(".asm.task"_s))
return Format::SpirvAssembly;
/* https://github.com/KhronosGroup/glslang/blob/3ce148638bdc3807316e358dee4a5c9583189ae7/StandAlone/StandAlone.cpp#L260-L274 */
else if(Utility::String::endsWith(normalized, ".glsl") ||
Utility::String::endsWith(normalized, ".vert") ||
Utility::String::endsWith(normalized, ".frag") ||
Utility::String::endsWith(normalized, ".geom") ||
Utility::String::endsWith(normalized, ".comp") ||
Utility::String::endsWith(normalized, ".tesc") ||
Utility::String::endsWith(normalized, ".tese") ||
Utility::String::endsWith(normalized, ".rgen") ||
Utility::String::endsWith(normalized, ".rint") ||
Utility::String::endsWith(normalized, ".rahit") ||
Utility::String::endsWith(normalized, ".rchit") ||
Utility::String::endsWith(normalized, ".rmiss") ||
Utility::String::endsWith(normalized, ".rcall") ||
Utility::String::endsWith(normalized, ".mesh") ||
Utility::String::endsWith(normalized, ".task"))
else if(normalized.hasSuffix(".glsl"_s) ||
normalized.hasSuffix(".vert"_s) ||
normalized.hasSuffix(".frag"_s) ||
normalized.hasSuffix(".geom"_s) ||
normalized.hasSuffix(".comp"_s) ||
normalized.hasSuffix(".tesc"_s) ||
normalized.hasSuffix(".tese"_s) ||
normalized.hasSuffix(".rgen"_s) ||
normalized.hasSuffix(".rint"_s) ||
normalized.hasSuffix(".rahit"_s) ||
normalized.hasSuffix(".rchit"_s) ||
normalized.hasSuffix(".rmiss"_s) ||
normalized.hasSuffix(".rcall"_s) ||
normalized.hasSuffix(".mesh"_s) ||
normalized.hasSuffix(".task"_s))
return Format::Glsl;
else if(Utility::String::endsWith(normalized, ".spv"))
else if(normalized.hasSuffix(".spv"_s))
return Format::Spirv;
Error{} << prefix << "cannot determine the format of" << filename;

Loading…
Cancel
Save