[plt-scheme] ignoring metadata

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sun Dec 9 21:36:23 EST 2007

On Dec 9, 2007 8:30 PM, Prabhakar Ragde <plragde at uwaterloo.ca> wrote:
> Robby Findler wrote:
> > You can do (read-line) three times and throw away the results (but
> > there may be a different number of lines in some future version
> > (hopefully not ...) so I'd suggest doing some checking, like looking
> > for ";" at the start of the first two and "#reader" at the start of
> > the third).
>
> I was hoping to avoid that, because I don't know for sure if the file
> will have the metadata or not. I can't create a "no-op" reader of some
> sort? --PR

I would probably just do something like this (note: untested code! Let
me know if you have questions):

(require (lib "port.ss"))

;; skip-metadata : port -> void
;; skips over drs v371 save-file metadata, if it is present
(define (skip-metadata port)
  (let ([peek-port (peeking-input-port port)]
        [line1 (read-line peek-port)]
        [line2 (read-line peek-port)]
        [line3 (read-line peek-port)])
    (when (and (string? line1)
               (string? line2)
               (string? line3)
               (regexp-match #rx"^;" line1)
               (regexp-match #rx"^;" line2)
               (regexp-match #rx"^#reader" line3))
      (read-line port)
      (read-line port)
      (read-line port))))

(call-with-input-file "file.ss"
  (λ (port)
    (skip-metadata port)
    ...whatever you were going to do...))

Posted on the users mailing list.