From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: nalaginrut Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Add "scandir" procedure Date: Sun, 28 Aug 2011 04:05:21 +0800 Organization: HFG Message-ID: <1314475521.3143.47.camel@Renee-desktop> Reply-To: NalaGinrut@gmail.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1314475543 3619 80.91.229.12 (27 Aug 2011 20:05:43 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 27 Aug 2011 20:05:43 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Aug 27 22:05:38 2011 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QxP8L-0005bI-GH for guile-devel@m.gmane.org; Sat, 27 Aug 2011 22:05:37 +0200 Original-Received: from localhost ([::1]:46969 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QxP8K-0002DB-M3 for guile-devel@m.gmane.org; Sat, 27 Aug 2011 16:05:36 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:48608) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QxP8H-0002Cu-3F for guile-devel@gnu.org; Sat, 27 Aug 2011 16:05:33 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QxP8F-00030u-Rs for guile-devel@gnu.org; Sat, 27 Aug 2011 16:05:33 -0400 Original-Received: from mail-gw0-f41.google.com ([74.125.83.41]:39128) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QxP8F-00030k-PL for guile-devel@gnu.org; Sat, 27 Aug 2011 16:05:31 -0400 Original-Received: by gwaa20 with SMTP id a20so4063373gwa.0 for ; Sat, 27 Aug 2011 13:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:from:reply-to:to:content-type:organization:date:message-id :mime-version:x-mailer:content-transfer-encoding; bh=+Sg5y3DlxIU8i5ZLPGA7PW+8INkmzhO42f9gA+5F6CI=; b=b/WJD++1+eQKP1BrERBrYUxYCSXUsUO8PpIxjuhPvhCmd+GgSiVuRzMClIGcOp9U92 TvD6ArzX7BJ5wmIdzPGrGksUSVhgaVsEPj/uJBaAVcAbtOV2Bzxon3q6PVLzmp+9hVoV Q0ye+Pnj1e//Nm5XUxkWcbKFp+Obh80FVjZGI= Original-Received: by 10.142.140.3 with SMTP id n3mr1337762wfd.218.1314475529714; Sat, 27 Aug 2011 13:05:29 -0700 (PDT) Original-Received: from [192.168.100.101] ([113.97.239.50]) by mx.google.com with ESMTPS id m1sm8736635pbf.3.2011.08.27.13.05.26 (version=SSLv3 cipher=OTHER); Sat, 27 Aug 2011 13:05:29 -0700 (PDT) X-Mailer: Evolution 2.28.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.83.41 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:12722 Archived-At: Hi guys! I found there isn't "scandir" in current Guile. And we may use "ftw" to instead. I guess "ftw" traverse all sub-directoies. Yes, we may use nftw to filter the level we don't need, but "ftw" seems always traverse all sub-directories. If my guess is correct, I believe it's too slow for someone, for a instance, me. :-) But the efficiency is not the case. The reason is that when I was trying to write a web server ,I need to print directory content in HTML back to the client. And I found "ftw" won't return "." and "..", moreover ,the result isn't sorted. And I think "scandir" which provided by POSIX is a better solution. Many dynamic language like PHP own a "scandir" implementation. I believe Guile should have one. So I wrote an implementation into filesys.c. There's a little difference of usage between C and Guile version. But it's trivial if you're familiar with the C version. The usage is like this: ------------------------------- scandir dir [filter [sort]] ------------------------------- "dir" must be a string which owned by a directory. You may run with one arg directly: ------------------ (scandir "mmr") ==>("." ".." "a" "b" "c" "dd" "dir1" "dir2") ------------------ As you see, it returned a list contained all contents of directory "mmr". So, you can do whatever you like with a list. And you may use filter: ------------------------ (scandir "mmr" (lambda (fname) (if (string-contains fname "dir") #f #t))) ==> ("." ".." "a" "b" "c" "dd") ------------------------ It's easy to filter the file you want. Return #f will skip current file, #t will add it to the result list. And the third arg "sort" has only two possibility: 'asort stands for alphasort or 'vsort stands for versionsort Call "scandir" with "sort" unspecified you'll get 'asort in default. If you can't understand that, you may checkout the C version manual: ---------------- man 3 scandir ---------------- Besides, there's a clumsy documentation in the REPL: ------------- ,d scandir ------------- PS: There must be some bugs in this implementation, like portable problem. I believe I didn't add some critical macro in it. But I think there're lots of people know more than me in the mail-list. So I submit it, and waiting for any advices or patches of patch. :-) Anyway, I've tested it. It's function complete. Use it as you like! My patch will be submitted in the next mail, I found some guy(like me) doesn't like to open an attachment of obscure origin. ;-) -- GNU Powered it GPL Protected it GOD Blessed it HFG - NalaGinrut --hacker key-- v4sw7CUSMhw6ln6pr8OSFck4ma9u8MLSOFw3WDXGm7g/l8Li6e7t4TNGSb8AGORTDLMen6g6RASZOGCHPa28s1MIr4p-x hackerkey.com ---end key---