Skip to content

Commit c0a02e7

Browse files
committed
add case sensitivity support for searching
1 parent 3ffb0e1 commit c0a02e7

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

include/omp-logger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct IOmpLog
6969

7070
virtual uint32_t getColor() const = 0;
7171

72-
virtual PaginatedResult fetchLogs(int linesPerPage, int pageStart, const std::string& searchTerm) const = 0;
72+
virtual PaginatedResult fetchLogs(int linesPerPage, int pageStart, const std::string& searchTerm, bool caseSensitive) const = 0;
7373

7474
virtual bool log(AMX* amx, OmpLogger::ELogLevel level, StringView message) const = 0;
7575

omp-logger.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ native bool:Logger_Error(Logger:id, const format[], OPEN_MP_TAGS:...);
3333
native bool:Logger_Fatal(Logger:id, const format[], OPEN_MP_TAGS:...);
3434

3535
// Logs result
36-
native Logger_FetchLogs(playerid, Logger:logger, amount, pageStart, const callback[], const search[] = "");
36+
native Logger_FetchLogs(playerid, Logger:logger, amount, pageStart, const callback[], const search[] = "", bool:caseSensitive = true);
3737
native Logger_GetResult(LogsResult:result, row, logs[], size = sizeof logs);
38-
native Logger_FreeResult(LogsResult:result);
38+
native Logger_FreeResult(LogsResult:result);
39+
40+
// Cleanup results once they go out of scope
41+
stock operator~(const LogsResult:result[], len)
42+
{
43+
for (new i = 0; i < len; ++ i)
44+
{
45+
Logger_FreeResult(result[i]);
46+
}
47+
}

src/natives.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ SCRIPT_API(Logger_Fatal, bool(IOmpLog& logger, cell const* format))
6161
return logger.log(amx, OmpLogger::ELogLevel::Fatal, AmxStringFormatter(format, amx, GetParams(), 2));
6262
}
6363

64-
SCRIPT_API(Logger_FetchLogs, bool(IPlayer& player, IOmpLog& logger, int linesPerPage, int pageStart, std::string const& callback, std::string const& searchTerm))
64+
SCRIPT_API(Logger_FetchLogs, bool(IPlayer& player, IOmpLog& logger, int linesPerPage, int pageStart, std::string const& callback, std::string const& searchTerm, bool caseSensitive))
6565
{
6666
AMX* amx = GetAMX();
6767

68-
auto func = [&player, &logger, amx, callback, searchTerm, linesPerPage, pageStart]() {
68+
auto func = [&player, &logger, amx, callback, searchTerm, linesPerPage, pageStart, caseSensitive]() {
6969

7070
int funcIDX = 0;
7171
if (!amx_FindPublic(amx, callback.c_str(), &funcIDX))
7272
{
73-
PaginatedResult result = logger.fetchLogs(linesPerPage, pageStart, searchTerm);
73+
PaginatedResult result = logger.fetchLogs(linesPerPage, pageStart, searchTerm, caseSensitive);
7474
ILogsResult* logsResult = OmpLoggerComponent::Get()->initLogsResult(result.lines);
7575
amx_Push(amx, result.totalPages);
7676
amx_Push(amx, result.currentPage);

src/omp-log.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3746
long 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 ++;

src/omp-log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OmpLog final
3838

3939
bool log(OmpLogger::ELogLevel level, StringView message) const override;
4040

41-
PaginatedResult fetchLogs(int linesPerPage, int pageStart, const std::string& searchTerm) const override;
41+
PaginatedResult fetchLogs(int linesPerPage, int pageStart, const std::string& searchTerm, bool caseSensitive) const override;
4242

4343
OmpLog(StringView name, uint32_t color, OmpLogger::ELogLevel level, std::FILE* file);
4444

0 commit comments

Comments
 (0)