Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up utils header file to be self-contained #696

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ along with sysdig. If not, see <http://www.gnu.org/licenses/>.
#include "filterchecks.h"
#include "chisel.h"
#include "protodecoder.h"
#include "json/json.h"
#include "uri.h"
#ifndef _WIN32
#include "curl/curl.h"
Expand Down
69 changes: 38 additions & 31 deletions userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ along with sysdig. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <string>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <locale>
#include <sstream>

#include <scap.h>
#include "json/json.h"

class sinsp_evttables;
typedef union _sinsp_sockinfo sinsp_sockinfo;
typedef union _ipv4tuple ipv4tuple;
Expand Down Expand Up @@ -80,12 +87,12 @@ class sinsp_utils
//
// Given a string, scan the event list and find the longest argument that the input string contains
//
static const struct ppm_param_info* find_longest_matching_evt_param(string name);
static const struct ppm_param_info* find_longest_matching_evt_param(std::string name);

//
// Get the list of filtercheck fields
//
static void get_filtercheck_fields_info(vector<const filter_check_info*>* list);
static void get_filtercheck_fields_info(std::vector<const filter_check_info*>* list);

static uint64_t get_current_time_ns();

Expand Down Expand Up @@ -116,7 +123,7 @@ struct g_invalidchar
}
};

inline void sanitize_string(string &str)
inline void sanitize_string(std::string &str)
{
// It turns out with -O3 (release flags) using erase and
// remove_if is slighly faster than the inline version that
Expand Down Expand Up @@ -149,33 +156,33 @@ SINSP_PUBLIC int gettimeofday(struct timeval *tv, struct timezone2 *tz);
///////////////////////////////////////////////////////////////////////////////
// gethostname wrapper
///////////////////////////////////////////////////////////////////////////////
string sinsp_gethostname();
std::string sinsp_gethostname();

///////////////////////////////////////////////////////////////////////////////
// tuples to string
///////////////////////////////////////////////////////////////////////////////

// each of these functions uses values in network byte order

string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve);
string ipv6tuple_to_string(_ipv6tuple* tuple, bool resolve);
string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve);
string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve);
std::string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve);
std::string ipv6tuple_to_string(_ipv6tuple* tuple, bool resolve);
std::string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve);
std::string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve);

// `l4proto` should be of type scap_l4_proto, but since it's an enum sometimes
// is used as int and we would have to cast
// `port` must be saved with network byte order
// `l4proto` could be neither TCP nor UDP, in this case any protocol will be
// matched
string port_to_string(uint16_t port, uint8_t l4proto, bool resolve);
std::string port_to_string(uint16_t port, uint8_t l4proto, bool resolve);

///////////////////////////////////////////////////////////////////////////////
// String helpers
///////////////////////////////////////////////////////////////////////////////
vector<string> sinsp_split(const string& s, char delim);
std::vector<std::string> sinsp_split(const std::string& s, char delim);

template<typename It>
string sinsp_join(It begin, It end, char delim)
std::string sinsp_join(It begin, It end, char delim)
{
if(begin == end)
{
Expand All @@ -191,31 +198,31 @@ string sinsp_join(It begin, It end, char delim)
return ss.str();
}

string& ltrim(string& s);
string& rtrim(string& s);
string& trim(string& s);
string& replace_in_place(string& s, const string& search, const string& replacement);
string replace(const string& str, const string& search, const string& replacement);
std::string& ltrim(std::string& s);
std::string& rtrim(std::string& s);
std::string& trim(std::string& s);
std::string& replace_in_place(std::string& s, const std::string& search, const std::string& replacement);
std::string replace(const std::string& str, const std::string& search, const std::string& replacement);

///////////////////////////////////////////////////////////////////////////////
// number parser
///////////////////////////////////////////////////////////////////////////////
class sinsp_numparser
{
public:
static uint8_t parseu8(const string& str);
static int8_t parsed8(const string& str);
static uint16_t parseu16(const string& str);
static int16_t parsed16(const string& str);
static uint32_t parseu32(const string& str);
static int32_t parsed32(const string& str);
static uint64_t parseu64(const string& str);
static int64_t parsed64(const string& str);

static bool tryparseu32(const string& str, uint32_t* res);
static bool tryparsed32(const string& str, int32_t* res);
static bool tryparseu64(const string& str, uint64_t* res);
static bool tryparsed64(const string& str, int64_t* res);
static uint8_t parseu8(const std::string& str);
static int8_t parsed8(const std::string& str);
static uint16_t parseu16(const std::string& str);
static int16_t parsed16(const std::string& str);
static uint32_t parseu32(const std::string& str);
static int32_t parsed32(const std::string& str);
static uint64_t parseu64(const std::string& str);
static int64_t parsed64(const std::string& str);

static bool tryparseu32(const std::string& str, uint32_t* res);
static bool tryparsed32(const std::string& str, int32_t* res);
static bool tryparseu64(const std::string& str, uint64_t* res);
static bool tryparsed64(const std::string& str, int64_t* res);

static bool tryparseu32_fast(const char* str, uint32_t strlen, uint32_t* res);
static bool tryparsed32_fast(const char* str, uint32_t strlen, int32_t* res);
Expand Down Expand Up @@ -285,8 +292,8 @@ class simple_lifo_queue
}

private:
list<OBJ*> m_avail_list;
list<OBJ*> m_full_list;
std::list<OBJ*> m_avail_list;
std::list<OBJ*> m_full_list;
};

///////////////////////////////////////////////////////////////////////////////
Expand Down