mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-06-07 17:14:56 +02:00
copy fixes from StyledStreamWriter
This commit is contained in:
parent
9e4bcf354f
commit
94665eab72
@ -665,6 +665,9 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
|
|||||||
value.hasComment(commentAfter);
|
value.hasComment(commentAfter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
// BuiltStyledStreamWriter
|
||||||
|
|
||||||
struct BuiltStyledStreamWriter : public StreamWriter
|
struct BuiltStyledStreamWriter : public StreamWriter
|
||||||
{
|
{
|
||||||
BuiltStyledStreamWriter(
|
BuiltStyledStreamWriter(
|
||||||
@ -683,8 +686,7 @@ private:
|
|||||||
void unindent();
|
void unindent();
|
||||||
void writeCommentBeforeValue(Value const& root);
|
void writeCommentBeforeValue(Value const& root);
|
||||||
void writeCommentAfterValueOnSameLine(Value const& root);
|
void writeCommentAfterValueOnSameLine(Value const& root);
|
||||||
bool hasCommentForValue(const Value& value);
|
static bool hasCommentForValue(const Value& value);
|
||||||
static std::string normalizeEOL(std::string const& text);
|
|
||||||
|
|
||||||
typedef std::vector<std::string> ChildValues;
|
typedef std::vector<std::string> ChildValues;
|
||||||
|
|
||||||
@ -693,7 +695,8 @@ private:
|
|||||||
int rightMargin_;
|
int rightMargin_;
|
||||||
std::string indentation_;
|
std::string indentation_;
|
||||||
CommentStyle cs_;
|
CommentStyle cs_;
|
||||||
bool addChildValues_;
|
bool addChildValues_ : 1;
|
||||||
|
bool indented_ : 1;
|
||||||
};
|
};
|
||||||
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
||||||
std::ostream* sout,
|
std::ostream* sout,
|
||||||
@ -703,11 +706,14 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
|||||||
, rightMargin_(74)
|
, rightMargin_(74)
|
||||||
, indentation_(indentation)
|
, indentation_(indentation)
|
||||||
, cs_(cs)
|
, cs_(cs)
|
||||||
|
, addChildValues_(false)
|
||||||
|
, indented_(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
int BuiltStyledStreamWriter::write(Value const& root)
|
int BuiltStyledStreamWriter::write(Value const& root)
|
||||||
{
|
{
|
||||||
addChildValues_ = false;
|
addChildValues_ = false;
|
||||||
|
indented_ = false;
|
||||||
indentString_ = "";
|
indentString_ = "";
|
||||||
writeCommentBeforeValue(root);
|
writeCommentBeforeValue(root);
|
||||||
writeValue(root);
|
writeValue(root);
|
||||||
@ -784,8 +790,10 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
|
|||||||
if (hasChildValue)
|
if (hasChildValue)
|
||||||
writeWithIndent(childValues_[index]);
|
writeWithIndent(childValues_[index]);
|
||||||
else {
|
else {
|
||||||
writeIndent();
|
if (!indented_) writeIndent();
|
||||||
|
indented_ = true;
|
||||||
writeValue(childValue);
|
writeValue(childValue);
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
if (++index == size) {
|
if (++index == size) {
|
||||||
writeCommentAfterValueOnSameLine(childValue);
|
writeCommentAfterValueOnSameLine(childValue);
|
||||||
@ -826,6 +834,9 @@ bool BuiltStyledStreamWriter::isMultineArray(Value const& value) {
|
|||||||
addChildValues_ = true;
|
addChildValues_ = true;
|
||||||
int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
|
int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
|
||||||
for (int index = 0; index < size; ++index) {
|
for (int index = 0; index < size; ++index) {
|
||||||
|
if (hasCommentForValue(value[index])) {
|
||||||
|
isMultiLine = true;
|
||||||
|
}
|
||||||
writeValue(value[index]);
|
writeValue(value[index]);
|
||||||
lineLength += int(childValues_[index].length());
|
lineLength += int(childValues_[index].length());
|
||||||
}
|
}
|
||||||
@ -843,24 +854,17 @@ void BuiltStyledStreamWriter::pushValue(std::string const& value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BuiltStyledStreamWriter::writeIndent() {
|
void BuiltStyledStreamWriter::writeIndent() {
|
||||||
/*
|
// blep intended this to look at the so-far-written string
|
||||||
Some comments in this method would have been nice. ;-)
|
// to determine whether we are already indented, but
|
||||||
|
// with a stream we cannot do that. So we rely on some saved state.
|
||||||
if ( !sout_.empty() )
|
// The caller checks indented_.
|
||||||
{
|
|
||||||
char last = sout_[sout_.length()-1];
|
|
||||||
if ( last == ' ' ) // already indented
|
|
||||||
return;
|
|
||||||
if ( last != '\n' ) // Comments may add new-line
|
|
||||||
sout_ << '\n';
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
sout_ << '\n' << indentString_;
|
sout_ << '\n' << indentString_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) {
|
void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) {
|
||||||
writeIndent();
|
if (!indented_) writeIndent();
|
||||||
sout_ << value;
|
sout_ << value;
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
|
void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
|
||||||
@ -873,8 +877,22 @@ void BuiltStyledStreamWriter::unindent() {
|
|||||||
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
||||||
if (!root.hasComment(commentBefore))
|
if (!root.hasComment(commentBefore))
|
||||||
return;
|
return;
|
||||||
sout_ << root.getComment(commentBefore);
|
|
||||||
sout_ << "\n";
|
sout_ << "\n";
|
||||||
|
writeIndent();
|
||||||
|
const std::string& comment = root.getComment(commentBefore);
|
||||||
|
std::string::const_iterator iter = comment.begin();
|
||||||
|
while (iter != comment.end()) {
|
||||||
|
sout_ << *iter;
|
||||||
|
if (*iter == '\n' &&
|
||||||
|
(iter != comment.end() && *(iter + 1) == '/'))
|
||||||
|
writeIndent();
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comments are stripped of trailing newlines, so add one here
|
||||||
|
sout_ << "\n";
|
||||||
|
indented_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
|
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
|
||||||
@ -888,12 +906,16 @@ void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
|
bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
|
||||||
return value.hasComment(commentBefore) ||
|
return value.hasComment(commentBefore) ||
|
||||||
value.hasComment(commentAfterOnSameLine) ||
|
value.hasComment(commentAfterOnSameLine) ||
|
||||||
value.hasComment(commentAfter);
|
value.hasComment(commentAfter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
// StreamWriter
|
||||||
|
|
||||||
StreamWriter::StreamWriter(std::ostream* sout)
|
StreamWriter::StreamWriter(std::ostream* sout)
|
||||||
: sout_(*sout)
|
: sout_(*sout)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user