File: ogr_utils.cpp

package info (click to toggle)
mapnik 4.2.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,592 kB
  • sloc: cpp: 163,859; python: 1,332; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (85 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include "ogr_utils.hpp"
#include <mapnik/datasource.hpp>

std::vector<ogr_utils::option_ptr> ogr_utils::split_open_options(std::string const& options)
{
    size_t i;
    bool escaped = false; // if previous character was a backslash to escape a backslash or space
    std::vector<ogr_utils::option_ptr> opts;
    std::string unescaped_str; // copy of input string but unescaped
    for (i = 0; i < options.size(); ++i)
    {
        char current = options.at(i);
        if (current == '\\')
        {
            if (escaped)
            {
                unescaped_str.push_back(current);
            }
            escaped = !escaped;
        }
        else if (current != ' ')
        {
            unescaped_str.push_back(current);
        }
        if (current == ' ' || i + 1 == options.size())
        {
            if (!escaped)
            {
                size_t count = unescaped_str.size();
                if (count > 0)
                {
                    option_ptr opt(new char[count + 1], [](char* arr) { delete[] arr; });
                    unescaped_str.copy(opt.get(), count);
                    opt[count] = '\0';
                    opts.push_back(std::move(opt));
                }
                unescaped_str = "";
            }
            else
            {
                escaped = false;
                unescaped_str.push_back(current);
            }
        }
    }
    if (escaped)
    {
        throw mapnik::datasource_exception("<open_options> parameter ends with single backslash");
    }
    opts.emplace_back(nullptr);
    return opts;
}

char** ogr_utils::open_options_for_ogr(std::vector<ogr_utils::option_ptr> const& options)
{
    char** for_ogr = new char*[options.size() + 1];
    for (size_t i = 0; i < options.size(); ++i)
    {
        for_ogr[i] = options.at(i).get();
    }
    for_ogr[options.size()] = nullptr;
    return for_ogr;
}