2017-06-01 13:41:58 +00:00
|
|
|
#include <Common/hex.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/StringUtils.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
2016-12-12 01:51:27 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string escapeForFileName(const std::string & s)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string res;
|
|
|
|
const char * pos = s.data();
|
|
|
|
const char * end = pos + s.size();
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos != end)
|
|
|
|
{
|
2017-08-07 08:03:23 +00:00
|
|
|
unsigned char c = *pos;
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (isWordCharASCII(c))
|
|
|
|
res += c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += '%';
|
2017-07-23 06:53:28 +00:00
|
|
|
res += hexDigitUppercase(c / 16);
|
|
|
|
res += hexDigitUppercase(c % 16);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++pos;
|
|
|
|
}
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-12-12 01:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string unescapeForFileName(const std::string & s)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string res;
|
|
|
|
const char * pos = s.data();
|
|
|
|
const char * end = pos + s.size();
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos != end)
|
|
|
|
{
|
|
|
|
if (*pos != '%')
|
|
|
|
res += *pos;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// skip '%'
|
|
|
|
if (++pos == end) break;
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char val = unhex(*pos) * 16;
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (++pos == end) break;
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
val += unhex(*pos);
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res += val;
|
|
|
|
}
|
2016-12-12 01:51:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
return res;
|
2016-12-12 01:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|