factor/library/io/stream.factor

73 lines
2.4 KiB
Factor
Raw Normal View History

! :folding=indent:collapseFolds=1:
2004-07-16 02:26:21 -04:00
! $Id$
!
! Copyright (C) 2003, 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: streams
USE: errors
USE: kernel
USE: namespaces
2004-08-10 23:48:08 -04:00
USE: strings
2004-11-28 21:56:58 -05:00
USE: generic
2004-07-16 02:26:21 -04:00
2004-11-28 21:56:58 -05:00
GENERIC: fflush ( stream -- )
GENERIC: fauto-flush ( stream -- )
GENERIC: freadln ( stream -- string )
GENERIC: fread# ( count stream -- string )
GENERIC: fwrite-attr ( string style stream -- )
GENERIC: fclose ( stream -- )
2004-07-16 02:26:21 -04:00
: fread1 ( stream -- string )
2004-11-28 21:56:58 -05:00
1 swap fread# dup f-or-"" [ 0 swap str-nth ] unless ;
2004-07-16 02:26:21 -04:00
: fwrite ( string stream -- )
2004-11-28 21:56:58 -05:00
f swap fwrite-attr ;
2004-07-16 02:26:21 -04:00
: fprint ( string stream -- )
tuck fwrite "\n" over fwrite fauto-flush ;
2004-11-28 21:56:58 -05:00
TRAITS: string-output-stream
2004-07-16 02:26:21 -04:00
2004-11-28 21:56:58 -05:00
M: string-output-stream fwrite-attr ( string style stream -- )
[ drop "buf" get sbuf-append ] bind ;
2004-07-16 02:26:21 -04:00
2004-11-28 21:56:58 -05:00
M: string-output-stream fclose ( stream -- )
drop ;
2004-07-16 02:26:21 -04:00
2004-11-28 21:56:58 -05:00
M: string-output-stream fflush ( stream -- )
drop ;
2004-11-28 21:56:58 -05:00
M: string-output-stream fauto-flush ( stream -- )
drop ;
2004-08-10 23:48:08 -04:00
: stream>str ( stream -- string )
#! Returns the string written to the given string output
#! stream.
[ "buf" get ] bind sbuf>str ;
2004-11-28 21:56:58 -05:00
C: string-output-stream ( size -- stream )
#! Creates a new stream for writing to a string buffer.
[ <sbuf> "buf" set ] extend ;