OGS
Applications/FileIO/GocadIO/Property.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Property.h"
5
6#include <algorithm>
7#include <boost/tokenizer.hpp>
8#include <iterator>
9#include <sstream>
10
11#include "BaseLib/StringTools.h"
12
13namespace FileIO
14{
15namespace Gocad
16{
17std::ostream& operator<<(std::ostream& os, Property const& p)
18{
19 return os << "'" << p._property_name << "' '" << p._property_id << "' '"
21 << "'";
22}
23
24Property parseGocadPropertyMetaData(std::string& line, std::istream& in,
25 std::string const& path)
26{
27 boost::char_separator<char> sep("\t ");
28 boost::tokenizer<boost::char_separator<char>> tokens(line, sep);
29 auto tok_it(tokens.begin());
30 // A property section in Gocad file starts with a line
31 // PROPERTY id "property_name"
32 if (*tok_it != "PROPERTY")
33 {
34 ERR("Expected PROPERTY keyword but '{:s}' found.", tok_it->c_str());
35 throw std::runtime_error(
36 "In parseGocadPropertyMetaData() expected PROPERTY keyword not "
37 "found.");
38 }
39 tok_it++;
40
41 Property prop;
42 prop._property_id = std::stoul(*tok_it);
43 tok_it++;
44 prop._property_name = *tok_it;
45 tok_it++;
46 while (tok_it != tokens.end())
47 {
48 prop._property_name += " " + *tok_it;
49 tok_it++;
50 }
51 BaseLib::trim(prop._property_name, '\"');
52
53 auto checkPropertyID =
54 [](boost::tokenizer<boost::char_separator<char>>::iterator const&
55 tok_it,
56 Property const& prop)
57 {
58 if (!prop.checkID(*tok_it))
59 {
60 throw std::runtime_error(
61 "parseGocadPropertyMetaData(): id mismatch.");
62 }
63 };
64
65 while (std::getline(in, line))
66 {
67 if (line.empty())
68 {
69 continue;
70 }
71 if (line.back() == '\r')
72 {
73 line.pop_back();
74 }
75 tokens.assign(line);
76
77 tok_it = tokens.begin();
78 // this is the last entry of the property
79 if (*tok_it == "PROP_FILE")
80 {
81 checkPropertyID(++tok_it, prop);
82 tok_it++;
83 std::string tmp(*tok_it);
84 tok_it++;
85 while (tok_it != tokens.end())
86 {
87 tmp += " " + *tok_it;
88 tok_it++;
89 }
90 BaseLib::trim(tmp, '\"');
91 if (tmp.front() == ' ')
92 {
93 tmp.erase(tmp.begin());
94 }
95 prop._property_data_fname = path + tmp;
96 return prop;
97 }
98
99 if (*tok_it == "PROPERTY_CLASS")
100 {
101 checkPropertyID(++tok_it, prop);
102 tok_it++;
103 prop._property_class_name = *tok_it;
104 }
105
106 if (*tok_it == "PROPERTY_SUBCLASS")
107 {
108 checkPropertyID(++tok_it, prop);
109 tok_it++;
110 if (*tok_it != "QUANTITY" && *tok_it != "ENUM")
111 {
112 ERR("Expected keywords QUANTITY or ENUM, but found '{:s}'.",
113 tok_it->c_str());
114 throw std::runtime_error(
115 "parseGocadPropertyMetaData(): Expected keywords QUANTITY "
116 "or ENUM, but found '" +
117 *tok_it + "'.");
118 }
119 if (*tok_it == "QUANTITY")
120 {
121 tok_it++;
122 prop._property_data_type = *tok_it;
123 }
124 }
125
126 if (*tok_it == "PROP_UNIT" || *tok_it == "PROP_ORIGINAL_UNIT")
127 {
128 checkPropertyID(++tok_it, prop);
129 tok_it++;
130 prop._property_unit = *tok_it;
131 }
132
133 if (*tok_it == "PROP_NO_DATA_VALUE")
134 {
135 checkPropertyID(++tok_it, prop);
136 tok_it++;
137 prop._property_no_data_value = std::stoul(*tok_it);
138 }
139 }
140 return prop;
141}
142
143} // end namespace Gocad
144} // end namespace FileIO
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void trim(std::string &str, char ch)
Property parseGocadPropertyMetaData(std::string &line, std::istream &in, std::string const &path)
std::ostream & operator<<(std::ostream &os, CoordinateSystem const &c)
bool checkID(std::string const &id_string) const