-
Notifications
You must be signed in to change notification settings - Fork 194
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
[WIP] Portname validation #1508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
#include <cstdlib> | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <regex> | ||
|
||
#if defined(YARP_HAS_ACE) | ||
# include <ace/INET_Addr.h> | ||
|
@@ -197,6 +198,87 @@ Contact Contact::fromString(const ConstString& txt) | |
return c; | ||
} | ||
|
||
bool Contact::isValidRegistrationName(const char* name) | ||
{ | ||
if(strcmp(name,"") == 0) // anonymous | ||
return true; | ||
|
||
const std::string port_name_s(name); | ||
std::regex port_exp("([a-zA-Z0-9]+(?:[_][a-zA-Z0-9]+)*)" // plain expression "abc[_def]" without leading '/', allowed for topics | ||
"|" // OR | ||
"(\\.\\.\\.)"); // "..." is accepted as registration name, a default one is assigned | ||
|
||
std::smatch port_match; | ||
if (std::regex_match(port_name_s, port_match, port_exp)) | ||
return true; | ||
|
||
return isValidRegisteredName(name); // otherwise, we need a good final name | ||
} | ||
|
||
bool Contact::isValidRegisteredName(const char* name) | ||
{ | ||
const std::string port_name_s(name); | ||
std::regex port_exp( | ||
"(?:([a-zA-Z0-9]+(?:[_][a-zA-Z0-9]+)*):/)?" // carrier_name:/ - optional | ||
"(" | ||
"(" // /port[category]@/node~wire syntax | ||
"((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)+)" // port_name - necessary | ||
"(?:" | ||
"([\\+|-][1]?)?" // category - optional, "+" | "-" | "+1" | "-1" | ||
"(@((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)*))" | ||
// @node_name - optional, could also be "" | ||
"(~([_a-zA-Z0-9]+))?" // ~wire_type - optional | ||
")?" | ||
")" | ||
"|" // OR | ||
"(" // /node[category]#/port~wire syntax | ||
"((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)+)" // node_name - necessary | ||
"([\\+|-][1]?)?" // category - optional, "+" | "-" | "+1" | "-1" | ||
"\x23" // # - necessary | ||
"((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)+)" // /port_name - necessary | ||
"(~([_a-zA-Z0-9]+))?" // ~wire_type - optional | ||
")" | ||
"|" // OR | ||
"(" // /node=[category]/port~wire syntax | ||
"((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)+)" // /node_name - necessary | ||
"=" // = - necessary | ||
"([\\+|-][1]?)?" // category - optional, "+" | "-" | "+1" | "-1" | ||
"((?:/[a-zA-Z0-9]+(?:[_:][a-zA-Z0-9]+)*)+)" // /port_name - necessary | ||
"(~([_a-zA-Z0-9]+))?" // ~wire_type - optional | ||
")" | ||
"|" // OR | ||
"(/" // "/machine:NNN/" syntax | ||
"[a-zA-Z0-9]+(?:\\.[a-zA-Z0-9]+)*" // machine name - "machine.domain.name" or IP addr | ||
":[\\d]+/?" // port number | ||
")" | ||
")"); | ||
|
||
// Matching | ||
std::smatch port_match; | ||
bool match = std::regex_match(port_name_s, port_match, port_exp); | ||
|
||
if (match) | ||
{ | ||
return true; | ||
} | ||
else // basic error reporting: | ||
{ | ||
std::cout << "\"" << port_name_s << "\" does not match the correct pattern for port definition.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is an error, perhaps it is better to use |
||
std::regex_search(port_name_s, port_match, port_exp); | ||
if (port_match[0].str() != "") | ||
{ | ||
std::cout << "Check for the error in prefix or suffix of the matched name:\n"; | ||
std::string prefix = (port_match.prefix().str().empty() ? "" : ("[" + port_match.prefix().str() + "] ")); | ||
std::string suffix = (port_match.suffix().str().empty() ? "" : (" [" + port_match.suffix().str() + "]")); | ||
std::cout << prefix << port_match[0].str() << suffix << std::endl; | ||
} | ||
else | ||
{ | ||
std::cout << "There is no matching pattern. Check the correct syntax --> where <-- for port names.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not fully understand this message. |
||
} | ||
return false; | ||
} | ||
} | ||
|
||
|
||
ConstString Contact::getName() const | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any idea about the order of magnitute necessary to build the
std::regex
object? If it is not trivial, we could think about having a regex singleton to avoid rebuilding the regex for each check.