ClickHouse/dbms/src/IO/ReadHelpers.cpp

114 lines
2.4 KiB
C++
Raw Normal View History

2010-06-04 18:25:25 +00:00
#include <DB/IO/ReadHelpers.h>
namespace DB
{
void assertString(const char * s, ReadBuffer & buf)
{
for (; *s; ++s)
{
if (buf.eof() || *buf.position() != *s)
throw Exception(String("Cannot parse input: expected ") + s, ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED);
++buf.position();
}
}
void readString(String & s, ReadBuffer & buf)
{
s = "";
while (!buf.eof())
{
size_t bytes = 0;
for (; buf.position() + bytes != buf.buffer().end(); ++bytes)
if (buf.position()[bytes] == '\t' || buf.position()[bytes] == '\n')
break;
s.append(buf.position(), bytes);
buf.position() += bytes;
if (buf.position() != buf.buffer().end())
return;
}
}
void readEscapedString(String & s, ReadBuffer & buf)
{
s = "";
while (!buf.eof())
{
size_t bytes = 0;
for (; buf.position() + bytes != buf.buffer().end(); ++bytes)
if (buf.position()[bytes] == '\\' || buf.position()[bytes] == '\t' || buf.position()[bytes] == '\n')
break;
s.append(buf.position(), bytes);
buf.position() += bytes;
if (*buf.position() == '\t' || *buf.position() == '\n')
return;
if (*buf.position() == '\\')
{
++buf.position();
if (buf.eof())
throw Exception("Cannot parse escape sequence", ErrorCodes::CANNOT_PARSE_ESCAPE_SEQUENCE);
s += parseEscapeSequence(*buf.position());
++buf.position();
}
}
}
2011-06-15 18:54:18 +00:00
template <char quote>
static void readAnyQuotedString(String & s, ReadBuffer & buf)
2010-06-04 18:25:25 +00:00
{
s = "";
2011-06-15 18:54:18 +00:00
if (buf.eof() || *buf.position() != quote)
throw Exception("Cannot parse quoted string: expected opening quote",
2010-06-04 18:25:25 +00:00
ErrorCodes::CANNOT_PARSE_QUOTED_STRING);
++buf.position();
while (!buf.eof())
{
size_t bytes = 0;
for (; buf.position() + bytes != buf.buffer().end(); ++bytes)
2011-06-15 18:54:18 +00:00
if (buf.position()[bytes] == '\\' || buf.position()[bytes] == quote)
2010-06-04 18:25:25 +00:00
break;
s.append(buf.position(), bytes);
buf.position() += bytes;
2011-06-15 18:54:18 +00:00
if (*buf.position() == quote)
2010-06-04 18:25:25 +00:00
{
++buf.position();
return;
}
if (*buf.position() == '\\')
{
++buf.position();
if (buf.eof())
throw Exception("Cannot parse escape sequence", ErrorCodes::CANNOT_PARSE_ESCAPE_SEQUENCE);
s += parseEscapeSequence(*buf.position());
++buf.position();
}
}
2011-06-15 18:54:18 +00:00
throw Exception("Cannot parse quoted string: expected closing quote",
2010-06-04 18:25:25 +00:00
ErrorCodes::CANNOT_PARSE_QUOTED_STRING);
}
2011-06-15 18:54:18 +00:00
void readQuotedString(String & s, ReadBuffer & buf)
{
readAnyQuotedString<'\''>(s, buf);
}
void readDoubleQuotedString(String & s, ReadBuffer & buf)
{
readAnyQuotedString<'"'>(s, buf);
}
2010-06-04 18:25:25 +00:00
}