@@ -73,9 +73,33 @@ bool ReadEachLineFromTextFile(const std::string &filename, const std::function<v
7373 try {
7474 ifstream f (filename);
7575 if (f) {
76- std::string line;
77- while (std::getline (f, line))
78- line_handle_func (line);
76+ std::string text;
77+ while (std::getline (f, text)) {
78+ while (!text.empty () && (text.back () == ' \r ' || text.back () == ' \n ' ))
79+ text.pop_back ();
80+ line_handle_func (text);
81+ }
82+ return true ;
83+ } else {
84+ LogWarn (" open failed, %s" , filename.c_str ());
85+ }
86+ } catch (const exception &e) {
87+ LogWarn (" catch exception: %s" , e.what ());
88+ }
89+ return false ;
90+ }
91+
92+ bool ReadAllLinesFromTextFile (const std::string &filename, std::vector<std::string> &text_vec)
93+ {
94+ try {
95+ ifstream f (filename);
96+ if (f) {
97+ std::string text;
98+ while (std::getline (f, text)) {
99+ while (!text.empty () && (text.back () == ' \r ' || text.back () == ' \n ' ))
100+ text.pop_back ();
101+ text_vec.emplace_back (std::move (text));
102+ }
79103 return true ;
80104 } else {
81105 LogWarn (" open failed, %s" , filename.c_str ());
@@ -86,14 +110,14 @@ bool ReadEachLineFromTextFile(const std::string &filename, const std::function<v
86110 return false ;
87111}
88112
89- bool ReadAllLinesFromTextFile (const std::string &filename, std::vector<std:: string> &lines )
113+ bool ReadFirstLineFromTextFile (const std::string &filename, std::string &text )
90114{
91115 try {
92116 ifstream f (filename);
93117 if (f) {
94- std::string line ;
95- while (std::getline (f, line ))
96- lines. emplace_back ( std::move (line) );
118+ std::getline (f, text) ;
119+ while (!text. empty () && (text. back () == ' \r ' || text. back () == ' \n ' ))
120+ text. pop_back ( );
97121 return true ;
98122 } else {
99123 LogWarn (" open failed, %s" , filename.c_str ());
0 commit comments