MONGO_EXPORT void bson_iterator_subobject_init( const bson_iterator *i, bson *sub, bson_bool_t copyData ) { const char *data = bson_iterator_value( i ); if( copyData ) bson_init_finished_data_with_copy( sub, data ); else bson_init_finished_data( sub, (char *)data, 0 ); }
int test_bson_init_finished( void ) { bson b; ALLOW_AND_REQUIRE_MALLOC_BEGIN; bson_init( &b ); ALLOW_AND_REQUIRE_MALLOC_END; bson_append_double( &b, "d", 3.14 ); bson_append_string( &b, "s", "hello" ); bson_finish( &b ); ASSERT( bson_size( &b ) == 29 ); // 29 determined by running this code bson b2; bson_init_finished_data( &b2, (char *) bson_data( &b ), 0 ); ASSERT( bson_size( &b ) == bson_size( &b2 ) ); bson_destroy( &b2 ); ALLOW_AND_REQUIRE_MALLOC_BEGIN; bson_init_finished_data_with_copy( &b2, (char *) bson_data( &b ) ); ALLOW_AND_REQUIRE_MALLOC_END; ASSERT( bson_size( &b ) == bson_size( &b2 ) ); ALLOW_AND_REQUIRE_FREE_BEGIN; bson_destroy( &b2 ); ALLOW_AND_REQUIRE_FREE_END; bson_init_finished_data( &b2, (char *) bson_data( &b ), 1 ); ASSERT( bson_size( &b ) == bson_size( &b2 ) ); ALLOW_AND_REQUIRE_FREE_BEGIN; bson_destroy( &b2 ); ALLOW_AND_REQUIRE_FREE_END; return 0; }
MONGO_EXPORT void bson_iterator_code_scope_init( const bson_iterator *i, bson *scope, bson_bool_t copyData ) { if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) { int codeLen = bson_finished_data_size( bson_iterator_value( i )+4 ); const char * scopeData = bson_iterator_value( i )+8+codeLen; if( copyData ) bson_init_finished_data_with_copy( scope, scopeData ); else bson_init_finished_data( scope, (char *)scopeData, 0 ); } else { bson_init_empty( scope ); } }
MONGO_EXPORT int bson_copy( bson *out, const bson *in ) { if ( !out || !in ) return BSON_ERROR; if ( !in->finished ) return BSON_ERROR; return bson_init_finished_data_with_copy( out, in->data ); }