88#include < array>
99#include < vector>
1010#include < cassert>
11+ #include < limits>
1112#include " source_base/formatter.h"
1213#include " source_base/global_file.h"
1314#include " source_base/global_function.h"
1415#include " source_base/tool_quit.h"
1516#include " source_base/tool_title.h"
1617#include " source_base/module_device/device.h"
18+
1719namespace ModuleIO
1820{
1921
@@ -228,7 +230,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
228230 ifs.clear ();
229231 ifs.seekg (0 );
230232
231- std::string word, word1 ;
233+ std::string word;
232234 int ierr = 0 ;
233235
234236 // ifs >> std::setiosflags(ios::uppercase);
@@ -251,19 +253,21 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
251253 << " The parameter list always starts with key word "
252254 " 'INPUT_PARAMETERS'. "
253255 << std::endl;
254- ModuleBase::WARNING_QUIT (" Input" , " Bad parameter, please check the input parameters in file INPUT" , 1 );
256+ ModuleBase::WARNING_QUIT (" Input" ,
257+ " Bad parameter, please check the input parameters in file INPUT" , 1 );
255258 }
256259
257260 ifs.rdstate ();
261+ // the `word1` is moved here and is renamed to improve the code-readability
262+ std::string word_; // temporary variable to store the keyword read-in
258263 while (ifs.good ())
259264 {
260- ifs >> word1 ;
265+ ifs >> word_ ;
261266 if (ifs.eof ()) { break ; }
262- word = FmtCore::lower (word1);
263- auto it = std::find_if (input_lists.begin (),
264- input_lists.end (),
265- [&word](const std::pair<std::string, Input_Item>& item) { return item.first == word; });
266- if (it != this ->input_lists .end ())
267+ word = FmtCore::lower (word_); // the lowercase of the keyword
268+ auto it = std::find_if (input_lists.begin (), input_lists.end (),
269+ [&word](const std::pair<std::string, Input_Item>& item) { return item.first == word; });
270+ if (it != this ->input_lists .end ()) // find the keyword
267271 {
268272 Input_Item* p_item = &(it->second );
269273 this ->readvalue_items .push_back (p_item);
@@ -275,17 +279,18 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
275279 // qianrui delete '/' 2024-07-10, because path has '/' head.
276280 read_information (ifs, p_item->str_values , " #!" );
277281 }
278- else
282+ else // otherwise, it should be a comment or an unrecognized parameter
279283 {
280- if (word[0 ] != ' #' && word[0 ] != ' /' && word[0 ] != ' !' )
284+ if (word[0 ] != ' #' && word[0 ] != ' /' && word[0 ] != ' !' ) // if not recognized
281285 {
282286 std::cout << " THE PARAMETER NAME '" << word << " ' IS INCORRECT!" << std::endl;
283287 ModuleBase::WARNING_QUIT (" Input" ,
284- " Bad parameter, please check the "
285- " input parameters in file INPUT" ,
286- 1 );
288+ " Bad parameter, please check the input parameters in file INPUT" , 1 );
287289 }
288- ifs.ignore (150 , ' \n ' );
290+ // otherwise, it is a comment. However, ...
291+ // but it is not always to be shorter than 150 characters
292+ // we can use ignore to skip the rest of the line
293+ ifs.ignore (std::numeric_limits<std::streamsize>::max (), ' \n ' );
289294 }
290295
291296 ifs.rdstate ();
0 commit comments