2004-12-23 06:51:42 -05:00
|
|
|
! $Id$
|
|
|
|
!
|
2005-02-12 02:23:38 -05:00
|
|
|
! Copyright (C) 2004, 2005 Mackenzie Straight.
|
2004-12-23 06:51:42 -05:00
|
|
|
!
|
|
|
|
! 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.
|
|
|
|
|
2004-12-25 05:49:30 -05:00
|
|
|
IN: kernel-internals
|
2005-02-12 02:23:38 -05:00
|
|
|
USING: alien errors generic kernel kernel-internals math namespaces strings
|
|
|
|
win32-api ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
2005-02-12 02:23:38 -05:00
|
|
|
TUPLE: buffer size ptr fill pos ;
|
2004-12-25 05:49:30 -05:00
|
|
|
|
2004-12-23 06:51:42 -05:00
|
|
|
: imalloc ( size -- address )
|
|
|
|
"int" "libc" "malloc" [ "int" ] alien-invoke ;
|
|
|
|
|
|
|
|
: ifree ( address -- )
|
|
|
|
"void" "libc" "free" [ "int" ] alien-invoke ;
|
|
|
|
|
2004-12-25 05:49:30 -05:00
|
|
|
: irealloc ( address size -- address )
|
|
|
|
"int" "libc" "realloc" [ "int" "int" ] alien-invoke ;
|
|
|
|
|
2005-02-12 02:23:38 -05:00
|
|
|
C: buffer ( size -- buffer )
|
|
|
|
2dup set-buffer-size
|
|
|
|
swap imalloc swap [ set-buffer-ptr ] keep
|
|
|
|
0 swap [ set-buffer-fill ] keep
|
|
|
|
0 swap [ set-buffer-pos ] keep ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-free ( buffer -- )
|
|
|
|
#! Frees the C memory associated with the buffer.
|
2005-02-12 02:23:38 -05:00
|
|
|
buffer-ptr ifree ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-contents ( buffer -- string )
|
|
|
|
#! Returns the current contents of the buffer.
|
2005-02-12 02:23:38 -05:00
|
|
|
dup buffer-ptr over buffer-pos +
|
|
|
|
over buffer-fill pick buffer-pos -
|
|
|
|
memory>string nip ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
2004-12-25 05:49:30 -05:00
|
|
|
: buffer-first-n ( count buffer -- string )
|
2005-02-12 02:23:38 -05:00
|
|
|
[ dup buffer-fill swap buffer-pos - min ] keep
|
|
|
|
dup buffer-ptr swap buffer-pos + swap memory>string ;
|
2004-12-25 05:49:30 -05:00
|
|
|
|
2004-12-23 06:51:42 -05:00
|
|
|
: buffer-reset ( count buffer -- )
|
|
|
|
#! Reset the position to 0 and the fill pointer to count.
|
2005-02-12 02:23:38 -05:00
|
|
|
[ set-buffer-fill ] keep 0 swap set-buffer-pos ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-consume ( count buffer -- )
|
|
|
|
#! Consume count characters from the beginning of the buffer.
|
2005-02-12 02:23:38 -05:00
|
|
|
[ buffer-pos + ] keep [ buffer-fill min ] keep [ set-buffer-pos ] keep
|
|
|
|
dup buffer-pos over buffer-fill = [
|
|
|
|
[ 0 swap set-buffer-pos ] keep [ 0 swap set-buffer-fill ] keep
|
|
|
|
] when drop ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-length ( buffer -- length )
|
|
|
|
#! Returns the amount of unconsumed input in the buffer.
|
2005-02-12 02:23:38 -05:00
|
|
|
dup buffer-fill swap buffer-pos - 0 max ;
|
2004-12-25 05:49:30 -05:00
|
|
|
|
|
|
|
: buffer-capacity ( buffer -- int )
|
|
|
|
#! Returns the amount of data that may be added to the buffer.
|
2005-02-12 02:23:38 -05:00
|
|
|
dup buffer-size swap buffer-fill - ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-set ( string buffer -- )
|
2005-03-05 16:33:40 -05:00
|
|
|
2dup buffer-ptr string>memory >r string-length r> buffer-reset ;
|
2005-02-12 02:23:38 -05:00
|
|
|
|
|
|
|
: (check-overflow) ( string buffer -- )
|
2005-03-05 16:33:40 -05:00
|
|
|
buffer-capacity swap string-length < [ "Buffer overflow" throw ] when ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
|
|
|
: buffer-append ( string buffer -- )
|
2005-02-12 02:23:38 -05:00
|
|
|
2dup (check-overflow)
|
|
|
|
[ dup buffer-ptr swap buffer-fill + string>memory ] 2keep
|
2005-03-05 16:33:40 -05:00
|
|
|
[ buffer-fill swap string-length + ] keep set-buffer-fill ;
|
2004-12-25 05:49:30 -05:00
|
|
|
|
2005-01-02 16:14:34 -05:00
|
|
|
: buffer-append-char ( int buffer -- )
|
2005-02-12 02:23:38 -05:00
|
|
|
#! Append a single character to a buffer
|
|
|
|
[ dup buffer-ptr swap buffer-fill + <alien> 0 set-alien-1 ] keep
|
|
|
|
[ buffer-fill 1 + ] keep set-buffer-fill ;
|
2005-01-02 16:14:34 -05:00
|
|
|
|
2004-12-25 05:49:30 -05:00
|
|
|
: buffer-extend ( length buffer -- )
|
|
|
|
#! Increases the size of the buffer by length.
|
2005-02-12 02:23:38 -05:00
|
|
|
[ buffer-size + dup ] keep [ buffer-ptr swap ] keep >r irealloc r>
|
|
|
|
[ set-buffer-ptr ] keep set-buffer-size ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
2005-02-12 02:23:38 -05:00
|
|
|
: buffer-inc-fill ( count buffer -- )
|
2004-12-25 05:49:30 -05:00
|
|
|
#! Increases the fill pointer by count.
|
2005-02-12 02:23:38 -05:00
|
|
|
[ buffer-fill + ] keep set-buffer-fill ;
|
2004-12-23 06:51:42 -05:00
|
|
|
|
2005-02-12 02:23:38 -05:00
|
|
|
: buffer-pos+ptr ( buffer -- int )
|
|
|
|
[ buffer-ptr ] keep buffer-pos + ;
|