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