GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/parameters.cpp Lines: 36 36 100.0 %
Date: 2021-03-17 Branches: 74 110 67.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "utils/parameters.h"
23
24
#include "utils/stringutils.h"
25
26
#include "debug.h"
27
28
static inline void addToken(StringVect &tokens,
29
                            std::string str) A_INLINE;
30
static inline void addToken(StringVect &tokens,
31
                            std::string str)
32
{
33

334
    std::string item = trim(str);
34
117
    const size_t sz = item.size();
35

117
    if (sz > 1)
36
    {
37


117
        if (str[0] == '\"' &&
38


85
            str[sz - 1] == '\"' &&
39
34
            str[sz - 2] != '\\')
40
        {
41

34
            item = item.substr(1, sz - 2);
42



119
            replaceAll(item, "\\\"", "\"");
43

17
            tokens.push_back(item);
44
17
            return;
45
        }
46
    }
47



700
    replaceAll(item, "\\\"", "\"");
48

100
    if (!item.empty())
49

35
        tokens.push_back(item);
50
}
51
52
static inline size_t findNextQuote(const std::string &str,
53
                                   const char quote,
54
                                   const size_t pos) A_INLINE;
55
static inline size_t findNextQuote(const std::string &str,
56
                                   const char quote,
57
                                   const size_t pos)
58
{
59
145
    size_t idx = str.find(quote, pos);
60


328
    while (idx > 0 &&
61




200
           idx != std::string::npos &&
62
36
           str[idx - 1] == '\\')
63
    {
64
19
        idx = str.find(quote, idx + 1);
65
    }
66
    return idx;
67
}
68
69
static inline size_t findNextSplit(const std::string &str,
70
                                   const std::string &separator,
71
                                   const char quote) A_INLINE;
72
static inline size_t findNextSplit(const std::string &str,
73
                                   const std::string &separator,
74
                                   const char quote)
75
{
76
33
    size_t pos = 0;
77
    while (true)
78
    {
79
        // search for next separator
80
130
        size_t idx1 = findAny(str, separator, pos);
81
        // search for next open quote, skipping escaped quotes
82
260
        size_t idx2 = findNextQuote(str, quote, pos);
83

130
        if (idx2 == std::string::npos)  // not quotes, return next separator
84
            return idx1;
85

25
        else if (idx1 == std::string::npos)  // also no separators, return npos
86
            return std::string::npos;
87
88

17
        if (idx2 < idx1)
89
        {   // first is quote and after separator: for example "test line", ...
90
30
            idx2 = findNextQuote(str, quote, idx2 + 1);
91

15
            if (idx2 == std::string::npos)
92
                return std::string::npos;  // no close quote, here error
93
            // set position for next separator or quote
94
13
            pos = idx2 + 1;
95
        }
96
        else
97
        {
98
            return idx1;
99
        }
100
    }
101
}
102
103
33
bool splitParameters(StringVect &tokens,
104
                     std::string text,
105
                     const std::string &separator,
106
                     const char quote)
107
{
108
33
    size_t idx = findNextSplit(text, separator, quote);
109
110
201
    while (idx != std::string::npos)
111
    {
112
168
        std::string item = text.substr(0, idx);
113
252
        addToken(tokens, item);
114
168
        text = text.substr(idx + 1);
115
168
        idx = findNextSplit(text, separator, quote);
116
    }
117
118
99
    addToken(tokens, text);
119
33
    return true;
120
}