@@ -43,13 +43,21 @@ struct Gimmick {
4343 return this ->json ->dump ();
4444 }
4545
46- std::string last_dict_key () const noexcept {
47- std::string key = " <NOT FOUND>" ;
48- for (const auto & item : this ->json ->items ())
49- {
46+ template <class T >
47+ std::string next_dict_key (T last_key, const T key_cond) const noexcept {
48+ std::string key = " " , prev_key = " " ;
49+ if (last_key == key_cond)
50+ last_key = " " ;
51+
52+ for (const auto & item : this ->json ->items ()) {
5053 if (this ->json ->is_array ()) {
5154 for (auto &entry : item.value ().items ()) {
52- key = entry.key ();
55+ if (prev_key == last_key) {
56+ key = entry.key ();
57+ break ;
58+ }
59+ else
60+ prev_key = entry.key ();
5361 }
5462 } else {
5563 key = item.key ();
@@ -63,7 +71,7 @@ struct Gimmick {
6371
6472 void zoom_in (const bpstd::string_view &sub) noexcept {
6573 auto it = this ->json ->is_array ()
66- ? this ->json ->at (this ->json ->size ()-1 ).begin ( )
74+ ? this ->json ->at (this ->json ->size ()-1 ).find (sub )
6775 : this ->json ->find (sub);
6876 // TODO #112: handle errors
6977 this ->json_parent .push (this ->json );
@@ -86,35 +94,29 @@ struct Gimmick {
8694 }
8795
8896 // TODO #112: to be removed after initialising GasData with a list, and not JSON?
89- std::string first_field_name () const noexcept {
97+ auto first_field_name () const noexcept {
9098 // TODO #112: handle errors
99+ std::string name = " " ;
91100 assert (this ->json ->size () > 0 );
92101 assert (this ->json ->begin ()->size () > 0 );
93102 for (auto &entry : this ->json ->at (0 ).items ())
94103 {
95- return entry.key ();
104+ name = entry.key ();
96105 }
97- assert (false );
98- return " " ;
99- }
100-
101- auto is_empty () noexcept {
102- return this ->json ->empty ();
103- }
104-
105- auto size () noexcept {
106- return this ->json ->size ();
106+ if (name == " " )
107+ assert (false );
108+ return name;
107109 }
108110
109- std::size_t n_elements (const bpstd::string_view &name) noexcept {
111+ auto n_elements (const bpstd::string_view &name) noexcept {
112+ std::size_t n_elem = 0 ;
110113 for (auto i=0u ; i<this ->json ->size (); ++i) {
111114 for (auto &entry : this ->json ->at (i).items ()) {
112115 if (entry.key () == name)
113- return entry.value ().size ();
116+ n_elem = entry.value ().size ();
114117 }
115118 }
116- assert (false );
117- return 0 ;
119+ return n_elem;
118120 }
119121
120122 auto n_numeric_array_entries () noexcept {
@@ -223,26 +225,30 @@ struct InputGimmick: Gimmick {
223225 }
224226
225227 bool read_line (std::string &name, std::string &data) {
226- bool eof = this ->is_empty ();
227-
228- if (this ->zoom_level () == this ->max_zoom_level ) { // TODO #112
229- eof = true ;
228+ bool subsequent_record = false ;
229+ if (this ->zoom_level () == this ->max_zoom_level ) {
230230 this ->zoom_out ();
231- }
232231
233- if (!eof) {
234- assert (this ->size () == 1 );
235- auto key = this ->last_dict_key ();
236- if (this ->key_name != " " && (this ->key_cond == this ->last_read_line_key )) {
237- name = this ->key_name ;
238- this ->zoom_in (key);
239- } else {
240- name = key;
232+ auto key = this ->next_dict_key (this ->last_read_line_key , this ->key_cond );
233+ if (key == " " ) {
234+ this ->last_read_line_key = " " ;
235+ return true ;
241236 }
242- data = key;
243- this -> last_read_line_key = key ;
237+ else
238+ subsequent_record = true ;
244239 }
245- return eof;
240+
241+ auto key = this ->next_dict_key (this ->last_read_line_key , this ->key_cond );
242+ if (subsequent_record || (this ->key_name != " " && (this ->key_cond == this ->last_read_line_key ))) {
243+ name = this ->key_name ;
244+ this ->zoom_in (key);
245+ } else {
246+ name = key;
247+ }
248+ data = key;
249+ this ->last_read_line_key = key;
250+
251+ return false ;
246252 }
247253};
248254
0 commit comments