2011-11-01 17:57:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-19 01:54:23 +00:00
|
|
|
#include <string>
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2011-11-03 20:30:12 +00:00
|
|
|
#define WRITE_BUFFER_FROM_STRING_INITIAL_SIZE_IF_EMPTY 32
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
|
2011-11-01 17:57:37 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Writes the data to a string.
|
|
|
|
* Note: before using the resulting string, destroy this object.
|
2011-11-01 17:57:37 +00:00
|
|
|
*/
|
|
|
|
class WriteBufferFromString : public WriteBuffer
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string & s;
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override
|
|
|
|
{
|
|
|
|
size_t old_size = s.size();
|
|
|
|
s.resize(old_size * 2);
|
|
|
|
internal_buffer = Buffer(reinterpret_cast<Position>(&s[old_size]), reinterpret_cast<Position>(&*s.end()));
|
|
|
|
working_buffer = internal_buffer;
|
|
|
|
}
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
void finish()
|
|
|
|
{
|
|
|
|
s.resize(count());
|
|
|
|
}
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2011-11-01 17:57:37 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferFromString(std::string & s_)
|
|
|
|
: WriteBuffer(reinterpret_cast<Position>(&s_[0]), s_.size()), s(s_)
|
|
|
|
{
|
|
|
|
if (s.empty())
|
|
|
|
{
|
|
|
|
s.resize(WRITE_BUFFER_FROM_STRING_INITIAL_SIZE_IF_EMPTY);
|
|
|
|
set(reinterpret_cast<Position>(&s[0]), s.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~WriteBufferFromString() override
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
2011-11-01 17:57:37 +00:00
|
|
|
};
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
|
2017-08-01 13:31:38 +00:00
|
|
|
class StringHolder {
|
|
|
|
protected:
|
|
|
|
std::string ss;
|
|
|
|
};
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
/// Creates the string by itself and allows to get it.
|
2017-08-01 13:31:38 +00:00
|
|
|
class WriteBufferFromOwnString : public StringHolder, public WriteBufferFromString
|
2017-07-31 21:39:24 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2017-08-01 13:31:38 +00:00
|
|
|
WriteBufferFromOwnString() : WriteBufferFromString(ss) {}
|
2017-07-31 21:39:24 +00:00
|
|
|
|
|
|
|
std::string & str()
|
|
|
|
{
|
|
|
|
finish();
|
2017-08-01 13:31:38 +00:00
|
|
|
return ss;
|
2017-07-31 21:39:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-01 17:57:37 +00:00
|
|
|
}
|