From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Nordl=F6w?= Newsgroups: gmane.emacs.help Subject: Re: Efficiently checking the initial contents of a file Date: Fri, 16 May 2008 05:52:40 -0700 (PDT) Organization: http://groups.google.com Message-ID: <36d03408-0373-4e0e-8d59-ea540ce55119@p25g2000hsf.googlegroups.com> References: <5f8b772a-e2ed-468c-89b3-2d9e40ed132b@m3g2000hsc.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1210945343 14890 80.91.229.12 (16 May 2008 13:42:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 16 May 2008 13:42:23 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri May 16 15:43:00 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Jx0Bn-000563-Nm for geh-help-gnu-emacs@m.gmane.org; Fri, 16 May 2008 15:41:39 +0200 Original-Received: from localhost ([127.0.0.1]:53681 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Jx0B4-0004u8-Ae for geh-help-gnu-emacs@m.gmane.org; Fri, 16 May 2008 09:40:54 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!p25g2000hsf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 56 Original-NNTP-Posting-Host: 150.227.15.253 Original-X-Trace: posting.google.com 1210942361 8912 127.0.0.1 (16 May 2008 12:52:41 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 16 May 2008 12:52:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p25g2000hsf.googlegroups.com; posting-host=150.227.15.253; posting-account=ytJKAgoAAAA1tg4ScoRszebXiIldA5vg User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en; rv:1.8.1.14) Gecko/20080418 Epiphany/2.20 Firefox/2.0.0.14,gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 netcache (NetCache NetApp/6.1.1RC1) Original-Xref: news.stanford.edu gnu.emacs.help:158699 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:54069 Archived-At: On 16 Maj, 13:12, David Hansen wrote: > On Fri, 16 May 2008 03:16:13 -0700 (PDT) Nordl=F6w wrote: > > > How can I efficiently using pure emacs-lisp (without calling any > > external process) investigate the first bytes of a file? > > > My guess is > > - Open parts of the file into a buffer or string. > > - Alt 1. Switch to the buffer and do things. > > (with-temp-buffer > ;; Read the first 42 characters (not bytes) into the temp buffer. > (insert-file-contents filename nil 0 42) > ;; Do whatever you want to do here. > ) > > David Great! Thanks for all the help! I wanted a quick way of discarding ELFs from my tags-query-replace() operations. This is the result of my coding. Is it ok to use string-width() and looking-at() if don't care about different string encodings, that is I just want to compare binary byte- arrays? ;; For additional speed you can use ;; `insert-file-contents-literally', if you don't need code ;; conversions, decompression, etc. (defun file-begin-p (filename beg) "Determine if FILENAME begins with BEG." (interactive "fFile to investigate: ") (if (and (file-exists-p filename) (file-readable-p filename)) (with-temp-buffer (let ((width (string-width beg))) (insert-file-contents-literally filename nil 0 width) (looking-at beg) )))) ;; TEST: (file-begin-p "/bin/ls" "=7FELF") (defun file-begin-ELF-p (filename) "Return non-nil if FILENAME is an ELF (Executable and Linkable Format)" (interactive "fFile to investigate: ") (file-begin-p filename "=7FELF") ) ;; TEST: (file-begin-ELF-p "/bin/ls") Thanks again, Nordl=F6w