From: Baptiste L. <bl...@us...> - 2005-07-30 17:41:14
|
Update of /cvsroot/jsoncpp/jsoncpp/src/lib_json In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8466/src/lib_json Modified Files: json_reader.cpp json_value.cpp json_writer.cpp Log Message: * added more doxygen documentation * added support for comment in StyledWriter Index: json_value.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_value.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** json_value.cpp 30 Jul 2005 11:15:11 -0000 1.4 --- json_value.cpp 30 Jul 2005 17:40:59 -0000 1.5 *************** *** 13,16 **** --- 13,39 ---- const Value::UInt Value::maxUInt = Value::UInt(-1); + + Value::CommentInfo::CommentInfo() + : comment_( 0 ) + { + } + + Value::CommentInfo::~CommentInfo() + { + if ( comment_ ) + free( comment_ ); + } + + + void + Value::CommentInfo::setComment( const char *text ) + { + if ( comment_ ) + free( comment_ ); + comment_ = strdup( text ); + } + + + // Notes: index_ indicates if the string was allocated when // a string is stored. *************** *** 92,96 **** Value::Value( ValueType type ) : type_( type ) ! , id_( 0 ) { switch ( type ) --- 115,119 ---- Value::Value( ValueType type ) : type_( type ) ! , comments_( 0 ) { switch ( type ) *************** *** 123,127 **** Value::Value( Int value ) : type_( intValue ) ! , id_( 0 ) { value_.int_ = value; --- 146,150 ---- Value::Value( Int value ) : type_( intValue ) ! , comments_( 0 ) { value_.int_ = value; *************** *** 131,135 **** Value::Value( UInt value ) : type_( uintValue ) ! , id_( 0 ) { value_.uint_ = value; --- 154,158 ---- Value::Value( UInt value ) : type_( uintValue ) ! , comments_( 0 ) { value_.uint_ = value; *************** *** 138,142 **** Value::Value( double value ) : type_( realValue ) ! , id_( 0 ) { value_.real_ = value; --- 161,165 ---- Value::Value( double value ) : type_( realValue ) ! , comments_( 0 ) { value_.real_ = value; *************** *** 145,149 **** Value::Value( const char *value ) : type_( stringValue ) ! , id_( 0 ) { value_.string_ = value ? strdup( value ) : 0; --- 168,172 ---- Value::Value( const char *value ) : type_( stringValue ) ! , comments_( 0 ) { value_.string_ = value ? strdup( value ) : 0; *************** *** 152,156 **** Value::Value( const std::string &value ) : type_( stringValue ) ! , id_( 0 ) { value_.string_ = value.empty() ? 0 : strdup( value.c_str() ); --- 175,179 ---- Value::Value( const std::string &value ) : type_( stringValue ) ! , comments_( 0 ) { value_.string_ = value.empty() ? 0 : strdup( value.c_str() ); *************** *** 159,163 **** Value::Value( bool value ) : type_( booleanValue ) ! , id_( 0 ) { value_.bool_ = value; --- 182,186 ---- Value::Value( bool value ) : type_( booleanValue ) ! , comments_( 0 ) { value_.bool_ = value; *************** *** 167,171 **** Value::Value( const Value &other ) : type_( other.type_ ) ! , id_( 0 ) { switch ( type_ ) --- 190,194 ---- Value::Value( const Value &other ) : type_( other.type_ ) ! , comments_( 0 ) { switch ( type_ ) *************** *** 189,192 **** --- 212,225 ---- JSON_ASSERT_UNREACHABLE; } + if ( other.comments_ ) + { + comments_ = new CommentInfo[numberOfCommentPlacement]; + for ( int comment =0; comment < numberOfCommentPlacement; ++comment ) + { + const CommentInfo &otherComment = other.comments_[comment]; + if ( otherComment.comment_ ) + comments_[comment].setComment( otherComment.comment_ ); + } + } } *************** *** 212,215 **** --- 245,251 ---- JSON_ASSERT_UNREACHABLE; } + + if ( comments_ ) + delete[] comments_; } *************** *** 767,780 **** void ! Value::setId( ValueId id ) { ! id_ = id; } ! ValueId ! Value::id() const { ! return id_; } --- 803,835 ---- void ! Value::setComment( const char *comment, ! CommentPlacement placement ) { ! if ( !comments_ ) ! comments_ = new CommentInfo[numberOfCommentPlacement]; ! comments_[placement].setComment( comment ); } ! void ! Value::setComment( const std::string &comment, ! CommentPlacement placement ) { ! setComment( comment.c_str(), placement ); ! } ! ! ! bool ! Value::hasComment( CommentPlacement placement ) const ! { ! return comments_ != 0 && comments_[placement].comment_ != 0; ! } ! ! std::string ! Value::getComment( CommentPlacement placement ) const ! { ! if ( hasComment(placement) ) ! return comments_[placement].comment_; ! return ""; } Index: json_reader.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_reader.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** json_reader.cpp 30 Jul 2005 11:15:11 -0000 1.2 --- json_reader.cpp 30 Jul 2005 17:40:59 -0000 1.3 *************** *** 3,6 **** --- 3,7 ---- #include <utility> #include <stdio.h> + #include <assert.h> namespace Json { *************** *** 19,27 **** // Class Reader // ////////////////////////////////////////////////////////////////// Reader::Reader() - : commentMemo_( 0 ) { } --- 20,38 ---- + static bool + containsNewLine( Reader::Location begin, + Reader::Location end ) + { + for ( ;begin < end; ++begin ) + if ( *begin == '\n' || *begin == '\r' ) + return true; + return false; + } + + // Class Reader // ////////////////////////////////////////////////////////////////// Reader::Reader() { } *************** *** 29,57 **** bool Reader::parse( const std::string &document, ! Value &root ) { document_ = document; begin_ = document_.c_str(); end_ = begin_ + document_.length(); current_ = begin_; errors_.clear(); while ( !nodes_.empty() ) nodes_.pop(); nodes_.push( &root ); ! return readValue(); ! } ! ! ! bool ! Reader::parse( const std::string &document, ! Value &root, ! CommentMemo &commentMemo ) ! { ! commentMemo_ = &commentMemo; ! commentMemo.beginDocumentParsing(); ! bool success = parse( document, root ); ! commentMemo.endDocumentParsing(); ! commentMemo_ = 0; ! return success; } --- 40,65 ---- bool Reader::parse( const std::string &document, ! Value &root, ! bool collectComments ) { + collectComments_ = collectComments; document_ = document; begin_ = document_.c_str(); end_ = begin_ + document_.length(); current_ = begin_; + lastValueEnd_ = 0; + lastValue_ = 0; + commentsBefore_.clear(); errors_.clear(); while ( !nodes_.empty() ) nodes_.pop(); nodes_.push( &root ); ! ! bool successful = readValue(); ! Token token; ! skipCommentTokens( token ); ! if ( collectComments_ && !commentsBefore_.empty() ) ! root.setComment( commentsBefore_, commentAfter ); ! return successful; } *************** *** 61,80 **** { Token token; ! do { ! readToken( token ); } ! while ( token.type_ == tokenComment ); switch ( token.type_ ) { case tokenObjectBegin: ! return readObject( token ); case tokenArrayBegin: ! return readArray( token ); case tokenNumber: ! return decodeNumber( token ); case tokenString: ! return decodeString( token ); case tokenTrue: currentValue() = true; --- 69,96 ---- { Token token; ! skipCommentTokens( token ); ! bool successful = true; ! ! if ( collectComments_ && !commentsBefore_.empty() ) { ! currentValue().setComment( commentsBefore_, commentBefore ); ! commentsBefore_.clear(); } ! switch ( token.type_ ) { case tokenObjectBegin: ! successful = readObject( token ); ! break; case tokenArrayBegin: ! successful = readArray( token ); ! break; case tokenNumber: ! successful = decodeNumber( token ); ! break; case tokenString: ! successful = decodeString( token ); ! break; case tokenTrue: currentValue() = true; *************** *** 89,93 **** return addError( "Syntax error: value, object or array expected.", token ); } ! return true; } --- 105,127 ---- return addError( "Syntax error: value, object or array expected.", token ); } ! ! if ( collectComments_ ) ! { ! lastValueEnd_ = current_; ! lastValue_ = ¤tValue(); ! } ! ! return successful; ! } ! ! ! void ! Reader::skipCommentTokens( Token &token ) ! { ! do ! { ! readToken( token ); ! } ! while ( token.type_ == tokenComment ); } *************** *** 210,219 **** Reader::readComment() { Char c = getNextChar(); if ( c == '*' ) ! return readCStyleComment(); else if ( c == '/' ) ! return readCppStyleComment(); ! return false; } --- 244,289 ---- Reader::readComment() { + Location commentBegin = current_ - 1; Char c = getNextChar(); + bool successful = false; if ( c == '*' ) ! successful = readCStyleComment(); else if ( c == '/' ) ! successful = readCppStyleComment(); ! if ( !successful ) ! return false; ! ! if ( collectComments_ ) ! { ! CommentPlacement placement = commentBefore; ! if ( lastValueEnd_ && !containsNewLine( lastValueEnd_, commentBegin ) ) ! { ! if ( c != '*' || !containsNewLine( commentBegin, current_ ) ) ! placement = commentAfterOnSameLine; ! } ! ! addComment( commentBegin, current_, placement ); ! } ! return true; ! } ! ! ! void ! Reader::addComment( Location begin, ! Location end, ! CommentPlacement placement ) ! { ! assert( collectComments_ ); ! if ( placement == commentAfterOnSameLine ) ! { ! assert( lastValue_ != 0 ); ! lastValue_->setComment( std::string( begin, end ), placement ); ! } ! else ! { ! if ( !commentsBefore_.empty() ) ! commentsBefore_ += "\n"; ! commentsBefore_ += std::string( begin, end ); ! } } *************** *** 281,284 **** --- 351,359 ---- while ( readToken( tokenName ) ) { + bool initialTokenOk = true; + while ( tokenName.type_ == tokenComment && initialTokenOk ) + initialTokenOk = readToken( tokenName ); + if ( !initialTokenOk ) + break; if ( tokenName.type_ == tokenObjectEnd && name.empty() ) // empty object return true; *************** *** 624,768 **** - // Class Reader - // ////////////////////////////////////////////////////////////////// - CommentMemo::CommentMemo() - : currentId_( 0 ) - , lastValueEnd_( 0 ) - { - } - - - void - CommentMemo::beginDocumentParsing() - { - monitorNextValue_ = 0; - lastValueEnd_ = 0; - } - - - void - CommentMemo::parsedCComment( Reader::Location begin, - Reader::Location end ) - { - if ( lastValueEnd_ == 0 ) - addComment( begin, end, atDocumentStart ); - else if ( containsNewLine( begin, end ) || containsNewLine( lastValueEnd_, begin ) ) - addComment( begin, end, beforeValue ); - else - addComment( begin, end, afterValueOnSameLine ); - } - - - void - CommentMemo::parsedCppComment( Reader::Location begin, - Reader::Location end ) - { - if ( lastValueEnd_ == 0 ) - addComment( begin, end, atDocumentStart ); - else if ( containsNewLine( lastValueEnd_, begin ) ) - addComment( begin, end, beforeValue ); - else - addComment( begin, end, afterValueOnSameLine ); - } - - - void - CommentMemo::parsedValue( Reader::Location begin, - Reader::Location end, - Value &value ) - { - value.setId( ++currentId_ ); - for ( ; monitorNextValue_ > 0; --monitorNextValue_ ) - { - unsigned int index = (unsigned int)(comments_.size() - monitorNextValue_); - comments_[ index ].id_ = currentId_; - registerComment( comments_[index].placement_, index ); - // commentsById_[currentId_ << 4 | comments_[index].placement_] = index; - } - } - - - void - CommentMemo::endDocumentParsing() - { - unsigned int size = (unsigned int)comments_.size(); - if ( !size ) - return; - while ( --size && comments_[size].id_ == ValueId(-1) ) - { - comments_[size].id_ = currentId_; - comments_[size].placement_ = atDocumentEnd; - registerComment( atDocumentEnd, size ); - } - } - - - unsigned int - CommentMemo::getCommentCount() const - { - return (unsigned int)comments_.size(); - } - - - const CommentMemo::CommentInfo & - CommentMemo::getComment( unsigned int index ) const - { - return comments_.at(index); - } - - - bool - CommentMemo::containsNewLine( Reader::Location begin, - Reader::Location end ) - { - for ( ;begin < end; ++begin ) - if ( *begin == '\n' || *begin == '\r' ) - return true; - return false; - } - - - void - CommentMemo::addComment( Reader::Location begin, - Reader::Location end, - Placement placement ) - { - CommentInfo info; - info.comment_ = std::string( begin, end ); - info.placement_ = placement; - if ( placement == afterValueOnSameLine ) - { - info.id_ = currentId_; - registerComment( placement, comments_.size() ); - } - else - { - info.id_ = -1; - ++monitorNextValue_; - } - - comments_.push_back( info ); - } - - - void - CommentMemo::registerComment( Placement placement, - unsigned int index ) - { - commentsById_[ placement | currentId_ * 8 ] = index; - } - - - const CommentMemo::CommentInfo * - CommentMemo::findComment( ValueId id, Placement placement ) const - { - unsigned int key = placement | id * 8; - CommentsById::const_iterator it = commentsById_.find( key ); - if ( it == commentsById_.end() ) - return 0; - return &((*it).second); - } - - - } // namespace Json --- 699,701 ---- Index: json_writer.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_writer.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** json_writer.cpp 30 Jul 2005 11:15:11 -0000 1.4 --- json_writer.cpp 30 Jul 2005 17:40:59 -0000 1.5 *************** *** 149,169 **** addChildValues_ = false; indentString_.clear(); - writeValue( root ); - document_ += "\n"; - return document_; - } - - - std::string - StyledWriter::write( const Value &root, - const CommentMemo &commentMemo ) - { - commentMemo_ = &commentMemo; - writeInitialComment( root ); writeCommentBeforeValue( root ); ! write( root ); writeCommentAfterValueOnSameLine( root ); ! writeFinalComment( root ); ! commentMemo_ = 0; return document_; } --- 149,156 ---- addChildValues_ = false; indentString_.clear(); writeCommentBeforeValue( root ); ! writeValue( root ); writeCommentAfterValueOnSameLine( root ); ! document_ += "\n"; return document_; } *************** *** 331,338 **** { char last = document_[document_.length()-1]; - assert( last != '\n' ); if ( last == ' ' ) // already indented return; ! document_ += '\n'; } document_ += indentString_; --- 318,325 ---- { char last = document_[document_.length()-1]; if ( last == ' ' ) // already indented return; ! if ( last != '\n' ) // Comments may add new-line ! document_ += '\n'; } document_ += indentString_; *************** *** 364,387 **** void ! StyledWriter::writeInitialComment( const Value &root ) { ! currentComment_ = 0; ! for ( ; hasMoreComments() ! && getCurrentComment().id_ == root.id() ! && getCurrentComment().placement_ == CommentMemo::atDocumentStart; ! ++currentComment_ ) ! writeComment( getCurrentComment() ); } void ! StyledWriter::writeFinalComment( const Value &root ) { ! for ( ; hasMoreComments() ! && getCurrentComment().id_ == root.id() ! && getCurrentComment().placement_ == CommentMemo::atDocumentEnd; ! ++currentComment_ ) { ! writeComment( commentMemo_->getComment( currentComment_++ ) ); } } --- 351,374 ---- void ! StyledWriter::writeCommentBeforeValue( const Value &root ) { ! if ( !root.hasComment( commentBefore ) ) ! return; ! document_ += normalizeEOL( root.getComment( commentBefore ) ); ! document_ += "\n"; } void ! StyledWriter::writeCommentAfterValueOnSameLine( const Value &root ) { ! if ( root.hasComment( commentAfterOnSameLine ) ) ! document_ += " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); ! ! if ( root.hasComment( commentAfter ) ) { ! document_ += "\n"; ! document_ += normalizeEOL( root.getComment( commentAfter ) ); ! document_ += "\n"; } } *************** *** 389,416 **** bool ! StyledWriter::hasMoreComments() const ! { ! return commentMemo_ && currentComment_ < commentMemo_->getCommentCount(); ! } ! ! ! const CommentMemo::CommentInfo & ! StyledWriter::getCurrentComment() const ! { ! return commentMemo_->getComment( currentComment_ ); ! } ! ! ! void ! StyledWriter::writeComment( const CommentMemo::CommentInfo &comment ) { ! document_ += comment.comment_; ! document_ += "\n"; } ! bool ! StyledWriter::hasCommentForValue( const Value &value ) const { } --- 376,408 ---- bool ! StyledWriter::hasCommentForValue( const Value &value ) { ! return value.hasComment( commentBefore ) ! || value.hasComment( commentAfterOnSameLine ) ! || 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; } |