Coverage Report

Created: 2025-08-29 07:11

/src/PROJ/src/wkt_parser.cpp
Line
Count
Source
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
35
875
void pj_wkt_error(pj_wkt_parse_context *context, const char *msg) {
36
875
    context->errorMsg = "Parsing error : ";
37
875
    context->errorMsg += msg;
38
875
    context->errorMsg += ". Error occurred around:\n";
39
40
875
    std::string ctxtMsg;
41
875
    const int n = static_cast<int>(context->pszLastSuccess - context->pszInput);
42
875
    int start_i = std::max(0, n - 40);
43
38.5k
    for (int i = start_i; i < n + 40 && context->pszInput[i]; i++) {
44
37.6k
        if (context->pszInput[i] == '\r' || context->pszInput[i] == '\n') {
45
168
            if (i > n) {
46
34
                break;
47
134
            } else {
48
134
                ctxtMsg.clear();
49
134
                start_i = i + 1;
50
134
            }
51
37.5k
        } else {
52
37.5k
            ctxtMsg += context->pszInput[i];
53
37.5k
        }
54
37.6k
    }
55
875
    context->errorMsg += ctxtMsg;
56
875
    context->errorMsg += '\n';
57
11.5k
    for (int i = start_i; i < n; i++)
58
10.6k
        context->errorMsg += ' ';
59
875
    context->errorMsg += '^';
60
875
}