10 #if _MSC_VER >= 1400 // VC++ 8.0
11 #pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
18 return ch > 0 && ch <= 0x1F;
36 *--current = (value % 10) +
'0';
45 char *current = buffer +
sizeof(buffer);
46 bool isNegative = value < 0;
52 assert( current >= buffer );
60 char *current = buffer +
sizeof(buffer);
62 assert( current >= buffer );
69 #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning.
70 sprintf_s(buffer,
sizeof(buffer),
"%#.16g", value);
72 sprintf(buffer,
"%#.16g", value);
74 char* ch = buffer + strlen(buffer) - 1;
75 if (*ch !=
'0')
return buffer;
76 while(ch > buffer && *ch ==
'0'){
79 char* last_nonzero = ch;
96 *(last_nonzero+2) =
'\0';
108 return value ?
"true" :
"false";
115 return std::string(
"\"") + value +
"\"";
119 unsigned maxsize = strlen(value)*2 + 3;
121 result.reserve(maxsize);
123 for (
const char* c=value; *c != 0; ++c)
159 std::ostringstream oss;
160 oss <<
"\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*c);
185 : yamlCompatiblityEnabled_( false )
193 yamlCompatiblityEnabled_ =
true;
208 FastWriter::writeValue(
const Value &value )
210 switch ( value.
type() )
233 int size = value.
size();
234 for (
int index =0; index < size; ++index )
238 writeValue( value[index] );
247 for ( Value::Members::iterator it = members.begin();
251 const std::string &name = *it;
252 if ( it != members.begin() )
255 document_ += yamlCompatiblityEnabled_ ?
": "
257 writeValue( value[name] );
280 addChildValues_ =
false;
282 writeCommentBeforeValue( root );
284 writeCommentAfterValueOnSameLine( root );
291 StyledWriter::writeValue(
const Value &value )
293 switch ( value.
type() )
314 writeArrayValue( value);
319 if ( members.empty() )
323 writeWithIndent(
"{" );
325 Value::Members::iterator it = members.begin();
328 const std::string &name = *it;
329 const Value &childValue = value[name];
330 writeCommentBeforeValue( childValue );
333 writeValue( childValue );
334 if ( ++it == members.end() )
336 writeCommentAfterValueOnSameLine( childValue );
340 writeCommentAfterValueOnSameLine( childValue );
343 writeWithIndent(
"}" );
352 StyledWriter::writeArrayValue(
const Value &value )
354 unsigned size = value.size();
359 bool isArrayMultiLine = isMultineArray( value );
360 if ( isArrayMultiLine )
362 writeWithIndent(
"[" );
364 bool hasChildValue = !childValues_.empty();
368 const Value &childValue = value[index];
369 writeCommentBeforeValue( childValue );
371 writeWithIndent( childValues_[index] );
375 writeValue( childValue );
377 if ( ++index == size )
379 writeCommentAfterValueOnSameLine( childValue );
383 writeCommentAfterValueOnSameLine( childValue );
386 writeWithIndent(
"]" );
390 assert( childValues_.size() == size );
392 for (
unsigned index =0; index < size; ++index )
396 document_ += childValues_[index];
405 StyledWriter::isMultineArray(
const Value &value )
407 int size = value.size();
408 bool isMultiLine = size*3 >= rightMargin_ ;
409 childValues_.clear();
410 for (
int index =0; index < size && !isMultiLine; ++index )
412 const Value &childValue = value[index];
413 isMultiLine = isMultiLine ||
414 ( (childValue.isArray() || childValue.isObject()) &&
415 childValue.size() > 0 );
419 childValues_.reserve( size );
420 addChildValues_ =
true;
421 int lineLength = 4 + (size-1)*2;
422 for (
int index =0; index < size && !isMultiLine; ++index )
424 writeValue( value[index] );
425 lineLength += int( childValues_[index].length() );
426 isMultiLine = isMultiLine && hasCommentForValue( value[index] );
428 addChildValues_ =
false;
429 isMultiLine = isMultiLine || lineLength >= rightMargin_;
436 StyledWriter::pushValue(
const std::string &value )
438 if ( addChildValues_ )
439 childValues_.push_back( value );
446 StyledWriter::writeIndent()
448 if ( !document_.empty() )
450 char last = document_[document_.length()-1];
456 document_ += indentString_;
461 StyledWriter::writeWithIndent(
const std::string &value )
469 StyledWriter::indent()
471 indentString_ += std::string( indentSize_,
' ' );
476 StyledWriter::unindent()
478 assert(
int(indentString_.size()) >= indentSize_ );
479 indentString_.resize( indentString_.size() - indentSize_ );
484 StyledWriter::writeCommentBeforeValue(
const Value &root )
488 document_ += normalizeEOL( root.getComment(
commentBefore ) );
494 StyledWriter::writeCommentAfterValueOnSameLine(
const Value &root )
502 document_ += normalizeEOL( root.getComment(
commentAfter ) );
509 StyledWriter::hasCommentForValue(
const Value &value )
518 StyledWriter::normalizeEOL(
const std::string &text )
520 std::string normalized;
521 normalized.reserve( text.length() );
522 const char *begin = text.c_str();
523 const char *end = begin + text.length();
524 const char *current = begin;
525 while ( current != end )
530 if ( *current ==
'\n' )
547 , indentation_( indentation )
556 addChildValues_ =
false;
558 writeCommentBeforeValue( root );
560 writeCommentAfterValueOnSameLine( root );
567 StyledStreamWriter::writeValue(
const Value &value )
569 switch ( value.
type() )
590 writeArrayValue( value);
595 if ( members.empty() )
599 writeWithIndent(
"{" );
601 Value::Members::iterator it = members.begin();
604 const std::string &name = *it;
605 const Value &childValue = value[name];
606 writeCommentBeforeValue( childValue );
609 writeValue( childValue );
610 if ( ++it == members.end() )
612 writeCommentAfterValueOnSameLine( childValue );
616 writeCommentAfterValueOnSameLine( childValue );
619 writeWithIndent(
"}" );
628 StyledStreamWriter::writeArrayValue(
const Value &value )
630 unsigned size = value.size();
635 bool isArrayMultiLine = isMultineArray( value );
636 if ( isArrayMultiLine )
638 writeWithIndent(
"[" );
640 bool hasChildValue = !childValues_.empty();
644 const Value &childValue = value[index];
645 writeCommentBeforeValue( childValue );
647 writeWithIndent( childValues_[index] );
651 writeValue( childValue );
653 if ( ++index == size )
655 writeCommentAfterValueOnSameLine( childValue );
659 writeCommentAfterValueOnSameLine( childValue );
662 writeWithIndent(
"]" );
666 assert( childValues_.size() == size );
668 for (
unsigned index =0; index < size; ++index )
672 *document_ << childValues_[index];
681 StyledStreamWriter::isMultineArray(
const Value &value )
683 int size = value.size();
684 bool isMultiLine = size*3 >= rightMargin_ ;
685 childValues_.clear();
686 for (
int index =0; index < size && !isMultiLine; ++index )
688 const Value &childValue = value[index];
689 isMultiLine = isMultiLine ||
690 ( (childValue.isArray() || childValue.isObject()) &&
691 childValue.size() > 0 );
695 childValues_.reserve( size );
696 addChildValues_ =
true;
697 int lineLength = 4 + (size-1)*2;
698 for (
int index =0; index < size && !isMultiLine; ++index )
700 writeValue( value[index] );
701 lineLength += int( childValues_[index].length() );
702 isMultiLine = isMultiLine && hasCommentForValue( value[index] );
704 addChildValues_ =
false;
705 isMultiLine = isMultiLine || lineLength >= rightMargin_;
712 StyledStreamWriter::pushValue(
const std::string &value )
714 if ( addChildValues_ )
715 childValues_.push_back( value );
722 StyledStreamWriter::writeIndent()
736 *document_ <<
'\n' << indentString_;
741 StyledStreamWriter::writeWithIndent(
const std::string &value )
749 StyledStreamWriter::indent()
751 indentString_ += indentation_;
756 StyledStreamWriter::unindent()
758 assert( indentString_.size() >= indentation_.size() );
759 indentString_.resize( indentString_.size() - indentation_.size() );
764 StyledStreamWriter::writeCommentBeforeValue(
const Value &root )
768 *document_ << normalizeEOL( root.getComment(
commentBefore ) );
774 StyledStreamWriter::writeCommentAfterValueOnSameLine(
const Value &root )
782 *document_ << normalizeEOL( root.getComment(
commentAfter ) );
789 StyledStreamWriter::hasCommentForValue(
const Value &value )
798 StyledStreamWriter::normalizeEOL(
const std::string &text )
800 std::string normalized;
801 normalized.reserve( text.length() );
802 const char *begin = text.c_str();
803 const char *end = begin + text.length();
804 const char *current = begin;
805 while ( current != end )
810 if ( *current ==
'\n' )
824 writer.
write(sout, root);