The code.ill.fr has been recreated and upgraded with the latest version this weekend, If you encounter any problem please inform the Helpdesk.

Commit 4e686cd5 authored by legoc's avatar legoc
Browse files

(split) Added strings classes in C++

parent b525fbd1
......@@ -32,9 +32,18 @@ find_package(ZeroMQ REQUIRED)
find_package(Rapidjson REQUIRED)
#------------------------------------------------------------
# Libraries
# C++ version
#------------------------------------------------------------
#target_compile_features(cameo PUBLIC cxx_std_11)
# https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_EXTENSIONS OFF)
#------------------------------------------------------------
# Libraries
#------------------------------------------------------------
#---------------
# Files
......@@ -50,7 +59,7 @@ target_include_directories(cameo
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
#target_compile_features(cameo PUBLIC cxx_std_11)
set_target_properties(cameo PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${PROJECT_VERSION}
......@@ -69,6 +78,7 @@ write_basic_package_version_file(cameoConfigVersion.cmake
# Tests
#------------------------------------------------------------
# Note: execute ctest --verbose to have more details
enable_testing()
add_subdirectory(tests)
......
......@@ -22,6 +22,10 @@
namespace cameo {
struct Name {
static bool check(const std::string& str);
};
std::vector<std::string> split(const std::string& str, char c);
class Endpoint {
......@@ -41,6 +45,65 @@ private:
int m_port;
};
class NameId {
public:
/**
* Constructor.
*/
NameId(const std::string& name, int id = -1);
const std::string& getName() const;
/**
* Return the id. A -1 value indicates that there is no id.
*/
int getId() const;
static NameId parse(const std::string& str);
std::string toString() const;
private:
std::string m_name;
int m_id;
};
class ApplicationIdentity {
public:
ApplicationIdentity(const NameId& nameId, const Endpoint& endpoint);
const NameId& getNameId() const;
const Endpoint& getEndpoint() const;
static ApplicationIdentity parse(const std::string& str);
std::string toString() const;
private:
NameId m_nameId;
Endpoint m_endpoint;
};
class ApplicationAndStarterIdentities {
public:
ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter);
const ApplicationIdentity& getApplication() const;
const ApplicationIdentity& getStarter() const;
static ApplicationAndStarterIdentities parse(const std::string& str);
std::string toString() const;
private:
ApplicationIdentity m_application;
ApplicationIdentity m_starter;
};
}
#endif
......@@ -16,18 +16,26 @@
#include "Strings.h"
#include "BadFormatException.h"
#include <iostream>
#include <regex>
using namespace std;
namespace cameo {
bool Name::check(const std::string& str) {
regex nameRegex("[a-zA-Z0-9\\-_]+");
std::cmatch m;
return regex_match(str.c_str(), m, nameRegex);
}
std::vector<std::string> split(const std::string& str, char c) {
vector<string> result;
int lastIndex = 0;
int index = str.find(c);
string::size_type lastIndex = 0;
string::size_type index = str.find(c);
while (index != string::npos) {
result.push_back(str.substr(lastIndex, index - lastIndex));
lastIndex = index + 1;
......@@ -57,7 +65,7 @@ Endpoint Endpoint::parse(const std::string& str) {
throw new BadFormatException("Bad format for endpoint " + str);
}
string substr = str.substr(7);
string substr = str.substr(6);
vector<string> tokens = split(substr, ':');
if (tokens.size() != 2) {
......@@ -81,4 +89,144 @@ std::string Endpoint::toString() const {
return string("tcp://") + m_address + ":" + to_string(m_port);
}
NameId::NameId(const std::string& name, int id) :
m_name(name),
m_id(id) {
}
const std::string& NameId::getName() const {
return m_name;
}
int NameId::getId() const {
return m_id;
}
NameId NameId::parse(const std::string& str) {
vector<string> tokens = split(str, '.');
if (tokens.size() > 2) {
throw BadFormatException("Bad format for nameid " + str);
}
string name = tokens[0];
int id = -1;
if (tokens.size() == 2) {
try {
id = stoi(tokens[1]);
}
catch (...) {
throw new BadFormatException("Bad format for endpoint " + str);
}
}
return NameId(name, id);
}
std::string NameId::toString() const {
if (m_id > 0) {
return m_name + "." + to_string(m_id);
}
return m_name;
}
ApplicationIdentity::ApplicationIdentity(const NameId& nameId, const Endpoint& endpoint) :
m_nameId(nameId),
m_endpoint(endpoint) {
}
const NameId& ApplicationIdentity::getNameId() const {
return m_nameId;
}
const Endpoint& ApplicationIdentity::getEndpoint() const {
return m_endpoint;
}
ApplicationIdentity ApplicationIdentity::parse(const std::string& str) {
vector<string> tokens = split(str, '@');
if (tokens.size() != 2) {
throw BadFormatException("Bad format for application identity " + str);
}
return ApplicationIdentity(NameId::parse(tokens[0]), Endpoint::parse(tokens[1]));
}
std::string ApplicationIdentity::toString() const {
return m_nameId.toString() + "@" + m_endpoint.toString();
}
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
m_application(application),
m_starter(starter) {
}
const ApplicationIdentity& ApplicationAndStarterIdentities::getApplication() const {
return m_application;
}
const ApplicationIdentity& ApplicationAndStarterIdentities::getStarter() const {
return m_starter;
}
ApplicationAndStarterIdentities ApplicationAndStarterIdentities::parse(const std::string& str) {
// The string is either <name>@<endpoint>:<name>@<endpoint> or <name>@<endpoint>:
// To separate the two identities, we search for the last : before the last @.
string::size_type firstIndex = str.find_first_of('@');
if (firstIndex == string::npos) {
throw new BadFormatException("Bad format for application and starter identities " + str);
}
string::size_type index = str.find_last_of('@');
if (index == firstIndex) {
// Format <name>@<endpoint>:
if (str[str.length() - 1] != ':') {
throw BadFormatException("Bad format for application and starter identities " + str);
}
string applicationString = str.substr(0, str.length() - 1);
ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
return ApplicationAndStarterIdentities(application, application);
}
else {
// Format <name>@<endpoint>:<name>@<endpoint>
string substring = str.substr(0, index);
index = substring.find_last_of(':');
if (index == string::npos) {
throw BadFormatException("Bad format for application and starter identities " + str);
}
string applicationString = str.substr(0, index);
string starterString = str.substr(index + 1, str.length() - index);
ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
ApplicationIdentity starter = ApplicationIdentity::parse(starterString);
return ApplicationAndStarterIdentities(application, starter);
}
}
std::string ApplicationAndStarterIdentities::toString() const {
// if (starter != null) {
// return application + ":" + starter;
// }
// return application + ":";
return "";
}
}
add_executable(teststrings TestStrings.cpp)
set(CMAKE_CXX_STANDARD 17)
target_link_libraries(teststrings PUBLIC
add_executable(testendpoint TestEndpoint.cpp)
target_link_libraries(testendpoint PUBLIC
cameo
)
add_test(endpoint testendpoint)
add_executable(testname TestName.cpp)
target_link_libraries(testname PUBLIC
cameo
)
add_test(name testname)
add_executable(testnameid TestNameId.cpp)
target_link_libraries(testnameid PUBLIC
cameo
)
add_test(cameo teststrings)
\ No newline at end of file
add_test(nameid testnameid)
\ No newline at end of file
......@@ -17,7 +17,7 @@
#ifndef CAMEO_TEST_H_
#define CAMEO_TEST_H_
#define ALEPH_ASSERT_THROW( condition ) \
#define CAMEO_ASSERT_TRUE( condition ) \
{ \
if( !( condition ) ) \
{ \
......@@ -30,7 +30,7 @@
} \
}
#define ALEPH_ASSERT_EQUAL( x, y ) \
#define CAMEO_ASSERT_EQUAL( x, y ) \
{ \
if( ( x ) != ( y ) ) \
{ \
......
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
Endpoint endpoint("gamma75", 9999);
CAMEO_ASSERT_TRUE("tcp://gamma75:9999" == endpoint.toString());
endpoint = Endpoint::parse("tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("gamma75" == endpoint.getAddress());
CAMEO_ASSERT_EQUAL(9999, endpoint.getPort());
endpoint = Endpoint::parse("tcp://175.29.285.15:9999");
CAMEO_ASSERT_TRUE("175.29.285.15" == endpoint.getAddress());
CAMEO_ASSERT_EQUAL(9999, endpoint.getPort());
bool error = false;
try {
Endpoint::parse("tcp:/gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
error = false;
try {
Endpoint::parse("tcp://gamma75:");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
......@@ -7,10 +7,14 @@ using namespace cameo;
int main(int argc, char *argv[]) {
Endpoint endpoint("gamma75", 9999);
cout << endpoint.toString() << endl;
CAMEO_ASSERT_TRUE(Name::check("myapp"));
CAMEO_ASSERT_TRUE(Name::check("MyApp"));
CAMEO_ASSERT_TRUE(Name::check("MyApp0"));
CAMEO_ASSERT_TRUE(Name::check("My-App0"));
CAMEO_ASSERT_TRUE(Name::check("My_App0"));
ALEPH_ASSERT_EQUAL(1, 1);
CAMEO_ASSERT_TRUE(!Name::check("myapp!"));
CAMEO_ASSERT_TRUE(!Name::check("my app"));
return 0;
}
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
CAMEO_ASSERT_TRUE("my-app.31" == NameId("my-app", 31).toString());
NameId nameId = NameId::parse("my-app.31");
CAMEO_ASSERT_TRUE("my-app" == nameId.getName());
CAMEO_ASSERT_EQUAL(31, nameId.getId());
nameId = NameId::parse("my-app32");
CAMEO_ASSERT_EQUAL(-1, nameId.getId());
bool error = false;
try {
NameId::parse("my-app.ff");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment