Skip to content
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
3 changes: 2 additions & 1 deletion common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ libswsscommon_la_SOURCES = \
notificationconsumer.cpp \
notificationproducer.cpp \
linkcache.cpp \
portmap.cpp
portmap.cpp \
tokenize.cpp

libswsscommon_la_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
Expand Down
12 changes: 5 additions & 7 deletions common/ipaddresses.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#include "ipaddresses.h"
#include "tokenize.h"

#include <sstream>

using namespace std;
using namespace swss;

#define IP_DELIMITER ','

IpAddresses::IpAddresses(const string &ipsStr)
{
string ip_str;
istringstream iss(ipsStr);

while (getline(iss, ip_str, ','))
{
IpAddress ip(ip_str);
auto ips = tokenize(ipsStr, IP_DELIMITER);
for (auto ip : ips)
m_ips.insert(ip);
}
}

void IpAddresses::add(const string &ipStr)
Expand Down
4 changes: 4 additions & 0 deletions common/portmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using namespace std;

namespace swss {

map<set<int>, string> handlePortMap(string file)
{
map<set<int>, string> port_map;
Expand Down Expand Up @@ -33,3 +35,5 @@ map<set<int>, string> handlePortMap(string file)

return port_map;
}

}
4 changes: 4 additions & 0 deletions common/portmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <set>
#include <sstream>

namespace swss {

std::map<std::set<int>, std::string> handlePortMap(std::string file);

}

#endif /* SWSS_COMMON_PORTMAP_H */
19 changes: 19 additions & 0 deletions common/tokenize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "tokenize.h"

using namespace std;

namespace swss {

vector<string> tokenize(const string &str, const char token)
{
string tmp;
vector<string> ret;
istringstream iss(str);

while (getline(iss, tmp, token))
ret.push_back(tmp);

return ret;
}

}
13 changes: 13 additions & 0 deletions common/tokenize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __TOKENIZE__
#define __TOKENIZE__

#include <sstream>
#include <vector>

namespace swss {

std::vector<std::string> tokenize(const std::string &, const char token);

}

#endif /* TOKENIZE */
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ CFLAGS_GTEST = -I $(top_srcdir)/googletest/googletest/include
LDADD_GTEST = $(top_srcdir)/googletest/build/googlemock/gtest/libgtest_main.a \
$(top_srcdir)/googletest/build/googlemock/gtest/libgtest.a

tests_SOURCES = redis_ut.cpp
tests_SOURCES = redis_ut.cpp \
tokenize_ut.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
Expand Down
49 changes: 49 additions & 0 deletions tests/tokenize_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "common/ipaddress.h"
#include "common/ipaddresses.h"
#include "common/tokenize.h"

#include "gtest/gtest.h"

using namespace std;
using namespace swss;

TEST(TOKENIZE, basic)
{
string str1 = "str1";
string str2 = "str2";

vector<string> origin;
origin.push_back(str1);
origin.push_back(str2);

string str_colon = "str1:str2";
vector<string> result = tokenize(str_colon, ':');

EXPECT_EQ(origin, result);
}

TEST(TOKENIZE, IP)
{
IpAddresses origin("192.168.0.1,192.168.0.2");

IpAddress ip1("192.168.0.1");
IpAddress ip2("192.168.0.2");

IpAddresses result;
result.add(ip1);
result.add(ip2);

EXPECT_EQ(origin, result);
}

TEST(TOKENIZE, IP_2)
{
IpAddresses origin("192.168.0.1");

IpAddress ip("192.168.0.1");

IpAddresses result;
result.add(ip);

EXPECT_EQ(origin, result);
}