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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
// General string-splitting methods. These methods along with lexical analyzer
// classes (such as those defined in lex.hpp and OptionList::LexComment) can be
// used as a basis for parsers.
#ifndef OPENVPN_COMMON_SPLIT_H
#define OPENVPN_COMMON_SPLIT_H
#include <string>
#include <vector>
#include <utility>
#include <openvpn/common/size.hpp>
#include <openvpn/common/lex.hpp>
#include <openvpn/common/clamp_typerange.hpp>
using namespace openvpn::numeric_util;
namespace openvpn::Split {
enum
{
TRIM_LEADING_SPACES = (1 << 0),
TRIM_SPECIAL = (1 << 1), // trims quotes (but respects their content)
};
struct NullLimit
{
void add_term()
{
}
};
// Split a string using a character (such as ',') as a separator.
// Types:
// V : string vector of return data
// LEX : lexical analyzer class such as StandardLex
// LIM : limit class such as OptionList::Limits
// Args:
// ret : return data -- a list of strings
// input : input string to be split
// split_by : separator
// flags : TRIM_LEADING_SPACES, TRIM_SPECIAL
// max_terms : the size of the returned string list will be, at most, this value + 1. Pass
// ~0 to disable.
// lim : an optional limits object such as OptionList::Limits
template <typename V, typename LEX, typename LIM>
inline void by_char_void(V &ret, const std::string &input, const char split_by, const unsigned int flags = 0, const unsigned int max_terms = ~0, LIM *lim = nullptr)
{
LEX lex;
unsigned int nterms = 0;
std::string term;
for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
{
const char c = *i;
lex.put(c);
if (!lex.in_quote() && c == split_by && nterms < max_terms)
{
if (lim)
lim->add_term();
ret.push_back(std::move(term));
++nterms;
term = "";
}
else if ((!(flags & TRIM_SPECIAL) || lex.available())
&& (!(flags & TRIM_LEADING_SPACES) || !term.empty() || !SpaceMatch::is_space(c)))
term += c;
}
if (lim)
lim->add_term();
ret.push_back(std::move(term));
}
// convenience method that returns data rather than modifying an in-place argument
template <typename V, typename LEX, typename LIM>
inline V by_char(const std::string &input, const char split_by, const unsigned int flags = 0, const unsigned int max_terms = ~0, LIM *lim = nullptr)
{
V ret;
by_char_void<V, LEX, LIM>(ret, input, split_by, flags, max_terms, lim);
return ret;
}
// Split a string using spaces as a separator.
// Types:
// V : string vector of return data
// LEX : lexical analyzer class such as StandardLex
// SPACE : class that we use to differentiate between space and non-space chars
// LIM : limit class such as OptionList::Limits
// Args:
// ret : return data -- a list of strings
// input : input string to be split
// lim : an optional limits object such as OptionList::Limits
template <typename V, typename LEX, typename SPACE, typename LIM>
inline void by_space_void(V &ret, const std::string &input, LIM *lim = nullptr)
{
LEX lex;
std::string term;
bool defined = false;
for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
{
const char c = *i;
lex.put(c);
if (lex.in_quote())
defined = true;
if (lex.available())
{
const char tc = clamp_to_default<char>(lex.get(), '?');
if (!SPACE::is_space(tc) || lex.in_quote())
{
defined = true;
term += tc;
}
else if (defined)
{
if (lim)
lim->add_term();
ret.push_back(std::move(term));
term = "";
defined = false;
}
}
}
if (defined)
{
if (lim)
lim->add_term();
ret.push_back(std::move(term));
}
}
// convenience method that returns data rather than modifying an in-place argument
template <typename V, typename LEX, typename SPACE, typename LIM>
inline V by_space(const std::string &input, LIM *lim = nullptr)
{
V ret;
by_space_void<V, LEX, SPACE, LIM>(ret, input, lim);
return ret;
}
} // namespace openvpn::Split
#endif // OPENVPN_COMMON_SPLIT_H
|