Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAras Pranckevicius <aras@nesnausk.org>2022-05-06 14:53:56 +0300
committerAras Pranckevicius <aras@nesnausk.org>2022-05-06 14:54:09 +0300
commitc7bffc8fa27b4ae2c92e018dc5f8f79e0dfff9b9 (patch)
tree600d7a6cec1461269a17745f99f996e1c9d7b1ff /source/blender/io/wavefront_obj/importer
parentbdfee6d8318b6dd261adbb08ea2323784f088140 (diff)
obj: move parsing utilities out of io_common, since they are fairly obj specific
As pointed out in https://developer.blender.org/rB213cd39b6db387bd88f12589fd50ff0e6563cf56#341113, the utilities are quite OBJ specific due to treating backslash as a line continuation character. It's unlikely that other formats need that. No functionality changes, just pure code move (and renamed tests so that their names reflect obj). Reviewed By: Campbell Barton Differential Revision: https://developer.blender.org/D14871
Diffstat (limited to 'source/blender/io/wavefront_obj/importer')
-rw-r--r--source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc3
-rw-r--r--source/blender/io/wavefront_obj/importer/obj_import_mtl.cc3
-rw-r--r--source/blender/io/wavefront_obj/importer/obj_import_string_utils.cc99
-rw-r--r--source/blender/io/wavefront_obj/importer/obj_import_string_utils.hh72
4 files changed, 173 insertions, 4 deletions
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
index be322f49840..fa89b49b605 100644
--- a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
+++ b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc
@@ -8,9 +8,8 @@
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"
-#include "IO_string_utils.hh"
-
#include "obj_import_file_reader.hh"
+#include "obj_import_string_utils.hh"
namespace blender::io::obj {
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc b/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc
index c2ecd8a37de..f39def0a4af 100644
--- a/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc
+++ b/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc
@@ -13,13 +13,12 @@
#include "DNA_material_types.h"
#include "DNA_node_types.h"
-#include "IO_string_utils.hh"
-
#include "NOD_shader.h"
/* TODO: move eMTLSyntaxElement out of following file into a more neutral place */
#include "obj_export_io.hh"
#include "obj_import_mtl.hh"
+#include "obj_import_string_utils.hh"
namespace blender::io::obj {
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_string_utils.cc b/source/blender/io/wavefront_obj/importer/obj_import_string_utils.cc
new file mode 100644
index 00000000000..c60306c8375
--- /dev/null
+++ b/source/blender/io/wavefront_obj/importer/obj_import_string_utils.cc
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "obj_import_string_utils.hh"
+
+/* Note: we could use C++17 <charconv> from_chars to parse
+ * floats, but even if some compilers claim full support,
+ * their standard libraries are not quite there yet.
+ * LLVM/libc++ only has a float parser since LLVM 14,
+ * and gcc/libstdc++ since 11.1. So until at least these are
+ * the minimum spec, use an external library. */
+#include "fast_float.h"
+#include <charconv>
+
+namespace blender::io::obj {
+
+StringRef read_next_line(StringRef &buffer)
+{
+ const char *start = buffer.begin();
+ const char *end = buffer.end();
+ size_t len = 0;
+ char prev = 0;
+ const char *ptr = start;
+ while (ptr < end) {
+ char c = *ptr++;
+ if (c == '\n' && prev != '\\') {
+ break;
+ }
+ prev = c;
+ ++len;
+ }
+
+ buffer = StringRef(ptr, end);
+ return StringRef(start, len);
+}
+
+static bool is_whitespace(char c)
+{
+ return c <= ' ' || c == '\\';
+}
+
+StringRef drop_whitespace(StringRef str)
+{
+ while (!str.is_empty() && is_whitespace(str[0])) {
+ str = str.drop_prefix(1);
+ }
+ return str;
+}
+
+StringRef drop_non_whitespace(StringRef str)
+{
+ while (!str.is_empty() && !is_whitespace(str[0])) {
+ str = str.drop_prefix(1);
+ }
+ return str;
+}
+
+static StringRef drop_plus(StringRef str)
+{
+ if (!str.is_empty() && str[0] == '+') {
+ str = str.drop_prefix(1);
+ }
+ return str;
+}
+
+StringRef parse_float(StringRef str, float fallback, float &dst, bool skip_space)
+{
+ if (skip_space) {
+ str = drop_whitespace(str);
+ }
+ str = drop_plus(str);
+ fast_float::from_chars_result res = fast_float::from_chars(str.begin(), str.end(), dst);
+ if (res.ec == std::errc::invalid_argument || res.ec == std::errc::result_out_of_range) {
+ dst = fallback;
+ }
+ return StringRef(res.ptr, str.end());
+}
+
+StringRef parse_floats(StringRef str, float fallback, float *dst, int count)
+{
+ for (int i = 0; i < count; ++i) {
+ str = parse_float(str, fallback, dst[i]);
+ }
+ return str;
+}
+
+StringRef parse_int(StringRef str, int fallback, int &dst, bool skip_space)
+{
+ if (skip_space) {
+ str = drop_whitespace(str);
+ }
+ str = drop_plus(str);
+ std::from_chars_result res = std::from_chars(str.begin(), str.end(), dst);
+ if (res.ec == std::errc::invalid_argument || res.ec == std::errc::result_out_of_range) {
+ dst = fallback;
+ }
+ return StringRef(res.ptr, str.end());
+}
+
+} // namespace blender::io::obj
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_string_utils.hh b/source/blender/io/wavefront_obj/importer/obj_import_string_utils.hh
new file mode 100644
index 00000000000..532224569ac
--- /dev/null
+++ b/source/blender/io/wavefront_obj/importer/obj_import_string_utils.hh
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include "BLI_string_ref.hh"
+
+/*
+ * Various text parsing utilities used by OBJ importer.
+ * The utilities are not directly usable by other formats, since
+ * they treat backslash (\) as a whitespace character (OBJ format
+ * allows backslashes to function as a line-continuation character).
+ */
+
+namespace blender::io::obj {
+
+/**
+ * Fetches next line from an input string buffer.
+ *
+ * The returned line will not have '\n' characters at the end;
+ * the `buffer` is modified to contain remaining text without
+ * the input line.
+ *
+ * Note that backslash (\) character is treated as a line
+ * continuation.
+ */
+StringRef read_next_line(StringRef &buffer);
+
+/**
+ * Drop leading white-space from a StringRef.
+ * Note that backslash character is considered white-space.
+ */
+StringRef drop_whitespace(StringRef str);
+
+/**
+ * Drop leading non-white-space from a StringRef.
+ * Note that backslash character is considered white-space.
+ */
+StringRef drop_non_whitespace(StringRef str);
+
+/**
+ * Parse an integer from an input string.
+ * The parsed result is stored in `dst`. The function skips
+ * leading white-space unless `skip_space=false`. If the
+ * number can't be parsed (invalid syntax, out of range),
+ * `fallback` value is stored instead.
+ *
+ * Returns the remainder of the input string after parsing.
+ */
+StringRef parse_int(StringRef str, int fallback, int &dst, bool skip_space = true);
+
+/**
+ * Parse a float from an input string.
+ * The parsed result is stored in `dst`. The function skips
+ * leading white-space unless `skip_space=false`. If the
+ * number can't be parsed (invalid syntax, out of range),
+ * `fallback` value is stored instead.
+ *
+ * Returns the remainder of the input string after parsing.
+ */
+StringRef parse_float(StringRef str, float fallback, float &dst, bool skip_space = true);
+
+/**
+ * Parse a number of white-space separated floats from an input string.
+ * The parsed `count` numbers are stored in `dst`. If a
+ * number can't be parsed (invalid syntax, out of range),
+ * `fallback` value is stored instead.
+ *
+ * Returns the remainder of the input string after parsing.
+ */
+StringRef parse_floats(StringRef str, float fallback, float *dst, int count);
+
+} // namespace blender::io::obj