Changeset 14443 in project


Ignore:
Timestamp:
04/25/09 19:13:46 (11 years ago)
Author:
Jim Ursetto
Message:

base64: document port I/O

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/base64

    r14254 r14443  
    2222== Examples
    2323
    24 <examples><example>
    25 <init>
    26 (require-extension base64)
    27 </init>
    28 <expr>
    29 (define s "thequickbrownfoxjumpsoverthelazydog")
    30 (base64-encode s)
    31 </expr>
    32 <result>
    33 "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
    34 </result>
    35 <expr>
    36 (base64-decode (base64-encode s))
    37 </expr>
    38 <result>
    39 "thequickbrownfoxjumpsoverthelazydog"
    40 </result>
    41 </example></examples>
     24 (require-extension base64)
     25 (define s "thequickbrownfoxjumpsoverthelazydog")
     26 (base64-encode s)
     27    ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
     28 (base64-decode (base64-encode s))
     29    ; => "thequickbrownfoxjumpsoverthelazydog"
     30 (get-output-string (base64-encode (open-input-string s)
     31                                   (open-output-string)))
     32    ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
    4233
    43 A script that encodes a file given on the command-line in the style of {{uuencode -m}}:
     34A script that encodes a file given on the command-line in the style of {{uuencode -m}}.
     35It reads the entire file into memory:
    4436
    4537 #!/usr/local/bin/csi -script
     
    5244     (lambda (p) (read-string #f p)))))
    5345
    54 The last script reads the file completely into memory.  You can use a constant amount of memory and obtain the same output by
    55 reading in multiples of 57 characters and displaying incrementally:
     46Instead, you can use a constant amount of memory by using port I/O:
    5647
    5748 #!/usr/local/bin/csi -script
    58  (use base64)
     49 (use base64)                           ;; uuencode.scm
    5950 (base64-line-breaks #t)
    60  
    61  (call-with-input-file (car (command-line-arguments))
    62    (lambda (p)
    63      (let loop ()
    64        (let ((s (read-string (* 57 60) p)))
    65          (cond ((string=? s ""))
    66                (else
    67                 (display (base64-encode s))
    68                 (loop)))))))
     51 (base64-encode (current-input-port)
     52                (current-output-port))
    6953
    70 For properly formatted output in a UTF-8 environment, you should read 57 bytes instead of 57 characters, as these procedures operate on octets.
     54 #!/usr/local/bin/csi -script
     55 (use base64)                           ;; uudecode.scm
     56 (base64-decode (current-input-port)
     57                (current-output-port))
     58
     59 $ md5sum chicken-4.0.0.tar.gz
     60 658deaae356f3360e48d6d1628fc062e  chicken-4.0.0.tar.gz
     61 $ csi4 -script uuencode.scm < chicken-4.0.0.tar.gz | csi4 -script uudecode.scm | md5sum
     62 658deaae356f3360e48d6d1628fc062e  -
    7163
    7264== About this egg
     
    7668* Bigloo code by James Bailey
    7769* Ported to CHICKEN by felix
    78 * Optimized 3.0 implementation by Jim Ursetto
     70* 3.0 implementation by Jim Ursetto
    7971
    8072=== Version history
    8173
     74; 3.2 : Encoding and decoding support port I/O
    8275; 3.1 : Add optional decoder linebreaks
    8376; 3.0 : API change, reimplement in Scheme
Note: See TracChangeset for help on using the changeset viewer.