Shapes
GIS made easy, a lightweight header-only planar geometry library for Modern C++
wkt_reader.hpp
1 #pragma once
2 
3 #include <ciso646>
4 #include <simo/geom/geometry.hpp>
5 #include <simo/exceptions.hpp>
6 #include <simo/io/wkt_parser.hpp>
7 #include <simo/io/wkt_lexer.hpp>
8 
9 namespace simo
10 {
11 namespace shapes
12 {
13 
21 {
22  public:
29  : m_parser(ParseAlloc(malloc))
30  {
31  }
32 
35  {
36  ParseFree(m_parser, free);
37  }
38 
47  wkt_result read(const std::string& wkt)
48  {
49  wkt_lexer lexer(wkt);
50  wkt_result result{};
51 
52 #ifdef SHAPES_VERBOSE
53  static char WKT_TRACE_PREFIX[] = "[shapes] ";
54  ParseTrace(stdout, WKT_TRACE_PREFIX);
55 #endif
56  while (true)
57  {
58  int token = lexer.scan();
59 #ifdef SHAPES_VERBOSE
60  std::cout << "--> " << lexer.get_token() << '\n';
61 #endif
62  if (token == WKT_END_OF_INPUT)
63  {
64  break;
65  }
66 
67  if (token == WKT_PARSE_ERROR)
68  {
69  throw exceptions::parse_error("lexer error at line " + std::to_string(lexer.get_position()));
70  }
71 
72  if (token == WKT_NUM)
73  {
74  Parse(m_parser, token, std::stod(lexer.get_token()), &result);
75  }
76  else
77  {
78  Parse(m_parser, token, 0, &result);
79  }
80 
81  if (result.parser_error)
82  {
83  throw exceptions::parse_error("parser error for token " + lexer.get_token());
84  }
85  }
86 
87  Parse(m_parser, 0, 0, &result);
88  if (result.parser_error)
89  {
90  throw exceptions::parse_error("parser error");
91  }
92  return result;
93  }
94 
95  private:
97  void* m_parser = nullptr;
98 };
99 
100 } // namespace shapes
101 } // namespace simo
wkt_result read(const std::string &wkt)
parse the given wkt string
Definition: wkt_reader.hpp:47
int scan()
scan the next token
Definition: wkt_lexer.hpp:42
A Well-known text (WKT) markup language parser results.
Definition: wkt_parser.hpp:38
Exception thrown when an error has been found while parsing.
Definition: exceptions.hpp:67
std::string get_token() const
returns the current token string
Definition: wkt_lexer.hpp:1000
~wkt_reader()
destructor
Definition: wkt_reader.hpp:34
size_t get_position()
returns the current reading position
Definition: wkt_lexer.hpp:1011
wkt_reader()
creates a wkt reader
Definition: wkt_reader.hpp:28