1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "dse.h"
18 #include "wkt.hpp"
19 
20 #include <string.h>
21 
22 #include "macros.hpp"
23 
24 extern "C" {
25 
dse_point_from_wkt(const char * wkt,cass_double_t * x,cass_double_t * y)26 CassError dse_point_from_wkt(const char* wkt, cass_double_t* x, cass_double_t* y) {
27   return dse_point_from_wkt_n(wkt, SAFE_STRLEN(wkt), x, y);
28 }
29 
dse_point_from_wkt_n(const char * wkt,size_t wkt_length,cass_double_t * x,cass_double_t * y)30 CassError dse_point_from_wkt_n(const char* wkt, size_t wkt_length, cass_double_t* x,
31                                cass_double_t* y) {
32   WktLexer lexer(wkt, wkt_length);
33 
34   if (lexer.next_token() != WktLexer::TK_TYPE_POINT ||
35       lexer.next_token() != WktLexer::TK_OPEN_PAREN || lexer.next_token() != WktLexer::TK_NUMBER) {
36     return CASS_ERROR_LIB_BAD_PARAMS;
37   }
38 
39   *x = lexer.number();
40 
41   if (lexer.next_token() != WktLexer::TK_NUMBER) {
42     return CASS_ERROR_LIB_BAD_PARAMS;
43   }
44 
45   *y = lexer.number();
46 
47   // Look for the closing paren
48   if (lexer.next_token() != WktLexer::TK_CLOSE_PAREN) {
49     return CASS_ERROR_LIB_BAD_PARAMS;
50   }
51 
52   return CASS_OK;
53 }
54 
55 } // extern "C"
56