Files
spark/src/lib/config/PreprocessInputStream.cpp
T

67 lines
1.8 KiB
C++

#include "PreprocessInputStream.h"
#include <boost/algorithm/string.hpp>
#include <string>
using namespace std;
namespace bts {
namespace config {
void replace_all(string &line, const string &before, const string &after) {
string::const_iterator end = line.end();
string::const_iterator current = line.begin();
string::const_iterator next =
search(current, end, before.begin(), before.end());
if (next != end) {
std::string result;
while (next != end) {
result.append(current, next);
result.append(after);
current = next + before.size();
next = search(current, end, before.begin(), before.end());
}
result.append(current, next);
line = result;
}
}
PreprocessInputStream::PreprocessInputStream(
std::istream &input, map<string, string> replacements)
: _buffer(), _stream() {
char temp[4096];
while (input.read(temp, sizeof(temp))) {
_buffer.append(temp, sizeof(temp));
}
_buffer.append(temp, input.gcount());
for (map<string, string>::iterator iter = replacements.begin();
iter != replacements.end(); iter++) {
const string &key = iter->first;
const string &value = iter->second;
replace_all(_buffer, key, value);
}
_stream.reset(
new proto::io::ArrayInputStream(_buffer.c_str(), _buffer.size()));
}
PreprocessInputStream::~PreprocessInputStream() {}
bool PreprocessInputStream::Next(const void **data, int *size) {
return _stream->Next(data, size);
}
void PreprocessInputStream::BackUp(int count) { _stream->BackUp(count); }
bool PreprocessInputStream::Skip(int count) { return _stream->Skip(count); }
proto::int64 PreprocessInputStream::ByteCount() const {
return _stream->ByteCount();
}
} // namespace config
} // namespace bts