From: Baptiste L. <bl...@us...> - 2005-07-30 11:15:20
|
Update of /cvsroot/jsoncpp/jsoncpp/src/lib_json In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3607/src/lib_json Modified Files: json_reader.cpp json_value.cpp json_writer.cpp Log Message: * added doxygen documentation Index: json_value.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_value.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** json_value.cpp 27 Jul 2005 22:25:57 -0000 1.3 --- json_value.cpp 30 Jul 2005 11:15:11 -0000 1.4 *************** *** 92,98 **** Value::Value( ValueType type ) : type_( type ) ! , parent_( 0 ) ! , previous_( 0 ) ! , next_( 0 ) { switch ( type ) --- 92,96 ---- Value::Value( ValueType type ) : type_( type ) ! , id_( 0 ) { switch ( type ) *************** *** 125,128 **** --- 123,127 ---- Value::Value( Int value ) : type_( intValue ) + , id_( 0 ) { value_.int_ = value; *************** *** 132,135 **** --- 131,135 ---- Value::Value( UInt value ) : type_( uintValue ) + , id_( 0 ) { value_.uint_ = value; *************** *** 138,141 **** --- 138,142 ---- Value::Value( double value ) : type_( realValue ) + , id_( 0 ) { value_.real_ = value; *************** *** 144,147 **** --- 145,149 ---- Value::Value( const char *value ) : type_( stringValue ) + , id_( 0 ) { value_.string_ = value ? strdup( value ) : 0; *************** *** 150,153 **** --- 152,156 ---- Value::Value( const std::string &value ) : type_( stringValue ) + , id_( 0 ) { value_.string_ = value.empty() ? 0 : strdup( value.c_str() ); *************** *** 156,159 **** --- 159,163 ---- Value::Value( bool value ) : type_( booleanValue ) + , id_( 0 ) { value_.bool_ = value; *************** *** 163,166 **** --- 167,171 ---- Value::Value( const Value &other ) : type_( other.type_ ) + , id_( 0 ) { switch ( type_ ) *************** *** 601,616 **** it = value_.map_->insert( it, defaultValue ); Value &value = (*it).second; - /* - if ( result.second ) - { - value.parent_ = this; - if ( value_.map_.size() > 1 ) - { - Value &headValue = (*value_.map_.begin()).second; - value.next_ = &headValue; - value.previous = headValue.previous_; - headValue.previous_ = &value; - } - }*/ return value; } --- 606,609 ---- *************** *** 773,776 **** --- 766,783 ---- + void + Value::setId( ValueId id ) + { + id_ = id; + } + + + ValueId + Value::id() const + { + return id_; + } + + // class PathArgument // ////////////////////////////////////////////////////////////////// Index: json_reader.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_reader.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** json_reader.cpp 27 Jul 2005 07:09:23 -0000 1.1.1.1 --- json_reader.cpp 30 Jul 2005 11:15:11 -0000 1.2 *************** *** 19,23 **** --- 19,27 ---- + // Class Reader + // ////////////////////////////////////////////////////////////////// + Reader::Reader() + : commentMemo_( 0 ) { } *************** *** 39,42 **** --- 43,60 ---- + 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; + } + + bool Reader::readValue() *************** *** 606,609 **** --- 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 Index: json_writer.cpp =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_writer.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** json_writer.cpp 28 Jul 2005 08:21:11 -0000 1.3 --- json_writer.cpp 30 Jul 2005 11:15:11 -0000 1.4 *************** *** 74,77 **** --- 74,78 ---- } + void FastWriter::writeValue( const Value &value ) *************** *** 154,157 **** --- 155,173 ---- + 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_; + } + + void StyledWriter::writeValue( const Value &value ) *************** *** 189,202 **** writeWithIndent( "{" ); indent(); ! for ( Value::Members::iterator it = members.begin(); ! it != members.end(); ! ++it ) { const std::string &name = *it; ! if ( it != members.begin() ) ! document_ += ","; writeWithIndent( valueToQuotedString( name.c_str() ) ); document_ += " : "; ! writeValue( value[name] ); } unindent(); --- 205,224 ---- writeWithIndent( "{" ); indent(); ! Value::Members::iterator it = members.begin(); ! while ( true ) { const std::string &name = *it; ! const Value &childValue = value[name]; ! writeCommentBeforeValue( childValue ); writeWithIndent( valueToQuotedString( name.c_str() ) ); document_ += " : "; ! writeValue( childValue ); ! if ( ++it == members.end() ) ! { ! writeCommentAfterValueOnSameLine( childValue ); ! break; ! } ! document_ += ","; ! writeCommentAfterValueOnSameLine( childValue ); } unindent(); *************** *** 223,230 **** indent(); bool hasChildValue = !childValues_.empty(); ! for ( int index =0; index < size; ++index ) { ! if ( index > 0 ) ! document_ += ","; if ( hasChildValue ) writeWithIndent( childValues_[index] ); --- 245,253 ---- indent(); bool hasChildValue = !childValues_.empty(); ! int index =0; ! while ( true ) { ! const Value &childValue = value[index]; ! writeCommentBeforeValue( childValue ); if ( hasChildValue ) writeWithIndent( childValues_[index] ); *************** *** 232,237 **** { writeIndent(); ! writeValue( value[index] ); } } unindent(); --- 255,267 ---- { writeIndent(); ! writeValue( childValue ); } + if ( ++index == size ) + { + writeCommentAfterValueOnSameLine( childValue ); + break; + } + document_ += ","; + writeCommentAfterValueOnSameLine( childValue ); } unindent(); *************** *** 276,279 **** --- 306,310 ---- writeValue( value[index] ); lineLength += int( childValues_[index].length() ); + isMultiLine = isMultiLine && hasCommentForValue( value[index] ); } addChildValues_ = false; *************** *** 332,334 **** --- 363,418 ---- + 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_++ ) ); + } + } + + + 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 + { + } + + } // namespace Json |