MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / searchFile

Function searchFile

app/src/AI/FileSandbox.cpp:462–494  ·  view source on GitHub ↗

* @brief Scans one text file for a needle, appending bounded line-level hits. */

Source from the content-addressed store, hash-verified

460 * @brief Scans one text file for a needle, appending bounded line-level hits.
461 */
462static void searchFile(const QString& path,
463 const QString& root,
464 const QRegularExpression& needle,
465 QJsonArray& hits,
466 bool& truncated)
467{
468 QFile file(path);
469 if (!file.open(QIODevice::ReadOnly))
470 return;
471
472 if (looksBinary(file.peek(AI::FileSandbox::kBinarySniffBytes)))
473 return;
474
475 QTextStream stream(&file);
476 int lineNo = 0;
477 while (!stream.atEnd()) {
478 const auto line = stream.readLine();
479 ++lineNo;
480 if (!needle.match(line).hasMatch())
481 continue;
482
483 if (hits.size() >= AI::FileSandbox::kMaxSearchHits) {
484 truncated = true;
485 return;
486 }
487
488 QJsonObject row;
489 row[QStringLiteral("file")] = displayPath(path, root);
490 row[QStringLiteral("line")] = lineNo;
491 row[QStringLiteral("text")] = line.left(400);
492 hits.append(row);
493 }
494}
495
496/**
497 * @brief Walks the read roots, collecting files to search within the scan cap.

Callers 1

searchMethod · 0.85

Calls 7

looksBinaryFunction · 0.85
displayPathFunction · 0.85
openMethod · 0.45
peekMethod · 0.45
atEndMethod · 0.45
sizeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected