unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Thien-Thi Nguyen <ttn@surf.glug.org>
Cc: guile-user@gnu.org
Subject: fortune-cookie
Date: Tue, 15 Jun 2004 18:21:36 +0200	[thread overview]
Message-ID: <E1BaGgq-0000bV-00@surf.glug.org> (raw)

(use-modules (database binconv) (scripts slurp))

(define (fortune-cookie cookie-file)

  (let* ((port (open-input-file (string-append cookie-file ".dat")))
         (readw (let ((s (make-string 4)))
                  (lambda ()
                    (string-set! s 0 (read-char port))
                    (string-set! s 1 (read-char port))
                    (string-set! s 2 (read-char port))
                    (string-set! s 3 (read-char port))
                    (integer-byte-string->integer s #f #t)))))

    (define (read-header)
      (list (cons #:version  (readw))
            (cons #:count    (readw))
            (cons #:longest  (readw))
            (cons #:shortest (readw))
            (cons #:flags    (let* ((w (readw))
                                    (chk (lambda (mask val)
                                           (if (= 0 (logand mask w))
                                               '()
                                               (list val)))))
                               `(,@(chk #x1 #:random)
                                 ,@(chk #x2 #:ordered)
                                 ,@(chk #x4 #:rotated))))
            (cons #:delim    (read-char port))
            (cons #:filler   (list (read-char port)
                                   (read-char port)
                                   (read-char port)))))

    ;; do it!
    (let* ((header (read-header))
           (selection (random (assq-ref header #:count)))
           (start (do ((i 0 (1+ i)))
                      ((= i selection) (readw))
                    (readw)))
           (finish (readw))
           (len (- finish start 2))
           (cookie (make-string len)))
      (slurp-file! cookie cookie-file start len 0)
      (display cookie))))

;;(set! *random-state* (seed->random-state (current-time)))
;;(fortune-cookie "lambda.txt") ; use strfile(1) to make lambda.txt.dat


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


                 reply	other threads:[~2004-06-15 16:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1BaGgq-0000bV-00@surf.glug.org \
    --to=ttn@surf.glug.org \
    --cc=guile-user@gnu.org \
    --cc=ttn@glug.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).