|
Prev: Brand Watches Bell & Ross Men's Watches Geneva 126 G126WTE-SL Discount, Swiss, Fake
Next: ANN: SymbolicWeb v0.1 (quite alpha) w/ source code this time
From: bufie on 25 Apr 2008 02:18 I've got a situation where my code needs to run in two different environments, one of which supports arguments to with-open-file that are not supported on the other. I'd like to have a simple check where I can use the same call, but with the additional arguments (part of the plist) in the second environment. For example, the simpler environment might use a call similar to: (with-open-file (o :direction :output) .... ) and the second would add something like: (with-open-file (o :direction :output :encoding :iso8859-1) .... ) The first env won't work with the extra parameters. What seems to be the ideal situation would be to build the plist and then just use that as the argument, but I haven't been able to make it work. i.e. something like: (let ((my-args (list :direction :output)) (when env2 ;; test which env we're using (push :iso8859-1 myargs) (push :encoding myargs)) (with-open-file (o myargs) .... ) Which doesn't work, of course, since myargs is a list which is not expected by with-open-file. I thought I had seen a method for expanding the plist into its components, but I haven't been able to find it again. Any thoughts or ideas would be greatly appreciated. Thanks! bufie
From: Pascal Bourguignon on 25 Apr 2008 02:37
bufie <bufie(a)spamneggs.com> writes: > I've got a situation where my code needs to run in two different > environments, one of which supports arguments to with-open-file that > are not supported on the other. > > I'd like to have a simple check where I can use the same call, but > with the additional arguments (part of the plist) in the second > environment. > > For example, the simpler environment might use a call similar to: > > (with-open-file (o :direction :output) > .... ) > > and the second would add something like: > > (with-open-file (o :direction :output :encoding :iso8859-1) > .... ) > > The first env won't work with the extra parameters. (with-open-file (o :direction :output #+sbcl :encoding #+scbl :iso8859-1 #+clisp :external-format #+clisp charset:iso-8859-1) ... ) > What seems to be the ideal situation would be to build the plist and > then just use that as the argument, but I haven't been able to make it > work. i.e. something like: > > (let ((my-args (list :direction :output)) > (when env2 ;; test which env we're using > (push :iso8859-1 myargs) > (push :encoding myargs)) > (with-open-file (o myargs) > .... ) > > Which doesn't work, of course, since myargs is a list which is not > expected by with-open-file. I thought I had seen a method for > expanding the plist into its components, but I haven't been able to > find it again. > > Any thoughts or ideas would be greatly appreciated. Otherwise, you can re-implement a do-open-file function similar to the macro, something like: (defun do-open-file (thunk path &rest keys &key &allow-other-keys) (let ((stream (apply (function open) path keys))) ;; missing error processing! (unwind-protect (funcall thunk stream) (close stream)))) (apply (function do-open-file) (lambda (stream) ...) file :direction :output other-args) -- __Pascal Bourguignon__ http://www.informatimago.com/ WARNING: This product attracts every other piece of matter in the universe, including the products of other manufacturers, with a force proportional to the product of the masses and inversely proportional to the distance between them. |