mirror of
				https://github.com/open-source-parsers/jsoncpp.git
				synced 2025-10-29 20:59:44 +01:00 
			
		
		
		
	Merge pull request #125 from cdunn2001/issue-116
1.2.1 Fix issue #116, DOS line-endings. Never output \r.
This commit is contained in:
		| @@ -4,10 +4,10 @@ | ||||
| #ifndef JSON_VERSION_H_INCLUDED | ||||
| # define JSON_VERSION_H_INCLUDED | ||||
|  | ||||
| # define JSONCPP_VERSION_STRING "1.2.0" | ||||
| # define JSONCPP_VERSION_STRING "1.2.1" | ||||
| # define JSONCPP_VERSION_MAJOR 1 | ||||
| # define JSONCPP_VERSION_MINOR 2 | ||||
| # define JSONCPP_VERSION_PATCH 0 | ||||
| # define JSONCPP_VERSION_PATCH 1 | ||||
| # define JSONCPP_VERSION_QUALIFIER | ||||
| # define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8)) | ||||
|  | ||||
|   | ||||
| @@ -135,14 +135,9 @@ bool Reader::readValue() { | ||||
|   bool successful = true; | ||||
|  | ||||
|   if (collectComments_ && !commentsBefore_.empty()) { | ||||
|     // Remove newline characters at the end of the comments | ||||
|     size_t lastNonNewline = commentsBefore_.find_last_not_of("\r\n"); | ||||
|     if (lastNonNewline != std::string::npos) { | ||||
|       commentsBefore_.erase(lastNonNewline + 1); | ||||
|     } else { | ||||
|       commentsBefore_.clear(); | ||||
|     } | ||||
|  | ||||
|     // Remove newline at the end of the comment | ||||
|     if (commentsBefore_[commentsBefore_.size() - 1] == '\n') | ||||
|       commentsBefore_.resize(commentsBefore_.size() - 1); | ||||
|     currentValue().setComment(commentsBefore_, commentBefore); | ||||
|     commentsBefore_ = ""; | ||||
|   } | ||||
| @@ -337,14 +332,34 @@ bool Reader::readComment() { | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| static std::string normalizeEOL(Reader::Location begin, Reader::Location end) { | ||||
|   std::string normalized; | ||||
|   normalized.reserve(end - begin); | ||||
|   Reader::Location current = begin; | ||||
|   while (current != end) { | ||||
|     char c = *current++; | ||||
|     if (c == '\r') { | ||||
|       if (current != end && *current == '\n') | ||||
|          // convert dos EOL | ||||
|          ++current; | ||||
|       // convert Mac EOL | ||||
|       normalized += '\n'; | ||||
|     } else { | ||||
|       normalized += c; | ||||
|     } | ||||
|   } | ||||
|   return normalized; | ||||
| } | ||||
|  | ||||
| void | ||||
| Reader::addComment(Location begin, Location end, CommentPlacement placement) { | ||||
|   assert(collectComments_); | ||||
|   const std::string& normalized = normalizeEOL(begin, end); | ||||
|   if (placement == commentAfterOnSameLine) { | ||||
|     assert(lastValue_ != 0); | ||||
|     lastValue_->setComment(std::string(begin, end), placement); | ||||
|     lastValue_->setComment(normalized, placement); | ||||
|   } else { | ||||
|     commentsBefore_ += std::string(begin, end); | ||||
|     commentsBefore_ += normalized; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @@ -360,8 +375,15 @@ bool Reader::readCStyleComment() { | ||||
| bool Reader::readCppStyleComment() { | ||||
|   while (current_ != end_) { | ||||
|     Char c = getNextChar(); | ||||
|     if (c == '\r' || c == '\n') | ||||
|     if (c == '\n') | ||||
|       break; | ||||
|     if (c == '\r') { | ||||
|       // Consume DOS EOL. It will be normalized in addComment. | ||||
|       if (current_ != end_ && *current_ == '\n') | ||||
|         getNextChar(); | ||||
|       // Break on Moc OS 9 EOL. | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
|   | ||||
| @@ -421,26 +421,27 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) { | ||||
|  | ||||
|   document_ += "\n"; | ||||
|   writeIndent(); | ||||
|   std::string normalizedComment = normalizeEOL(root.getComment(commentBefore)); | ||||
|   std::string::const_iterator iter = normalizedComment.begin(); | ||||
|   while (iter != normalizedComment.end()) { | ||||
|   const std::string& comment = root.getComment(commentBefore); | ||||
|   std::string::const_iterator iter = comment.begin(); | ||||
|   while (iter != comment.end()) { | ||||
|     document_ += *iter; | ||||
|     if (*iter == '\n' && *(iter + 1) == '/') | ||||
|     if (*iter == '\n' && | ||||
|        (iter != comment.end() && *(iter + 1) == '/')) | ||||
|       writeIndent(); | ||||
|     ++iter; | ||||
|   } | ||||
|  | ||||
|   // Comments are stripped of newlines, so add one here | ||||
|   // Comments are stripped of trailing newlines, so add one here | ||||
|   document_ += "\n"; | ||||
| } | ||||
|  | ||||
| void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) { | ||||
|   if (root.hasComment(commentAfterOnSameLine)) | ||||
|     document_ += " " + normalizeEOL(root.getComment(commentAfterOnSameLine)); | ||||
|     document_ += " " + root.getComment(commentAfterOnSameLine); | ||||
|  | ||||
|   if (root.hasComment(commentAfter)) { | ||||
|     document_ += "\n"; | ||||
|     document_ += normalizeEOL(root.getComment(commentAfter)); | ||||
|     document_ += root.getComment(commentAfter); | ||||
|     document_ += "\n"; | ||||
|   } | ||||
| } | ||||
| @@ -451,25 +452,6 @@ bool StyledWriter::hasCommentForValue(const Value& value) { | ||||
|          value.hasComment(commentAfter); | ||||
| } | ||||
|  | ||||
| std::string StyledWriter::normalizeEOL(const std::string& text) { | ||||
|   std::string normalized; | ||||
|   normalized.reserve(text.length()); | ||||
|   const char* begin = text.c_str(); | ||||
|   const char* end = begin + text.length(); | ||||
|   const char* current = begin; | ||||
|   while (current != end) { | ||||
|     char c = *current++; | ||||
|     if (c == '\r') // mac or dos EOL | ||||
|     { | ||||
|       if (*current == '\n') // convert dos EOL | ||||
|         ++current; | ||||
|       normalized += '\n'; | ||||
|     } else // handle unix EOL & other char | ||||
|       normalized += c; | ||||
|   } | ||||
|   return normalized; | ||||
| } | ||||
|  | ||||
| // Class StyledStreamWriter | ||||
| // ////////////////////////////////////////////////////////////////// | ||||
|  | ||||
| @@ -646,17 +628,17 @@ void StyledStreamWriter::unindent() { | ||||
| void StyledStreamWriter::writeCommentBeforeValue(const Value& root) { | ||||
|   if (!root.hasComment(commentBefore)) | ||||
|     return; | ||||
|   *document_ << normalizeEOL(root.getComment(commentBefore)); | ||||
|   *document_ << root.getComment(commentBefore); | ||||
|   *document_ << "\n"; | ||||
| } | ||||
|  | ||||
| void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) { | ||||
|   if (root.hasComment(commentAfterOnSameLine)) | ||||
|     *document_ << " " + normalizeEOL(root.getComment(commentAfterOnSameLine)); | ||||
|     *document_ << " " + root.getComment(commentAfterOnSameLine); | ||||
|  | ||||
|   if (root.hasComment(commentAfter)) { | ||||
|     *document_ << "\n"; | ||||
|     *document_ << normalizeEOL(root.getComment(commentAfter)); | ||||
|     *document_ << root.getComment(commentAfter); | ||||
|     *document_ << "\n"; | ||||
|   } | ||||
| } | ||||
| @@ -667,25 +649,6 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) { | ||||
|          value.hasComment(commentAfter); | ||||
| } | ||||
|  | ||||
| std::string StyledStreamWriter::normalizeEOL(const std::string& text) { | ||||
|   std::string normalized; | ||||
|   normalized.reserve(text.length()); | ||||
|   const char* begin = text.c_str(); | ||||
|   const char* end = begin + text.length(); | ||||
|   const char* current = begin; | ||||
|   while (current != end) { | ||||
|     char c = *current++; | ||||
|     if (c == '\r') // mac or dos EOL | ||||
|     { | ||||
|       if (*current == '\n') // convert dos EOL | ||||
|         ++current; | ||||
|       normalized += '\n'; | ||||
|     } else // handle unix EOL & other char | ||||
|       normalized += c; | ||||
|   } | ||||
|   return normalized; | ||||
| } | ||||
|  | ||||
| std::ostream& operator<<(std::ostream& sout, const Value& root) { | ||||
|   Json::StyledStreamWriter writer; | ||||
|   writer.write(sout, root); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Christopher Dunn
					Christopher Dunn