This is G o o g l e's cache of http://home.earthlink.net/~neilbawd/outredir.html.
G o o g l e's cache is the snapshot that we took of the page as we crawled the web.
The page may have changed since that time. Click here for the current page without highlighting.
To link to or bookmark this page, use the following url: http://www.google.com/search?q=cache:dupeScWjrs8C:home.earthlink.net/~neilbawd/outredir.html+&hl=en&ie=UTF-8


Google is not affiliated with the authors of this page nor responsible for its content.

Output Redirection and SPONGE

Output Redirection and SPONGE

TEXT

Wil Baden 2000-08-21

CORE Words for OUTPUT-REDIRECTION

SPONGE requires SLURP

'OUTPUT   .   ."   >OUTPUT   CR   EMIT   OUTPUT-REDIRECTION   SPACE   SPACES   SPONGE   TYPE   U.   sponge  

Only the words of the CORE word set have been redefined, as output redirection should be done by the system.


OUTPUT-REDIRECTION  ( -- )
Vocabulary of replacements for display words of CORE word set.
Usage:
ALSO OUTPUT-REDIRECTION ... PREVIOUS
or
ONLY FORTH DEFINITIONS ALSO OUTPUT-REDIRECTION ... ONLY FORTH DEFINITIONS,
etc.
'OUTPUT             ( -- addr)
When addr has zero, do normal display. When addr has a file-id, buffer output to the file. Ideally the system should set 'OUTPUT to 0 on an abort or error. Not having that you may want to use 'OUTPUT OFF at the beginning of a function that's going to redirect output.
>OUTPUT             ( n -- )
n is 0 or a file-id. When n is the same as the value at 'OUTPUT, do nothing. Otherwise cease output to the file, and set up for new redirection. Use n equal 0 to terminate output redirection.
Program Text 1
 
VARIABLE 'OUTPUT  0 'OUTPUT !  \  Should be user variable.

S" /COUNTED-STRING" ENVIRONMENT? 0= [IF]  255  [THEN]
    CONSTANT /COUNTED-STRING

VOCABULARY OUTPUT-REDIRECTION

ONLY FORTH  ALSO OUTPUT-REDIRECTION DEFINITIONS

CREATE OUTBUFFER  /COUNTED-STRING 1+ CHARS ALLOT  \  Should be user array.
    0 OUTBUFFER C!


CORE Word Set Display Words

Program Text 2
 
: CR                ( -- )
   'OUTPUT @ IF
      OUTBUFFER COUNT  'OUTPUT @  WRITE-LINE FILE-CHECK
      0 OUTBUFFER C!
   ELSE  CR  THEN ;

   : MORE              ( n addr -- addr )
       TUCK C@ + /COUNTED-STRING > IF CR THEN ;

: TYPE              ( str len -- )
   'OUTPUT @ IF  DUP OUTBUFFER MORE APPEND
   ELSE  TYPE  THEN ;

: EMIT              ( char -- )
   'OUTPUT @ IF  1 OUTBUFFER MORE APPEND-CHAR
   ELSE  EMIT  THEN ;

: SPACE ( -- ) BL EMIT ;

: SPACES            ( n -- )
    'OUTPUT @ IF
        0 MAX  /COUNTED-STRING MIN
        DUP OUTBUFFER MORE    ( n addr)
        2DUP  COUNT + SWAP BL FILL
        C+!
    ELSE  SPACES  THEN ;

: .  ( n -- )  (.) TYPE SPACE ;

: U. ( u -- )  0 <# #S #> TYPE SPACE ;

: ."                ( "string<single-quote>" -- )
    POSTPONE S"
    STATE @ IF  POSTPONE TYPE
    ELSE        TYPE
    THEN ; IMMEDIATE


>OUTPUT

Program Text 3
 
ONLY FORTH DEFINITIONS ALSO OUTPUT-REDIRECTION

: >OUTPUT           ( n -- )
    DUP 'OUTPUT @ = IF  DROP  EXIT THEN
    'OUTPUT @ IF
        OUTBUFFER C@ IF  CR  THEN
    THEN
    0 OUTBUFFER C!  \  Be sure.
    'OUTPUT ! ;

ONLY FORTH DEFINITIONS


Wil Baden 2000-08-26

From JARGON File

sponge
n. [Unix] A special case of a filter that reads its entire input before writing any output; the canonical example is a sort utility. Unlike most filters, a sponge can conveniently overwrite the input file with the output data stream....

A sponge can be written using SLURP with output redirection.

SPONGE              ( "file-name" -- src length file-id )
Slurp file-name, delete file-name, and create file-name for output redirection.

For an example of a sponge, see DETAB.

Program Text 4
 
    CREATE Name-Holder  64 CHARS ALLOT

: SPONGE          ( "file-name" -- src length file-id )
    BL WORD COUNT Name-Holder PLACE             ( )
    Name-Holder COUNT R/O OPEN-FILE FILE-CHECK  ( file-id)

    DUP SLURP ROT CLOSE-FILE FILE-CHECK         ( src length)

    Name-Holder COUNT DELETE-FILE DROP
    Name-Holder COUNT W/O CREATE-FILE FILE-CHECK
                                        ( src length file-id)
    ;


Another approach is slurp file, delete and create temporary file, and redirect output to temporary file. Then close temporary file, and if successful, delete original file and rename the temporary file as the original file.

Go back to Neil Bawd's home page.