1 /******************************************************************************
2 * Project: PROJ
3 * Purpose: WKT parser common routines
4 * Author: Even Rouault, <even.rouault at spatialys.com>
5 *
6 ******************************************************************************
7 * Copyright (c) 2018 Even Rouault, <even.rouault at spatialys.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 ****************************************************************************/
27
28 #include "wkt_parser.hpp"
29
30 #include <algorithm>
31 #include <string>
32
33 // ---------------------------------------------------------------------------
34
pj_wkt_error(pj_wkt_parse_context * context,const char * msg)35 void pj_wkt_error(pj_wkt_parse_context *context, const char *msg) {
36 context->errorMsg = "Parsing error : ";
37 context->errorMsg += msg;
38 context->errorMsg += ". Error occurred around:\n";
39
40 std::string ctxtMsg;
41 const int n = static_cast<int>(context->pszLastSuccess - context->pszInput);
42 int start_i = std::max(0, n - 40);
43 for (int i = start_i; i < n + 40 && context->pszInput[i]; i++) {
44 if (context->pszInput[i] == '\r' || context->pszInput[i] == '\n') {
45 if (i > n) {
46 break;
47 } else {
48 ctxtMsg.clear();
49 start_i = i + 1;
50 }
51 } else {
52 ctxtMsg += context->pszInput[i];
53 }
54 }
55 context->errorMsg += ctxtMsg;
56 context->errorMsg += '\n';
57 for (int i = start_i; i < n; i++)
58 context->errorMsg += ' ';
59 context->errorMsg += '^';
60 }
61