GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/parameters.cpp Lines: 36 36 100.0 %
Date: 2017-11-29 Branches: 64 98 65.3 %

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

668
    std::string item = trim(str);
33
234
    const size_t sz = item.size();
34

234
    if (sz > 1)
35
    {
36

136
        if (str[0] == '\"' &&
37


170
            str[sz - 1] == '\"' &&
38
68
            str[sz - 2] != '\\')
39
        {
40

68
            item = item.substr(1, sz - 2);
41



238
            replaceAll(item, "\\\"", "\"");
42

34
            tokens.push_back(item);
43
34
            return;
44
        }
45
    }
46



1400
    replaceAll(item, "\\\"", "\"");
47

200
    if (!item.empty())
48

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




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

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

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

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

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