Skip to content

Commit

Permalink
Change snippet_library::add_snippets_from_json to take a const refere…
Browse files Browse the repository at this point in the history
…nce.

Which allows to iterate over the array using a range-based loop.
  • Loading branch information
BevapDin committed Dec 18, 2019
1 parent d088831 commit 59f8ce1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2204,8 +2204,7 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std:
// auto-create a category that is unlikely to already be used and put the
// snippets in it.
def.snippet_category = std::string( "auto:" ) + def.id;
JsonArray jarr = jo.get_array( "snippet_category" );
SNIPPET.add_snippets_from_json( def.snippet_category, jarr );
SNIPPET.add_snippets_from_json( def.snippet_category, jo.get_array( "snippet_category" ) );
} else {
def.snippet_category = jo.get_string( "snippet_category", "" );
}
Expand Down
15 changes: 7 additions & 8 deletions src/text_snippets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,27 @@ void snippet_library::load_snippet( const JsonObject &jsobj )
hash_to_id_migration = cata::nullopt;
const std::string category = jsobj.get_string( "category" );
if( jsobj.has_array( "text" ) ) {
JsonArray jarr = jsobj.get_array( "text" );
add_snippets_from_json( category, jarr );
add_snippets_from_json( category, jsobj.get_array( "text" ) );
} else {
add_snippet_from_json( category, jsobj );
}
}

void snippet_library::add_snippets_from_json( const std::string &category, JsonArray &jarr )
void snippet_library::add_snippets_from_json( const std::string &category, const JsonArray &jarr )
{
if( hash_to_id_migration.has_value() ) {
debugmsg( "snippet_library::add_snippets_from_json called after snippet_library::migrate_hash_to_id." );
}
hash_to_id_migration = cata::nullopt;
while( jarr.has_more() ) {
if( jarr.test_string() ) {
for( const JsonValue &entry : jarr ) {
if( entry.test_string() ) {
translation text;
if( !jarr.read_next( text ) ) {
jarr.throw_error( "Error reading snippet from JSON array" );
if( !entry.read( text ) ) {
entry.throw_error( "Error reading snippet from JSON array" );
}
snippets_by_category[category].no_id.emplace_back( text );
} else {
JsonObject jo = jarr.next_object();
JsonObject jo = entry.get_object();
add_snippet_from_json( category, jo );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/text_snippets.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class snippet_library
* Entries in the array can be simple strings, or json objects (for the
* later see add_snippet_from_json).
*/
void add_snippets_from_json( const std::string &category, JsonArray &jarr );
void add_snippets_from_json( const std::string &category, const JsonArray &jarr );
/**
* Load a single snippet text from the json object. The object should have
* a "text" member with the text of the snippet.
Expand Down

0 comments on commit 59f8ce1

Please sign in to comment.