Subject: Re: file to string conversion
From: Erik Naggum <>
Date: 1998/12/23
Newsgroups: comp.lang.lisp
Message-ID: <>

* Pierre Mai <>
| READ-SEQUENCE is probably one of the best ways, if you want to stay
| portable.  I.e. something along the following lines:
| (defun get-file-as-string (filename)
|   "Returns a string with the content of the file `filename'."
|   (with-open-file (stream filename)
|     (read-sequence (make-string (file-length stream)) stream)))
| For production-quality code, you should check some of the semantics of
| the interaction between ELEMENT-TYPE for OPEN/WITH-OPEN-FILE and
| MAKE-STRING (I'm currently not sure, whether the standard guarantees
| that ELEMENT-TYPE defaults to a type that's "compatible" with STRING.

  you might also want to check out the return value from READ-SEQUENCE.

| You'd also probably not use MAKE-STRING, since MAKE-STRING initializes 
| it's result, which causes unnecessary cache pollution...

  well, it would if it did, but MAKE-STRING isn't required to initialize
  the allocated vector.  in particular, the `initial-element' argument's
  default value is implementation-dependent.  in Allegro CL 5.0, it is not
  initialized.  it can be quite an interesting read, actually.  (this
  taught me to zero out temporary strings used in functions with sensitive
  data -- I never thought I'd have to do _that_ in Common Lisp.)

  (I have written a SETF'able FILE-CONTENTS function that I use a lot, but
  my lawyer is working on the license stuff that should accompany all the
  code I want to publish.  all the licenses I have seen basically focus on
  making it easy to give something away, but very hard to accept it.  I
  want a license that makes it very easy to accept it in the simplest case
  (run it, use it, don't modify it, and don't re-distribute it, tell people
  where to find it), with options to get wider licenses (such as giving or
  licensing to me modifications of any kind, so the _single_ maintained
  version can be improved, and such that those who redistribute are
  required to accept all newer versions in addition to or replacing older
  versions, etc).  if anybody knows of a license that grants something like
  this (more details upon request), let me know -- it could save me a _lot_
  of lawyer fees.  both GNU GPL and LGPL are completely useless.  we're
  investigating the BSD license.)

  Nie wieder KrF!  Nie wieder KrF!  Nie wieder KrF!  Nie wieder KrF!