Move quote triming function to common place

Function for removing MySQL quote characters surrounding a
string moved from Cache's rules.cc to common place and renamed.

The function was now moved to mysql_utils.[h|c], which is in
maxscale-common, and not to the perhaps more appropriate place
protocol/mysql.h, as the latter would have implied that MySQLCommon
would have to be linked to, which actually might be the right thing
to do. However, that has to wait until protocol/mysql.h gets an
overhaul.
This commit is contained in:
Johan Wikman
2016-12-29 09:44:38 +02:00
parent b40872e600
commit c6e155cf2b
3 changed files with 85 additions and 82 deletions

View File

@ -168,3 +168,70 @@ MYSQL *mxs_mysql_real_connect(MYSQL *con, SERVER *server, const char *user, cons
return mysql_real_connect(con, server->name, user, passwd, NULL, server->port, NULL, 0);
}
bool mxs_mysql_trim_quotes(char *s)
{
bool dequoted = true;
char *i = s;
char *end = s + strlen(s);
// Remove space from the beginning
while (*i && isspace(*i))
{
++i;
}
if (*i)
{
// Remove space from the end
while (isspace(*(end - 1)))
{
*(end - 1) = 0;
--end;
}
ss_dassert(end > i);
char quote;
switch (*i)
{
case '\'':
case '"':
case '`':
quote = *i;
++i;
break;
default:
quote = 0;
}
if (quote)
{
--end;
if (*end == quote)
{
*end = 0;
memmove(s, i, end - i + 1);
}
else
{
dequoted = false;
}
}
else if (i != s)
{
memmove(s, i, end - i + 1);
}
}
else
{
*s = 0;
}
return dequoted;
}