TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on March 29, 2005, 12:45:45 PM
-
Write a function that, given a valid file name, returns all the lines that contain the search string.
example:
(defun return-found (file_name search_string)
(foreach line-in-file
(if line-has-search-string
...
)
)
output-lines-that-match
)
No time limit, post it if ya got it, or when you get it.
-
Suggestion: Add a flag to the function declaration so that the function can perform case sensitive / insensitive searches.
i.e.
(defun FileScan ( fileName scanText caseFlag )
...
)
Where caseFlag would be nil or non nil.
-
Good idea MP. Let's include that as bonus points.
-
So it's ok to post a quick and dirty little solution? I thought (at one time at band camp) that you or se7en had everyone defer posting their solutions until 'x' time was up. Was I dreamin'?
-
No you were not dreaming. I gave up on that idea because even after a 24 hour period it was still the same folks posting to these challenges. So, yes it's ok to post what you have, let's see your "quick and dirty" solution. :)
-
Well ... unless I'm missing something fundamentally all that's required is --
(vl-remove-if-not
'(lambda (string) (wcmatch string pattern))
listOfStrings
)
but in the interests of reusable / convenience functions perhaps this --
(defun FileToList ( filename / handle result )
;;================================================================
;;
;; FileToList ( filename )
;;
;; If the specified file can be opened as input return the file
;; as an ordered list of strings.
;;
;; © 199?-2005 Michael Puckett
;;
;;================================================================
;;
;; e.g. (FileToList (findfile "acad.mnl"))
;;
;; (
;;
;; (";;; ACAD.MNL")
;; (";;; Copyright (C) 1992 - 1997 by Autodesk, Inc.")
;; (";;;")
;; (";;; Permission to use, copy, modify, and dis...")
;; (";;; for any purpose and without fee is hereb...")
;; (";;; that the above copyright notice appears ...")
;; (";;; that both that copyright notice and the ...")
;; (";;; restricted rights notice below appear in...")
;; (";;; documentation.")
;;
;; ...
;;
;; )
;;
;;================================================================
(cond
( (and
(eq 'str (type filename))
(setq handle (open filename "r"))
)
(while (setq stream (read-line handle))
(setq result
(cons
stream
result
)
)
)
(close handle)
(reverse result)
)
)
)
and this --
(defun WCMatchFile ( filename pattern caseSensitive / foo i )
;;================================================================
;;
;; WCMatchFile ( filename pattern caseSensitive )
;;
;; Return a list of all lines in a file that match the pattern.
;;
;; © 2005 Michael Puckett
;;
;;================================================================
;;
;; Using the format of --
;;
;; ( (LineNumber "The text ...")
;; (LineNumber "More text ...")
;; ...
;; )
;;
;; e.g. (WCMatchFile (findfile "acad.mnl") "*defun*" nil)
;;
;; (
;;
;; (32 "[defun ai_tiledvp_chk [new]")
;; (52 "[defun ai_tiledvp [num ori]")
;; (119 "[defun ai_popmenucfg [param]")
;; (127 "[defun ai_putmenucfg [param cfgval]")
;; (131 "[defun *merr* [msg]")
;; (137 "[defun *merrmsg* [msg]")
;; (144 "[defun ai_showedge_alert []")
;; (149 "[defun ai_hideedge_alert []")
;; (154 "[defun ai_viewports_alert []")
;; (160 "[defun ai_refedit_alert []")
;; (166 "[defun ai_support_assistance_alert []")
;; (171 "[defun la_support_assistance_alert []")
;;
;; ...
;;
;; )
;;
;;================================================================
(cond
( caseSensitive
(defun foo ( pair )
(wcmatch (cadr pair) pattern)
)
)
( (defun foo ( pair )
(wcmatch (strcase (cadr pair)) pattern)
)
(setq pattern (strcase pattern))
)
)
(setq i 0)
(vl-remove-if-not 'foo
(mapcar
'(lambda (stream) (list (setq i (1+ i)) stream))
(FileToList filename)
)
)
)
would be of use.
Nominally tested.
:)
-
Here's a real simple, albeit limited version.
(defun FindWord (
; ARGUMENTS
fn ; valid file name
word ; word to find
/
; LOCAL VARIABLES
fo line search found
)
;; Simple word finder.
;;
;; searches the given file(fn) for 'word' and returns
;; a list of the the line(s) that match.
(setq fo (open fn "r"))
(while (setq line (read-line fo))
(if (vl-string-search word line)
(setq found (cons line found))
)
)
(close fo)
found
)
-
Nice and compact Mark. :)
-
Borrowing code from MP, this is my attempt.
(defun WCMatchFile (filename pattern caseSensitive / handle result idx)
(setq idx 0)
(and
(eq 'str (type filename))
(or (wcmatch filename "*\\*")
(setq filename (findfile filename))
)
(setq handle (open filename "r"))
(while (setq idx (1+ idx) stream (read-line handle))
(if (or
(and caseSensitive
(wcmatch stream pattern))
(and (not caseSensitive)
(wcmatch (strcase stream) (strcase pattern)))
)
(setq result (cons (list idx stream) result))
)
T
)
(close handle)
)
(reverse result)
)
You can feed it this
(WCMatchFile (findfile "acad.mnl") "*defun*" nil)
or this:
(WCMatchFile "acad.mnl" "*defun*" nil)
-
Two more variations.
(defun FindWordC (
; ARGUMENTS
fn ; valid file name
word ; word to find
case ; switch, T turns off case matching
/
; LOCAL VARIABLES
fo line search found
)
;; Simple word finder.
;;
;; searches the given file(fn) for 'word' and returns
;; a list of the the line(s) that match.
;; If 'case' is T then preform case insensitivity search
;;
;; Example
;; (FindWordC <filename> "word" T)
;; will match 'word', 'WORD', or 'Word'
(setq fo (open fn "r"))
(while (setq line (read-line fo))
(cond
(case
(if (vl-string-search word (strcase line T))
(setq found (cons line found))
)
)
(T
(if (vl-string-search word line)
(setq found (cons line found))
)
)
)
)
(close fo)
found
)
(defun FindPat (
; ARGUMENTS
fn ; valid file name
pattern ; 'wcmatch' pattern
/
; LOCAL VARIABLES
fo line search found
)
;; Pattern finder.
;;
;; searches the given file(fn) for 'wcmatch' pattern
;; and returns a list of the the line(s) that match.
(setq fo (open fn "r"))
(while (setq line (read-line fo))
(if (wcmatch line pattern)
(setq found (cons line found))
)
)
(close fo)
found
)