programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

Fixed READ-LINE

From: Bill McCarthy <WJMc_at_pobox.com>
Date: Sat, 15 Feb 2003 19:32:09 -0500

anew __Fixed_READ-LINE__

\ I believe I've solved the problem with the failure of READ-LINE
\ when the length of the line (not counting EOL) equals the
\ number of characters requested.
\
\ This code, as before, works only with DOS and Unix files. Mac
\ files are not supported.
\
\ To check out the fix, you'll need definitions for "CR, "LR & EOL.
\ (Those 3 definitions are hidden.)

CTRL M CONSTANT "CR
CTRL J CONSTANT "LF

CODE EOL ( addr len -- n len )
   0 [EBP] EDX MOV \ get addr
   EAX EAX XOR
   BEGIN
      EBX EBX OR
      0<> WHILE
      0 [EDX] AL MOV
      "CR # AL CMP 0= IF
         2 # 0 [EBP] MOV RET
      THEN
      "LF # AL CMP 0= IF
         1 # 0 [EBP] MOV RET
      THEN
      EBX DEC EDX INC
   REPEAT
   RET END-CODE

\ Here's the fixed code. I've replaced one line with two. The
\ replaced line is commented out. The two added lines follow it.

: READ-LINE ( addr ask fid -- 0 0 0 | got t 0 )
   -ROT 3DUP 1+ 0 >R RP@ 0 ReadFile 0<> R> AND
   ( read) DUP IF \ fid addr ask read
      ROT OVER EOL \ fid ask read addr xcr ***
      DUP IF ( eol) \ fid ask read addr xcr ***
\ TUCK - >R - MIN \ fid got
         TUCK - >R - OVER >R MIN \ WJMc: fid got
         dup r> = if r> drop -1 >r then \ WJMc: fid got
      ELSE ( absent) \ fid ask read addr xcr ***
         2DROP 2DUP U< >R MIN \
      THEN \ fid got
      SWAP R> 0 1 SetFilePointer
      'NEXTPOS @ 'LINEPOS 2! \ got
      TRUE 0 EXIT \ got t
   THEN
   ( eof) 2DROP 2DROP 0 0 0 ;

\ NOTE: The lines I've marked with '***' have an error in the
\ comments. That 'addr' is not the buffer address. It's the
\ 'n' returned from EOL.

\ Bill
----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
Received on Sat Feb 15 2003 - 16:30:55 PST

This archive was generated by hypermail 2.2.0 : Tue Dec 02 2008 - 03:04:28 PST