55#include < fstream>
66#include < iostream>
77#include < sstream>
8+ #include < array>
9+ #include < vector>
10+ #include < cassert>
11+ #include " module_base/formatter.h"
812#include " module_base/global_file.h"
913#include " module_base/global_function.h"
1014#include " module_base/tool_quit.h"
1115#include " module_base/tool_title.h"
1216namespace ModuleIO
1317{
1418
15- void strtolower ( char * sa, char * sb )
19+ std::string longstring ( const std::vector<std::string>& words )
1620{
17- char c;
18- int len = strlen (sa);
19- for (int i = 0 ; i < len; i++)
20- {
21- c = sa[i];
22- sb[i] = tolower (c);
23- }
24- sb[len] = ' \0 ' ;
21+ return FmtCore::join (" " , words);
2522}
2623
27- std::string longstring (const std::vector<std:: string>& str_values )
24+ bool assume_as_boolean (const std::string& val )
2825{
29- std::string output;
30- output = " " ;
31- const size_t length = str_values.size ();
32- for (int i = 0 ; i < length; ++i)
33- {
34- output += str_values[i];
35- if (i != length - 1 )
36- {
37- output += " " ;
38- }
39- }
40- return output;
41- }
26+ const std::string val_ = FmtCore::lower (val);
4227
43- bool convert_bool (std::string str)
44- {
45- for (auto & i: str)
46- {
47- i = tolower (i);
48- }
49- if (str == " true" )
50- {
51- return true ;
52- }
53- else if (str == " false" )
54- {
55- return false ;
56- }
57- else if (str == " 1" )
58- {
59- return true ;
60- }
61- else if (str == " 0" )
62- {
63- return false ;
64- }
65- else if (str == " t" )
28+ const std::array<std::string, 7 > t_ = {" true" , " 1" , " t" , " yes" , " y" , " on" , " .true." };
29+ const std::array<std::string, 7 > f_ = {" false" , " 0" , " f" , " no" , " n" , " off" , " .false." };
30+ // This will work because std::array<T, N>::size() is a constexpr function
31+ // Ouch it is of C++17 standard...
32+ // static_assert(t_.size() == f_.size(), "t_ and f_ must have the same lengths");
33+ #ifdef __DEBUG // C++11 can do this
34+ assert (t_.size () == f_.size ());
35+ #endif
36+
37+ if (std::find (t_.begin (), t_.end (), val_) != t_.end ())
6638 {
6739 return true ;
6840 }
69- else if (str == " f " )
41+ else if (std::find (f_. begin (), f_. end (), val_) != f_. end () )
7042 {
7143 return false ;
7244 }
7345 else
7446 {
75- std::string warningstr = " Bad boolean parameter " ;
76- warningstr .append (str );
77- warningstr .append (" , please check the input parameters in file INPUT" );
78- ModuleBase::WARNING_QUIT (" Input" , warningstr );
47+ std::string warnmsg = " Bad boolean parameter " ;
48+ warnmsg .append (val );
49+ warnmsg .append (" , please check the input parameters in file INPUT" );
50+ ModuleBase::WARNING_QUIT (" Input" , warnmsg );
7951 }
8052}
53+
8154std::string to_dir (const std::string& str)
8255{
8356 std::string str_dir = str;
@@ -216,8 +189,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
216189 ifs.clear ();
217190 ifs.seekg (0 );
218191
219- char word[80 ];
220- char word1[80 ];
192+ std::string word, word1;
221193 int ierr = 0 ;
222194
223195 // ifs >> std::setiosflags(ios::uppercase);
@@ -226,7 +198,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
226198 {
227199 ifs >> word;
228200 ifs.ignore (150 , ' \n ' );
229- if (strcmp ( word, " INPUT_PARAMETERS " ) == 0 )
201+ if (word == " INPUT_PARAMETERS " )
230202 {
231203 ierr = 1 ;
232204 break ;
@@ -247,10 +219,8 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
247219 while (ifs.good ())
248220 {
249221 ifs >> word1;
250- if (ifs.eof ()) {
251- break ;
252- }
253- strtolower (word1, word);
222+ if (ifs.eof ()) { break ; }
223+ word = FmtCore::lower (word1);
254224 auto it = std::find_if (input_lists.begin (),
255225 input_lists.end (),
256226 [&word](const std::pair<std::string, Input_Item>& item) { return item.first == word; });
@@ -311,7 +281,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
311281 Input_Item* resetvalue_item = &(input_item.second );
312282 if (resetvalue_item->reset_value != nullptr ) {
313283 resetvalue_item->reset_value (*resetvalue_item, param);
314- }
284+ }
315285 }
316286 this ->set_globalv (param);
317287
@@ -327,7 +297,7 @@ void ReadInput::read_txt_input(Parameter& param, const std::string& filename)
327297 Input_Item* checkvalue_item = &(input_item.second );
328298 if (checkvalue_item->check_value != nullptr ) {
329299 checkvalue_item->check_value (*checkvalue_item, param);
330- }
300+ }
331301 }
332302}
333303
@@ -505,12 +475,6 @@ void ReadInput::add_item(const Input_Item& item)
505475 }
506476}
507477
508- bool find_str (const std::vector<std::string>& strings, const std::string& strToFind)
509- {
510- auto it = std::find (strings.begin (), strings.end (), strToFind);
511- return it != strings.end ();
512- }
513-
514478std::string nofound_str (std::vector<std::string> init_chgs, const std::string& str)
515479{
516480 std::string warningstr = " The parameter " ;
0 commit comments