Next: , Previous: database binconv, Up: Persistence


42.3 database fcookie

This module provides two procs:
 (create-index-file! out-name in-name delim flags) => #t for success
 (fortune-cookie cookie-filename [dat-filename]) => string

A fortune cookie file is a text file containing groups of lines separated
by "delim lines", a specially chosen character (traditionally the percent
`%' character) on a line by itself.  The first and last lines may also be
delim lines.  Each group of lines thus delimited is a "cookie".  Thus, a
file with N cookies may have N-1, N or N+1 delim lines.

A fortune cookie index file is a binary file consisting of a six-word
header followed by the offset table (each entry a word).  A word is four
bytes in network (big-endian) order.  The header is:

  version     -- typically 1 for old files and 2 for newer ones
  count       -- number of cookies
  longest     -- number of bytes of longest cookie
  shortest    -- number of bytes of shortest cookie
  flags       -- logior of #x1 (random)
                           #x2 (ordered)
                           #x4 (rotated)
  delim-char  -- this is shifted to the MSB position
                 (the remaining bytes are #\nul)

Traditionally, for cookie file foo, the index file is named foo.dat, but
that is not required.
— Scheme Procedure: create-index-file! out-name in-name delim [flags...]

Create index file out-name from cookie file in-name, separating cookies by looking for char delim on a line by itself. Optional flags are keywords:

#:random
Set bit 0 (corresponding to a mask of #x1) in the flags word in the header, but do nothing else at the moment (FIXME).
#:ordered
Set bit 1 (corresponding to a mask of #x2) in the flags word in the header, and order the offsets by sorting the cookies with string<?, ignoring non-alphanumeric leading characters.
#:rotated
Set bit 2 (corresponding to a mask of #x4) in the flags word in the header, to note that the cookies are ROT13.

Return #t on success.

— Scheme Procedure: fortune-cookie cookie-file [dat-file]

Return a randomly-chosen string extracted from cookie-file, using the index file named by appending .dat to cookie-file. Optional arg dat-file specifies the index file to use instead of the default.