Subject: Re: loading data files
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 16 Nov 2007 21:32:26 -0600
Newsgroups: comp.lang.lisp
Message-ID: <vJOdnQDI1ZBX_aPanZ2dnUVZ_gWdnZ2d@speakeasy.net>
Andy Chambers  <achambers.home@googlemail.com> wrote:
+---------------
| Alan Crowe <a...@cawtech.freeserve.co.uk> wrote:
| > Andy Chambers <achambers.h...@googlemail.com> writes:
| > > I've defined a bunch of macros so that my program can load its data-
| > > structures by just calling `load' on a file that has something like
| > >    (item 1 "test" :a 1 :b 2)
| > >    (item 2 "test-2" :a 1 :b 2)
| > > This works fine but now I'm thinking about how to handle "untrusted"...
...
| > LOAD merely works through a file, reading and evaling each
| > form in turn, so you can write your own load that walks the
| > executable form built by read before evaling it.
| 
| I thought that might be the case.  Didn't realize how easy it would be
| though.
+---------------

Might even be something as simple as this [though with ITEM
being a *function* in this case, rather than a macro!]:

    (defun my-data-load (file)
      (with-open-file (s file)
	(let ((*read-eval* nil))
	  (loop with eof = (list nil)
		for form = (read s nil eof)
		until (eq form eof) do
	    (if (and (consp form) (eq (car form) 'item))
	      (apply #'item (cdr form))
	      (error "Bad form in input: ~s" form))))))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607