From: Matt
Subject: slurp-file
Date: 
Message-ID: <x9qDf.8885$vU2.2348@newsread3.news.atl.earthlink.net>
Warning: typos may follow...

(defun slurp-file (name)
   "Slurps up file <name> and returns the data as a string."
   (let ((data nil))
     (with-open-file (file name :direction :input)
       (let ((len (file-length file)))
         (setf (data (make-array len :element-type 'character
                                 :fill-pointer 0 :adjustable t))
         (read-sequence data file :start 0 :end len)))
     (values data))) ; ?


Hi, I'm new and I'm trying to load a file into a string and
return it, but this just returns an empty string.  I guess
the array is going out of scope?  How should I write this?

Thanks for any help!
Matt

From: ············@gmail.com
Subject: Re: slurp-file
Date: 
Message-ID: <1138635311.154594.321120@g14g2000cwa.googlegroups.com>
It works if you use
(setf data (make-array len :element-type 'character))
instead of
(setf data (make-array len :element-type 'character
                                 :fill-pointer 0 :adjustable t))
[you wrote "setf (data", but I imagine the extra parenthesis was not
present in the code you tested]

Googling for "slurp file common lisp" returns a couple of useful
pointers.
From: Matt
Subject: Re: slurp-file
Date: 
Message-ID: <nLqDf.8895$vU2.1093@newsread3.news.atl.earthlink.net>
············@gmail.com wrote:
> It works if you use
> (setf data (make-array len :element-type 'character))
> instead of
> (setf data (make-array len :element-type 'character
>                                  :fill-pointer 0 :adjustable t))
> [you wrote "setf (data", but I imagine the extra parenthesis was not
> present in the code you tested]

Yeah, that was one of those typos I mentioned.  My internet
computer is trapped in a lexical fence :-)

> Googling for "slurp file common lisp" returns a couple of useful
> pointers.

Thanks, I should have thought of that, but I'm braindead sometimes.

Thanks!
Matt
From: z0d
Subject: Re: slurp-file
Date: 
Message-ID: <1138642081.222969.110380@g14g2000cwa.googlegroups.com>
Matt wrote:
>
> Hi, I'm new and I'm trying to load a file into a string and
> return it, but this just returns an empty string.  I guess
> the array is going out of scope?  How should I write this?

Maybe it will be interesting for you:
http://www.emmett.ca/~sabetts/slurp.html
From: Matt
Subject: Re: slurp-file
Date: 
Message-ID: <p%vDf.2843$Nv2.263@newsread1.news.atl.earthlink.net>
z0d wrote:
> Matt wrote:
>>
>> Hi, I'm new and I'm trying to load a file into a string and
>> return it, but this just returns an empty string.  I guess
>> the array is going out of scope?  How should I write this?
> 
> Maybe it will be interesting for you:
> http://www.emmett.ca/~sabetts/slurp.html

Yeah, I saw that one, thanks.  Looking at the last version on
there, I guess I don't need the call to (values) at the end.  I
guess I knew that, but damn!  It's hard to remember all the
rules sometimes! :-)

Matt
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: slurp-file
Date: 
Message-ID: <87bqxtgxn6.fsf@qrnik.zagroda>
"z0d" <·············@gmail.com> writes:

> Maybe it will be interesting for you:
> http://www.emmett.ca/~sabetts/slurp.html

The last version doesn't work correctly in environments where the
conversion from bytes to characters changes the length, like newline
translation or UTF-8 decoding.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Wade Humeniuk
Subject: Re: slurp-file
Date: 
Message-ID: <HrqDf.164146$km.131228@edtnps89>
Matt wrote:
> Warning: typos may follow...
> 
> (defun slurp-file (name)
>   "Slurps up file <name> and returns the data as a string."
>   (let ((data nil))
>     (with-open-file (file name :direction :input)
>       (let ((len (file-length file)))
>         (setf (data (make-array len :element-type 'character
>                                 :fill-pointer 0 :adjustable t))
>         (read-sequence data file :start 0 :end len)))
>     (values data))) ; ?
> 
> 
> Hi, I'm new and I'm trying to load a file into a string and
> return it, but this just returns an empty string.  I guess
> the array is going out of scope?  How should I write this?
> 
> Thanks for any help!
> Matt

Change :fill-pointer 0 to :fill-pointer t in the code.  With
fill-pointer 0 the length is essentially 0 (empty string).

Wade
From: Matt
Subject: Re: slurp-file
Date: 
Message-ID: <SOqDf.8897$vU2.3492@newsread3.news.atl.earthlink.net>
Wade Humeniuk wrote:
> Matt wrote:
>
>> Hi, I'm new and I'm trying to load a file into a string and
>> return it, but this just returns an empty string.  I guess
>> the array is going out of scope?  How should I write this?
>>
>> Thanks for any help!
>> Matt
> 
> 
> Change :fill-pointer 0 to :fill-pointer t in the code.  With
> fill-pointer 0 the length is essentially 0 (empty string).
> 
> Wade

Thanks, that worked!  I thought it said that read-sequence
acted like vector-push-extend in that it increments the fill
pointer as it reads, but I guess I was thinking of something
else.

Thanks!
Matt
From: Wade Humeniuk
Subject: Re: slurp-file
Date: 
Message-ID: <NZqDf.164322$km.36566@edtnps89>
Matt wrote:
> Wade Humeniuk wrote:
>> Matt wrote:
>>
>>> Hi, I'm new and I'm trying to load a file into a string and
>>> return it, but this just returns an empty string.  I guess
>>> the array is going out of scope?  How should I write this?
>>>
>>> Thanks for any help!
>>> Matt
>>
>>
>> Change :fill-pointer 0 to :fill-pointer t in the code.  With
>> fill-pointer 0 the length is essentially 0 (empty string).
>>
>> Wade
> 
> Thanks, that worked!  I thought it said that read-sequence
> acted like vector-push-extend in that it increments the fill
> pointer as it reads, but I guess I was thinking of something
> else.
> 

You are probably thinking of FORMAT.

Wade
From: Matt
Subject: Re: slurp-file
Date: 
Message-ID: <w9rDf.2716$Nv2.2011@newsread1.news.atl.earthlink.net>
I wound up writing it like this because I guess MS-DOS newlines
screw up the file size and makes the array length longer than
the data that was really read in.  I'm not really sure, but if I
turn around and write the data to another file, it leaves a bunch
of garbage at the end unless I explicitly set the fill pointer
with the return value of read-sequence.

(defun slurp-file (name)
   "Slurps up file <name> and returns the data as a string."
   (let ((data nil))
     (with-open-file (file name :direction :input)
       (setf data (make-array (file-length file) :element-type 'character
                               :fill-pointer t :adjustable t))
       (setf (fill-pointer data) (read-sequence data file))
     (values data))))

(defun spew-data (data name)
   "Spews string <data> to file <name>."
   (with-open-file (file name :direction :output)
     (write-sequence data file)))

(defun copy-file (src dest)
   (let ((data (slurp-file src)))
     (spew-data data dest)))