mirror of
https://github.com/valitydev/osquery-1.git
synced 2024-11-07 01:55:20 +00:00
zwass' comment on etc_host table
This commit is contained in:
parent
48c8ebed17
commit
4bec86c534
@ -22,18 +22,11 @@ aggregateQuery(const std::string& q, int& error_return, sqlite3* db);
|
||||
// be called in an executable's main() function
|
||||
void initOsquery(int argc, char *argv[]);
|
||||
|
||||
// Split a given string based on whitespace
|
||||
// Split a given string based on an optional deliminator. If not deliminator is
|
||||
// supplied, the string will be split based on whitespace.
|
||||
std::vector<std::string> split(const std::string& s);
|
||||
std::vector<std::string> split(const std::string& s,
|
||||
const std::string& regexp);
|
||||
|
||||
// Join a given string based on a given deliminator.
|
||||
std::string join(const std::vector<std::string>& v, const std::string& delim);
|
||||
|
||||
// trim the surrounding whitespace from a string
|
||||
std::string <rim(std::string &s);
|
||||
std::string &rtrim(std::string &s);
|
||||
std::string &trim(std::string &s);
|
||||
const std::string& delim);
|
||||
|
||||
}}
|
||||
|
||||
|
@ -11,8 +11,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
#include <glog/logging.h>
|
||||
@ -24,48 +24,18 @@ std::vector<std::string> split(const std::string& s) {
|
||||
}
|
||||
|
||||
std::vector<std::string> split(const std::string& s,
|
||||
const std::string& regexp) {
|
||||
const std::string& delim) {
|
||||
std::vector<std::string> elems;
|
||||
boost::split(elems, s, boost::is_any_of(regexp));
|
||||
boost::split(elems, s, boost::is_any_of(delim));
|
||||
auto start = std::remove_if(elems.begin(), elems.end(),
|
||||
std::bind1st(std::equal_to<std::string>(), std::string(""))
|
||||
);
|
||||
elems.erase(start, elems.end());
|
||||
for (auto& each : elems) {
|
||||
trim(each);
|
||||
boost::algorithm::trim(each);
|
||||
|
||||
}
|
||||
return elems;
|
||||
}
|
||||
|
||||
std::string join(const std::vector<std::string>& v, const std::string& delim) {
|
||||
return boost::algorithm::join(v, delim);
|
||||
}
|
||||
|
||||
std::string& ltrim(std::string &s) {
|
||||
s.erase(
|
||||
s.begin(),
|
||||
std::find_if(
|
||||
s.begin(),
|
||||
s.end(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))
|
||||
)
|
||||
);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string& rtrim(std::string &s) {
|
||||
s.erase(
|
||||
std::find_if(
|
||||
s.rbegin(),
|
||||
s.rend(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
|
||||
s.end()
|
||||
);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string& trim(std::string &s) {
|
||||
return ltrim(rtrim(s));
|
||||
}
|
||||
|
||||
}}
|
||||
|
@ -12,32 +12,11 @@ namespace osquery { namespace core {
|
||||
class TextTests : public testing::Test {};
|
||||
|
||||
TEST_F(TextTests, test_split) {
|
||||
for (auto i : generateSplitStringTestData()) {
|
||||
for (const auto& i : generateSplitStringTestData()) {
|
||||
EXPECT_EQ(split(i.test_string), i.test_vector);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TextTests, test_join_string) {
|
||||
for (auto i : generateJoinStringTestData()) {
|
||||
EXPECT_EQ(join(i.test_vector, i.delim), i.test_string);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TextTests, test_ltrim) {
|
||||
std::string s = " foo ";
|
||||
EXPECT_EQ(ltrim(s), "foo ");
|
||||
}
|
||||
|
||||
TEST_F(TextTests, test_rtrim) {
|
||||
std::string s = " foo ";
|
||||
EXPECT_EQ(rtrim(s), " foo");
|
||||
}
|
||||
|
||||
TEST_F(TextTests, test_trim) {
|
||||
std::string s = " foo ";
|
||||
EXPECT_EQ(trim(s), "foo");
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
#include <glog/logging.h>
|
||||
@ -44,7 +45,7 @@ QueryData parseEtcHostsContent(const std::string& content) {
|
||||
for (int i = 1; i < line.size(); ++i) {
|
||||
hostnames.push_back(line[i]);
|
||||
}
|
||||
r["hostnames"] = join(hostnames, " ");
|
||||
r["hostnames"] = boost::algorithm::join(hostnames, " ");
|
||||
}
|
||||
results.push_back(r);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user