ManaPlus
parameters.cpp
Go to the documentation of this file.
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  std::string item = trim(str);
34  const size_t sz = item.size();
35  if (sz > 1)
36  {
37  if (str[0] == '\"' &&
38  str[sz - 1] == '\"' &&
39  str[sz - 2] != '\\')
40  {
41  item = item.substr(1, sz - 2);
42  replaceAll(item, "\\\"", "\"");
43  tokens.push_back(item);
44  return;
45  }
46  }
47  replaceAll(item, "\\\"", "\"");
48  if (!item.empty())
49  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  size_t idx = str.find(quote, pos);
60  while (idx > 0 &&
61  idx != std::string::npos &&
62  str[idx - 1] == '\\')
63  {
64  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  size_t pos = 0;
77  while (true)
78  {
79  // search for next separator
80  size_t idx1 = findAny(str, separator, pos);
81  // search for next open quote, skipping escaped quotes
82  size_t idx2 = findNextQuote(str, quote, pos);
83  if (idx2 == std::string::npos) // not quotes, return next separator
84  return idx1;
85  else if (idx1 == std::string::npos) // also no separators, return npos
86  return std::string::npos;
87 
88  if (idx2 < idx1)
89  { // first is quote and after separator: for example "test line", ...
90  idx2 = findNextQuote(str, quote, idx2 + 1);
91  if (idx2 == std::string::npos)
92  return std::string::npos; // no close quote, here error
93  // set position for next separator or quote
94  pos = idx2 + 1;
95  }
96  else
97  {
98  return idx1;
99  }
100  }
101 }
102 
104  std::string text,
105  const std::string &separator,
106  const char quote)
107 {
108  size_t idx = findNextSplit(text, separator, quote);
109 
110  while (idx != std::string::npos)
111  {
112  std::string item = text.substr(0, idx);
113  addToken(tokens, item);
114  text = text.substr(idx + 1);
115  idx = findNextSplit(text, separator, quote);
116  }
117 
118  addToken(tokens, text);
119  return true;
120 }
std::string trim(std::string const &str)
static size_t findNextSplit(const std::string &str, const std::string &separator, const char quote) A_INLINE
Definition: parameters.cpp:72
static size_t findNextQuote(const std::string &str, const char quote, const size_t pos) A_INLINE
Definition: parameters.cpp:55
bool splitParameters(StringVect &tokens, std::string text, const std::string &separator, const char quote)
Definition: parameters.cpp:103
static void addToken(StringVect &tokens, std::string str) A_INLINE
Definition: parameters.cpp:30
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
size_t findAny(const std::string &text, const std::string &chars, const size_t pos)
std::vector< std::string > StringVect
Definition: stringvector.h:29