|
|
@@ -6,31 +6,31 @@ |
|
|
|
#include <errno.h> |
|
|
|
|
|
|
|
int BXParser::get() { |
|
|
|
if (dataidx_ < datalen_) { |
|
|
|
return buf_[dataidx_++]; |
|
|
|
if (bufidx_ < buflen_) { |
|
|
|
return buf_[bufidx_++]; |
|
|
|
} |
|
|
|
|
|
|
|
dataidx_ = 0; |
|
|
|
bufidx_ = 0; |
|
|
|
stream_.read(buf_, sizeof(buf_)); |
|
|
|
datalen_ = stream_.gcount(); |
|
|
|
if (datalen_ == 0) { |
|
|
|
buflen_ = stream_.gcount(); |
|
|
|
if (buflen_ == 0) { |
|
|
|
return EOF; |
|
|
|
} |
|
|
|
|
|
|
|
return buf_[dataidx_++]; |
|
|
|
return buf_[bufidx_++]; |
|
|
|
} |
|
|
|
|
|
|
|
int BXParser::peek() { |
|
|
|
if (dataidx_ < datalen_) { |
|
|
|
return buf_[dataidx_]; |
|
|
|
if (bufidx_ < buflen_) { |
|
|
|
return buf_[bufidx_]; |
|
|
|
} else { |
|
|
|
return stream_.peek(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int BXParser::peek2() { |
|
|
|
if (dataidx_ + 1 < datalen_) { |
|
|
|
return buf_[dataidx_ + 1]; |
|
|
|
if (bufidx_ + 1 < buflen_) { |
|
|
|
return buf_[bufidx_ + 1]; |
|
|
|
} else { |
|
|
|
stream_.get(); |
|
|
|
int ch = stream_.peek(); |
|
|
@@ -380,20 +380,35 @@ void BXParser::parseList(const BXVariables &vars, std::vector<std::string> &valu |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
BXWriter::~BXWriter() { |
|
|
|
if (bufidx_ > 0) { |
|
|
|
stream_.write(buf_, bufidx_); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void BXWriter::put(char ch) { |
|
|
|
ch_ += 1; |
|
|
|
stream_ << ch; |
|
|
|
buf_[bufidx_++] = ch; |
|
|
|
|
|
|
|
if (bufidx_ == sizeof(buf_)) { |
|
|
|
stream_.write(buf_, sizeof(buf_)); |
|
|
|
bufidx_ = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void BXWriter::put(const std::string &str) { |
|
|
|
ch_ += str.size(); |
|
|
|
stream_ << str; |
|
|
|
} |
|
|
|
size_t w = sizeof(buf_) - bufidx_; |
|
|
|
if (w > str.size()) { |
|
|
|
w = str.size(); |
|
|
|
} |
|
|
|
|
|
|
|
void BXWriter::newline() { |
|
|
|
ch_ = 1; |
|
|
|
line_ += 1; |
|
|
|
stream_ << '\n'; |
|
|
|
memcpy(buf_ + bufidx_, str.c_str(), w); |
|
|
|
if (str.size() - w > 0) { |
|
|
|
stream_.write(buf_, bufidx_ + w); |
|
|
|
stream_.write(str.c_str() + w, str.size() - w); |
|
|
|
bufidx_ = 0; |
|
|
|
} else { |
|
|
|
bufidx_ += w; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void BXWriter::escape(const std::string &str) { |
|
|
@@ -409,18 +424,21 @@ void BXWriter::escape(const std::string &str) { |
|
|
|
|
|
|
|
void BXWriter::write(const BXVariables &vars) { |
|
|
|
for (const auto &pair: vars) { |
|
|
|
size_t chars = 0; |
|
|
|
put(pair.first); |
|
|
|
put(" :="); |
|
|
|
for (auto &val: pair.second) { |
|
|
|
if (ch_ >= 80) { |
|
|
|
newline(); |
|
|
|
if (chars >= 80) { |
|
|
|
put('\n'); |
|
|
|
put('\t'); |
|
|
|
chars = 0; |
|
|
|
} else { |
|
|
|
put(' '); |
|
|
|
} |
|
|
|
|
|
|
|
escape(val); |
|
|
|
chars += val.size(); |
|
|
|
} |
|
|
|
newline(); |
|
|
|
put('\n'); |
|
|
|
} |
|
|
|
} |