Skip to content

Commit c16e1c2

Browse files
committed
Refactor: release the limit (<150) on length of comment lines of INPUT
1 parent a145293 commit c16e1c2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

source/module_io/read_input.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <array>
99
#include <vector>
1010
#include <cassert>
11+
#include <limits>
1112
#include "module_base/formatter.h"
1213
#include "module_base/global_file.h"
1314
#include "module_base/global_function.h"
@@ -227,7 +228,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
227228
ifs.clear();
228229
ifs.seekg(0);
229230

230-
std::string word, word1;
231+
std::string word;
231232
int ierr = 0;
232233

233234
// ifs >> std::setiosflags(ios::uppercase);
@@ -250,19 +251,21 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
250251
<< " The parameter list always starts with key word "
251252
"'INPUT_PARAMETERS'. "
252253
<< std::endl;
253-
ModuleBase::WARNING_QUIT("Input", "Bad parameter, please check the input parameters in file INPUT", 1);
254+
ModuleBase::WARNING_QUIT("Input",
255+
"Bad parameter, please check the input parameters in file INPUT", 1);
254256
}
255257

256258
ifs.rdstate();
259+
// the `word1` is moved here and is renamed to improve the code-readability
260+
std::string word_; // temporary variable to store the keyword read-in
257261
while (ifs.good())
258262
{
259-
ifs >> word1;
263+
ifs >> word_;
260264
if (ifs.eof()) { break; }
261-
word = FmtCore::lower(word1);
262-
auto it = std::find_if(input_lists.begin(),
263-
input_lists.end(),
264-
[&word](const std::pair<std::string, Input_Item>& item) { return item.first == word; });
265-
if (it != this->input_lists.end())
265+
word = FmtCore::lower(word_); // the lowercase of the keyword
266+
auto it = std::find_if(input_lists.begin(), input_lists.end(),
267+
[&word](const std::pair<std::string, Input_Item>& item) { return item.first == word; });
268+
if (it != this->input_lists.end()) // find the keyword
266269
{
267270
Input_Item* p_item = &(it->second);
268271
this->readvalue_items.push_back(p_item);
@@ -274,17 +277,18 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
274277
// qianrui delete '/' 2024-07-10, because path has '/' head.
275278
read_information(ifs, p_item->str_values, "#!");
276279
}
277-
else
280+
else // otherwise, it should be a comment or an unrecognized parameter
278281
{
279-
if (word[0] != '#' && word[0] != '/' && word[0] != '!')
282+
if (word[0] != '#' && word[0] != '/' && word[0] != '!') // if not recognized
280283
{
281284
std::cout << " THE PARAMETER NAME '" << word << "' IS INCORRECT!" << std::endl;
282285
ModuleBase::WARNING_QUIT("Input",
283-
"Bad parameter, please check the "
284-
"input parameters in file INPUT",
285-
1);
286+
"Bad parameter, please check the input parameters in file INPUT", 1);
286287
}
287-
ifs.ignore(150, '\n');
288+
// otherwise, it is a comment. However, ...
289+
// but it is not always to be shorter than 150 characters
290+
// we can use ignore to skip the rest of the line
291+
ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
288292
}
289293

290294
ifs.rdstate();

0 commit comments

Comments
 (0)