From 1beadba0bbb31a0620effd43f6618170ee3825c5 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Wed, 13 Sep 2023 17:53:45 +0100 Subject: [PATCH] 3DFileViewer: Remove DeprecatedString usage in object loader This change also improves error handling if the given .obj file contains malformed floating point numbers. --- .../3DFileViewer/WavefrontOBJLoader.cpp | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp index a2c0b3943c..76625360d0 100644 --- a/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp +++ b/Userland/Applications/3DFileViewer/WavefrontOBJLoader.cpp @@ -10,13 +10,21 @@ #include #include #include -#include static inline GLuint get_index_value(StringView& representation) { return representation.to_uint().value_or(1) - 1; } +static ErrorOr parse_float(StringView string) +{ + auto maybe_float = string.to_float(TrimWhitespace::No); + if (!maybe_float.has_value()) + return Error::from_string_literal("Wavefront: Expected floating point value when parsing TexCoord line"); + + return maybe_float.release_value(); +} + ErrorOr> WavefrontOBJLoader::load(String const& filename, NonnullOwnPtr file) { auto buffered_file = TRY(Core::InputBufferedFile::create(move(file))); @@ -43,8 +51,8 @@ ErrorOr> WavefrontOBJLoader::load(String const& filename, No return Error::from_string_literal("Wavefront: Malformed TexCoord line."); } - tex_coords.append({ static_cast(atof(DeprecatedString(tex_coord_line.at(1)).characters())), - static_cast(atof(DeprecatedString(tex_coord_line.at(2)).characters())) }); + tex_coords.append({ TRY(parse_float(tex_coord_line.at(1))), + TRY(parse_float(tex_coord_line.at(2))) }); continue; } @@ -55,9 +63,9 @@ ErrorOr> WavefrontOBJLoader::load(String const& filename, No return Error::from_string_literal("Wavefront: Malformed vertex normal line."); } - normals.append({ static_cast(atof(DeprecatedString(normal_line.at(1)).characters())), - static_cast(atof(DeprecatedString(normal_line.at(2)).characters())), - static_cast(atof(DeprecatedString(normal_line.at(3)).characters())) }); + normals.append({ TRY(parse_float(normal_line.at(1))), + TRY(parse_float(normal_line.at(2))), + TRY(parse_float(normal_line.at(3))) }); continue; } @@ -69,9 +77,9 @@ ErrorOr> WavefrontOBJLoader::load(String const& filename, No return Error::from_string_literal("Wavefront: Malformed vertex line."); } - vertices.append({ static_cast(atof(DeprecatedString(vertex_line.at(1)).characters())), - static_cast(atof(DeprecatedString(vertex_line.at(2)).characters())), - static_cast(atof(DeprecatedString(vertex_line.at(3)).characters())) }); + vertices.append({ TRY(parse_float((vertex_line.at(1)))), + TRY(parse_float((vertex_line.at(2)))), + TRY(parse_float((vertex_line.at(3)))) }); continue; }