make some changes according to the code review.

- use `std::string` instead of `new char`;
- reserve 1 MB for buffer;
pull/6949/head
MYLS 8 years ago
parent 7429ed07be
commit 86238d2748
  1. 26
      modules/core/src/persistence.cpp

@ -1172,19 +1172,17 @@ static void check_if_write_struct_is_delayed( CvFileStorage* fs, bool change_typ
if ( fs->is_write_struct_delayed ) if ( fs->is_write_struct_delayed )
{ {
/* save data to prevent recursive call errors */ /* save data to prevent recursive call errors */
char * struct_key = 0; std::string struct_key;
char * type_name = 0; std::string type_name;
int struct_flags = fs->delayed_struct_flags; int struct_flags = fs->delayed_struct_flags;
if ( fs->delayed_struct_key != 0 ) if ( fs->delayed_struct_key != 0 && *fs->delayed_type_name != '\0' )
{ {
struct_key = new char[strlen(fs->delayed_struct_key) + 1U]; struct_key.assign(fs->delayed_struct_key);
strcpy(struct_key, fs->delayed_struct_key);
} }
if ( fs->delayed_type_name != 0 ) if ( fs->delayed_type_name != 0 && *fs->delayed_type_name != '\0' )
{ {
type_name = new char[strlen(type_name) + 1U]; struct_key.assign(fs->delayed_type_name);
strcpy(type_name, fs->delayed_type_name);
} }
/* reset */ /* reset */
@ -1199,21 +1197,18 @@ static void check_if_write_struct_is_delayed( CvFileStorage* fs, bool change_typ
/* call */ /* call */
if ( change_type_to_base64 ) if ( change_type_to_base64 )
{ {
fs->start_write_struct( fs, struct_key, struct_flags, "binary"); fs->start_write_struct( fs, struct_key.c_str(), struct_flags, "binary");
if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) if ( fs->state_of_writing_base64 != base64::fs::Uncertain )
switch_to_Base64_state( fs, base64::fs::Uncertain ); switch_to_Base64_state( fs, base64::fs::Uncertain );
switch_to_Base64_state( fs, base64::fs::InUse ); switch_to_Base64_state( fs, base64::fs::InUse );
} }
else else
{ {
fs->start_write_struct( fs, struct_key, struct_flags, type_name); fs->start_write_struct( fs, struct_key.c_str(), struct_flags, type_name.c_str());
if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) if ( fs->state_of_writing_base64 != base64::fs::Uncertain )
switch_to_Base64_state( fs, base64::fs::Uncertain ); switch_to_Base64_state( fs, base64::fs::Uncertain );
switch_to_Base64_state( fs, base64::fs::NotUse ); switch_to_Base64_state( fs, base64::fs::NotUse );
} }
delete struct_key;
delete type_name;
} }
} }
@ -1246,6 +1241,7 @@ static void make_write_struct_delayed(
fs->is_write_struct_delayed = true; fs->is_write_struct_delayed = true;
} }
static const size_t PARSER_BASE64_BUFFER_SIZE = 1024U * 1024U / 8U;
/****************************************************************************************\ /****************************************************************************************\
* YAML Parser * * YAML Parser *
@ -1348,7 +1344,7 @@ static char* icvYMLParseBase64(CvFileStorage* fs, char* ptr, int indent, CvFileN
/* get all Base64 data */ /* get all Base64 data */
std::string base64_buffer; std::string base64_buffer;
base64_buffer.reserve( 16U * 1024U * 1024U ); base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE );
while( beg < end ) while( beg < end )
{ {
base64_buffer.append( beg, end ); base64_buffer.append( beg, end );
@ -2304,7 +2300,7 @@ static char* icvXMLParseBase64(CvFileStorage* fs, char* ptr, CvFileNode * node)
/* get all Base64 data */ /* get all Base64 data */
std::string base64_buffer; // not an efficient way. std::string base64_buffer; // not an efficient way.
base64_buffer.reserve( 16U * 1024U * 1024U ); base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE );
while( beg < end ) while( beg < end )
{ {
base64_buffer.append( beg, end ); base64_buffer.append( beg, end );

Loading…
Cancel
Save