2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-05-12 06:31:13 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-04 21:12:06 +00:00
|
|
|
|
2015-03-26 23:18:28 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/core.h>
|
2014-08-04 21:12:06 +00:00
|
|
|
|
2015-03-26 23:18:28 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-08-04 23:08:49 +00:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2014-08-06 22:55:46 +00:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2014-08-04 21:12:06 +00:00
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
namespace osquery {
|
2014-08-04 21:12:06 +00:00
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
std::vector<std::string> split(const std::string& s, const std::string& delim) {
|
2014-08-04 21:12:06 +00:00
|
|
|
std::vector<std::string> elems;
|
2014-08-06 22:55:46 +00:00
|
|
|
boost::split(elems, s, boost::is_any_of(delim));
|
2014-08-15 07:25:30 +00:00
|
|
|
auto start = std::remove_if(
|
|
|
|
elems.begin(), elems.end(), [](const std::string& s) { return s == ""; });
|
2014-08-04 21:12:06 +00:00
|
|
|
elems.erase(start, elems.end());
|
2014-08-04 23:08:49 +00:00
|
|
|
for (auto& each : elems) {
|
2014-08-06 22:55:46 +00:00
|
|
|
boost::algorithm::trim(each);
|
2014-08-04 23:08:49 +00:00
|
|
|
}
|
2014-08-04 21:12:06 +00:00
|
|
|
return elems;
|
|
|
|
}
|
2015-03-26 23:18:28 +00:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& s,
|
|
|
|
const std::string& delim,
|
|
|
|
size_t occurences) {
|
|
|
|
// Split the string normally with the required delimiter.
|
|
|
|
auto content = split(s, delim);
|
|
|
|
// While the result split exceeds the number of requested occurences, join.
|
|
|
|
std::vector<std::string> accumulator;
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
for (size_t i = 0; i < content.size(); i++) {
|
|
|
|
if (i < occurences) {
|
|
|
|
elems.push_back(content.at(i));
|
|
|
|
} else {
|
|
|
|
accumulator.push_back(content.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Join the optional accumulator.
|
|
|
|
if (accumulator.size() > 0) {
|
|
|
|
elems.push_back(join(accumulator, delim));
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string join(const std::vector<std::string>& s, const std::string& tok) {
|
|
|
|
return boost::algorithm::join(s, tok);
|
|
|
|
}
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|