8
8
#include < array>
9
9
#include < vector>
10
10
#include < cassert>
11
+ #include < limits>
11
12
#include " source_base/formatter.h"
12
13
#include " source_base/global_file.h"
13
14
#include " source_base/global_function.h"
14
15
#include " source_base/tool_quit.h"
15
16
#include " source_base/tool_title.h"
16
17
#include " source_base/module_device/device.h"
18
+
17
19
namespace ModuleIO
18
20
{
19
21
@@ -228,7 +230,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
228
230
ifs.clear ();
229
231
ifs.seekg (0 );
230
232
231
- std::string word, word1 ;
233
+ std::string word;
232
234
int ierr = 0 ;
233
235
234
236
// ifs >> std::setiosflags(ios::uppercase);
@@ -251,19 +253,21 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
251
253
<< " The parameter list always starts with key word "
252
254
" 'INPUT_PARAMETERS'. "
253
255
<< 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 );
255
258
}
256
259
257
260
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
258
263
while (ifs.good ())
259
264
{
260
- ifs >> word1 ;
265
+ ifs >> word_ ;
261
266
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
267
271
{
268
272
Input_Item* p_item = &(it->second );
269
273
this ->readvalue_items .push_back (p_item);
@@ -275,17 +279,18 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
275
279
// qianrui delete '/' 2024-07-10, because path has '/' head.
276
280
read_information (ifs, p_item->str_values , " #!" );
277
281
}
278
- else
282
+ else // otherwise, it should be a comment or an unrecognized parameter
279
283
{
280
- if (word[0 ] != ' #' && word[0 ] != ' /' && word[0 ] != ' !' )
284
+ if (word[0 ] != ' #' && word[0 ] != ' /' && word[0 ] != ' !' ) // if not recognized
281
285
{
282
286
std::cout << " THE PARAMETER NAME '" << word << " ' IS INCORRECT!" << std::endl;
283
287
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 );
287
289
}
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 ' );
289
294
}
290
295
291
296
ifs.rdstate ();
0 commit comments