guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I/O, modules


From: tantalum
Subject: Re: I/O, modules
Date: Tue, 13 Nov 2012 12:45:49 +0100

Hi

The (ice-9 streams) module could be interesting, too.
http://www.gnu.org/software/guile/manual/html_node/Streams.html

Example:
-------------
(import
  (ice-9 streams)
  (ice-9 rdelim))

(define (port->line-stream port handle-delim)
  (port->stream port (lambda (port) (read-line port handle-delim))))

(define (file-upcase source-path target-path)
  (call-with-input-file source-path
    (lambda (source-file)
      (call-with-output-file target-path
        (lambda (target-file)
          (stream-for-each
            (lambda (line) (display (string-upcase line) target-file))
            (port->line-stream source-file (quote concat))))))))


;test
(system "echo test >/tmp/file-upcase-test")
(file-upcase "/tmp/file-upcase-test" "/tmp/file-upcase-test-out")
--------------
Where
  - Handle-delim specifies what should happen with the newline character. 
Default is to remove it
  - And display is Schemes standard string write procedure

I don't know of any existing generic file to string procedure.
Something similar to what I have used is:
---------------
(import
  (ice-9 streams)
  (ice-9 rdelim))

(define (port->line-stream port handle-delim)
  (port->stream port (lambda (port) (read-line port handle-delim))))

(define (file->string file)
  (apply string-append
    (stream->list (port->line-stream file (quote concat)))))

(define (file-from-path->string path)
  (call-with-input-file path file->string))

(display (file-from-path->string "/home/jkalbhenn/temp/temp.scm"))
-------------

Julian



reply via email to

[Prev in Thread] Current Thread [Next in Thread]