OGS
MediaCreation.cpp
Go to the documentation of this file.
1
10#include <spdlog/fmt/bundled/ranges.h>
11
12#include <range/v3/range/conversion.hpp>
13#include <range/v3/view/adjacent_remove_if.hpp>
14
15#include "BaseLib/Algorithm.h"
16#include "BaseLib/Logging.h"
17#include "BaseLib/StringTools.h"
19
20namespace MaterialLib
21{
22
23std::vector<int> splitMaterialIdString(std::string const& material_id_string)
24{
25 auto const material_ids_strings =
26 BaseLib::splitString(material_id_string, ',');
27
28 std::vector<int> material_ids;
29 for (auto& mid_str : material_ids_strings)
30 {
31 std::size_t num_chars_processed = 0;
32 int material_id;
33 try
34 {
35 material_id = std::stoi(mid_str, &num_chars_processed);
36 }
37 catch (std::invalid_argument&)
38 {
40 "Could not parse material ID from '{}' to a valid integer.",
41 mid_str);
42 }
43 catch (std::out_of_range&)
44 {
46 "Could not parse material ID from '{}'. The integer value of "
47 "the given string exceeds the permitted range.",
48 mid_str);
49 }
50
51 if (num_chars_processed != mid_str.size())
52 {
53 // Not the whole string has been parsed. Check the rest.
54 if (auto const it = std::find_if_not(
55 begin(mid_str) + num_chars_processed, end(mid_str),
56 [](unsigned char const c) { return std::isspace(c); });
57 it != end(mid_str))
58 {
60 "Could not parse material ID from '{}'. Please separate "
61 "multiple material IDs by comma only. Invalid character: "
62 "'{}' at position {}.",
63 mid_str, *it, distance(begin(mid_str), it));
64 }
65 }
66
67 material_ids.push_back(material_id);
68 };
69
70 return material_ids;
71}
72
73std::vector<int> parseMaterialIdString(
74 std::string const& material_id_string,
75 MeshLib::PropertyVector<int> const* const material_ids)
76{
77 if (material_id_string == "*")
78 {
79 if (material_ids == nullptr)
80 {
82 "MaterialIDs property is not defined in the mesh but it is "
83 "required to parse '*' definition.");
84 }
85
86 std::vector<int> material_ids_of_this_medium =
87 *material_ids |
88 ranges::views::adjacent_remove_if(std::equal_to<>()) |
89 ranges::to_vector;
90 BaseLib::makeVectorUnique(material_ids_of_this_medium);
91 DBUG("Catch all medium definition for material ids {}.",
92 fmt::join(material_ids_of_this_medium, ", "));
93 return material_ids_of_this_medium;
94 }
95
96 // Usual case of ids separated by comma.
97 return splitMaterialIdString(material_id_string);
98}
99
100} // namespace MaterialLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of string helper functions.
void makeVectorUnique(std::vector< T > &v)
Definition Algorithm.h:180
std::vector< std::string > splitString(std::string const &str)
std::vector< int > splitMaterialIdString(std::string const &material_id_string)
std::vector< int > parseMaterialIdString(std::string const &material_id_string, MeshLib::PropertyVector< int > const *const material_ids)