commit 0f03457b2ada8a46611132fc60d5410da1cae675 parent fd057f6a78083690082d7887e91f79d63222ced3 Author: ChanderG <[email protected]> Date: Mon, 1 Dec 2025 19:58:00 +0530 add line-by-line search with error handling Diffstat:
| M | grep.asd | | | 3 | ++- |
| M | grep.lisp | | | 15 | ++++++++++++++- |
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/grep.asd b/grep.asd @@ -9,7 +9,8 @@ :pathname-utils :vom :lparallel - :bordeaux-threads) + :bordeaux-threads + :cl-string-match) :build-operation "program-op" :build-pathname "cl-grep" :entry-point "cl-grep:main") diff --git a/grep.lisp b/grep.lisp @@ -1,11 +1,23 @@ (in-package :cl-grep) +(defparameter *match* nil) + (defparameter *queue* nil) (defun setup-tasks-mgmt () (setf *queue* (lq:make-queue))) (defun grep-file (file) - (lo "Grepping file: ~a" file)) + (lo "Grepping file: ~a" file) + (handler-case + (with-open-file (stream file) + (loop for line = (read-line stream nil :eof) + until (eq line :eof) collect + (if (sm:string-contains-brute *match* line) + (format t "~a: ~a ~%" file line)))) + (stream-error (stream) + (lo "Skipping binary file: ~a" file)) + (error (c) + (lo "Unknown error: ~a when reading file ~a" c file)))) (defun process-task () (loop @@ -42,6 +54,7 @@ ;; Main entrypoint for the grep functionality (defun grep-launcher (str dir) + (setf *match* str) (setup-tasks-mgmt) (let* ((numw 4) (workers (loop for i from 0 below numw