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

Sample code for I/O ideas



I've put together examples of how a unix cat function would look like in
the current scheme and each of the proposed schemes.

I've an eof? method that takes a port as an argument rather than the
return value of get/peek as it suits this example better

cat-cur:
For simple I/O I find the current scheme is the best, it has the
shortest most understandable code

cat-ex:
The condition signalling scheme is my next favourite for simple code,
but it seems the best for complex code (such as where there are
multiple I/O calls in a block or where restarts are desirable, cat was a
good candidate for using restarts for each file change)

cat-nl:
I don't like the ending with a new line scheme as the return value of
gets has to be assigned to a variable and its length checked before
using it, with the previous two methods the return value of gets can be
used directly

cat-tup:
Similar to the new line scheme, the return values from gets needs to be
assigned and checked before they can be used

My preference is leaning toward using exceptions, it'll be great if both
that and the current scheme can work together, I don't know how that'll
work as you add more I/O methods such as block reads and writes or even
asynchronous I/O, will methods be written for both schemes or will one
take precedence?

(use goo)

(dc <end-of-file> (<condition>))

(dv *eof* -1)
(dv *not-eof* 0)

(dm eof? (port|<port> => <log>)
  (= (as <int> (peek port)) 255) )
  ; (eof-object? (peek port))

(dm gets-ex (port|<port> => <str>)
  (if (eof? port)
    (sig (new <end-of-file>))
    (gets port) ) )

(dm gets-nl (port|<port> => <str>)
  (if (eof? port)
    ""
    (cat (gets port) "\n") ) )

(dm gets-tup (port|<port> => <str>)
  (if (eof? port)
    (tup *eof* "")
    (tup *not-eof* (gets port)) ) )

(dm cat-cur (files|...)
  (for ((file files))
    (def port (open <file-in-port> file))
    (rep loop ()
      (unless (eof? port)
        (post "%s\n" (gets port)) (loop) ) )
    (close port) ) )

(dm cat-ex (files|...)
  (for ((file files))
    (def port (open <file-in-port> file))
    (esc break
      (try <end-of-file> (fun (c r) (close port) (break #f))
        (rep loop()
          (post "%s\n" (gets-ex port)) (loop) ) ) ) ) )

(dm cat-nl (files|...)
  (for ((file files))
    (def port (open <file-in-port> file))
    (rep loop()
      (def line (gets-nl port))
      (unless (= (len line) 0)
        (post line) (loop) ) )
    (close port) ) )

(dm cat-tup (files|...)
  (for ((file files))
    (def port (open <file-in-port> file))
    (rep loop()
      (def (tup status line) (gets-tup port))
      (unless (= status *eof*)
        (post "%s\n" line) (loop) ) )
    (close port) ) )

(export eof? cat-cur cat-ex cat-nl cat-tup)