2014-06-23 18:50:03 -07:00
|
|
|
#include "runtime/helpers/spy_reader.h"
|
2014-03-10 13:25:31 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
static const char * spy_read(void *data, size_t *bytes_read) {
|
|
|
|
|
SpyReader *reader = static_cast<SpyReader *>(data);
|
|
|
|
|
size_t size = std::min(reader->chunk_size,
|
|
|
|
|
reader->content.length() - reader->position);
|
|
|
|
|
const char *result = reader->content.data() + reader->position;
|
2014-03-18 13:23:00 -07:00
|
|
|
reader->strings_read.back() += string(result, size);
|
2014-03-10 13:25:31 -07:00
|
|
|
reader->position += size;
|
|
|
|
|
*bytes_read = size;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int spy_seek(void *data, size_t position) {
|
2014-03-11 08:30:19 -07:00
|
|
|
SpyReader *reader = static_cast<SpyReader *>(data);
|
2014-03-18 13:23:00 -07:00
|
|
|
reader->strings_read.push_back("");
|
2014-03-10 13:25:31 -07:00
|
|
|
reader->position = position;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SpyReader::SpyReader(string content, size_t chunk_size) :
|
|
|
|
|
content(content),
|
|
|
|
|
position(0),
|
|
|
|
|
chunk_size(chunk_size),
|
|
|
|
|
input({
|
2014-04-08 20:34:29 -07:00
|
|
|
this,
|
|
|
|
|
spy_read,
|
|
|
|
|
spy_seek,
|
2014-06-09 13:02:39 -07:00
|
|
|
nullptr,
|
2014-03-10 13:25:31 -07:00
|
|
|
}) {}
|