ManaPlus
Functions
parameters.cpp File Reference

(986a3bf)

#include "utils/parameters.h"
#include "utils/stringutils.h"
#include "debug.h"

Go to the source code of this file.

Functions

static void addToken (StringVect &tokens, std::string str) A_INLINE
 
static size_t findNextQuote (const std::string &str, const char quote, const size_t pos) A_INLINE
 
static size_t findNextSplit (const std::string &str, const std::string &separator, const char quote) A_INLINE
 
bool splitParameters (StringVect &tokens, std::string text, const std::string &separator, const char quote)
 

Function Documentation

◆ addToken()

static void addToken ( StringVect tokens,
std::string  str 
)
inlinestatic

Definition at line 30 of file parameters.cpp.

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 }
std::string trim(std::string const &str)
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)

References replaceAll(), and Catch::trim().

Referenced by splitParameters().

◆ findNextQuote()

static size_t findNextQuote ( const std::string &  str,
const char  quote,
const size_t  pos 
)
inlinestatic

Definition at line 55 of file parameters.cpp.

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 }

Referenced by findNextSplit().

◆ findNextSplit()

static size_t findNextSplit ( const std::string &  str,
const std::string &  separator,
const char  quote 
)
inlinestatic

Definition at line 72 of file parameters.cpp.

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 }
static size_t findNextQuote(const std::string &str, const char quote, const size_t pos) A_INLINE
Definition: parameters.cpp:55
size_t findAny(const std::string &text, const std::string &chars, const size_t pos)

References findAny(), and findNextQuote().

Referenced by splitParameters().

◆ splitParameters()

bool splitParameters ( StringVect tokens,
std::string  text,
const std::string &  separator,
const char  quote 
)

Definition at line 103 of file parameters.cpp.

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 }
static size_t findNextSplit(const std::string &str, const std::string &separator, const char quote) A_INLINE
Definition: parameters.cpp:72
static void addToken(StringVect &tokens, std::string str) A_INLINE
Definition: parameters.cpp:30

References addToken(), and findNextSplit().