@@ -34,6 +34,15 @@ std::FILE* OmpLog::getFile() const
3434 return file_;
3535}
3636
37+ bool caseInsensitiveCompare (const std::string& str1, const std::string& str2)
38+ {
39+ std::string lowerStr1 = str1;
40+ std::string lowerStr2 = str2;
41+ std::transform (lowerStr1.begin (), lowerStr1.end (), lowerStr1.begin (), ::tolower);
42+ std::transform (lowerStr2.begin (), lowerStr2.end (), lowerStr2.begin (), ::tolower);
43+ return lowerStr1.find (lowerStr2) != std::string::npos;
44+ }
45+
3746long long countTotalLines (std::FILE* file)
3847{
3948 if (file == nullptr )
@@ -55,7 +64,7 @@ long long countTotalLines(std::FILE* file)
5564 return lineCount;
5665}
5766
58- long long countMatchingLines (std::FILE* file, const std::string& searchTerm)
67+ long long countMatchingLines (std::FILE* file, const std::string& searchTerm, bool caseSensitive )
5968{
6069 if (file == nullptr )
6170 {
@@ -69,17 +78,27 @@ long long countMatchingLines(std::FILE* file, const std::string& searchTerm)
6978 while (std::fgets (buffer, sizeof (buffer), file) != nullptr )
7079 {
7180 std::string line (buffer);
72- if (line.find (searchTerm) != std::string::npos)
81+ if (caseSensitive)
82+ {
83+ if (line.find (searchTerm) != std::string::npos)
84+ {
85+ lineCount ++;
86+ }
87+ }
88+ else
7389 {
74- lineCount ++;
90+ if (caseInsensitiveCompare (line, searchTerm))
91+ {
92+ lineCount ++;
93+ }
7594 }
7695 }
7796
7897 std::fseek (file, 0 , SEEK_SET);
7998 return lineCount;
8099}
81100
82- PaginatedResult OmpLog::fetchLogs (int linesPerPage, int pageStart, const std::string& searchTerm) const
101+ PaginatedResult OmpLog::fetchLogs (int linesPerPage, int pageStart, const std::string& searchTerm, bool caseSensitive ) const
83102{
84103 PaginatedResult result;
85104
@@ -90,7 +109,7 @@ PaginatedResult OmpLog::fetchLogs(int linesPerPage, int pageStart, const std::st
90109
91110 if (!searchTerm.empty ())
92111 {
93- result.totalPages = (countMatchingLines (file_, searchTerm) + linesPerPage - 1 ) / linesPerPage;
112+ result.totalPages = (countMatchingLines (file_, searchTerm, caseSensitive ) + linesPerPage - 1 ) / linesPerPage;
94113 }
95114 else
96115 {
@@ -114,9 +133,20 @@ PaginatedResult OmpLog::fetchLogs(int linesPerPage, int pageStart, const std::st
114133 while (currentLine < startLine && ::fgets (buffer, sizeof (buffer), file_) != nullptr )
115134 {
116135 std::string line (buffer);
117- if (line.find (searchTerm) != std::string::npos)
136+
137+ if (caseSensitive)
138+ {
139+ if (line.find (searchTerm) != std::string::npos)
140+ {
141+ currentLine ++;
142+ }
143+ }
144+ else
118145 {
119- currentLine ++;
146+ if (caseInsensitiveCompare (line, searchTerm))
147+ {
148+ currentLine ++;
149+ }
120150 }
121151 }
122152 }
@@ -140,7 +170,8 @@ PaginatedResult OmpLog::fetchLogs(int linesPerPage, int pageStart, const std::st
140170 return !std::isspace (ch);
141171 }).base (), line.end ());
142172
143- if (searchTerm.empty () || line.find (searchTerm) != std::string::npos)
173+ bool caseSensitiveResult = caseSensitive ? line.find (searchTerm) != std::string::npos : caseInsensitiveCompare (line, searchTerm);
174+ if (searchTerm.empty () || caseSensitiveResult)
144175 {
145176 result.lines .push_back (line);
146177 linesRead ++;
0 commit comments