Guest User

Test new READ-LINE (Rev 1 2021-11-06)

a guest
Nov 5th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. \ Test new READ-LINE
  2. \
  3. \ Rev 1 2021-11-06 fix READ-LINE return codes & test to match
  4. \ description
  5. \
  6. \ Suggested alterations to ANS READ-LINE specification to avoid the
  7. \ quirky buffer requirements and ensure end-of-line detection.
  8. \
  9. \ Amend the interface to:
  10. \
  11. \ READ-LINE ( c-addr u1 fid -- u2 n3 ior )
  12. \
  13. \ where 'n3' returns a code representing the status:
  14. \ 0 = EOF, -1 = EOL received, 1 = EOL not received
  15. \
  16. \ Summary of changes:
  17. \
  18. \ - Previously 'flag' was true|false. In the replacement it may take
  19. \ the values -1|0|1 so we rename 'flag' to 'n3'.
  20. \
  21. \ - Previously 'u1' was the maximum chars to receive. 'u1' now refers
  22. \ to the buffer size.
  23.  
  24. \ Sample implementation & test:
  25.  
  26. empty
  27.  
  28. 0 value ifid
  29. 0 value ofid
  30.  
  31. : readtext0 ( a u1 -- a u2 flag ) over swap ifid read-line throw ;
  32.  
  33. \ EOL string
  34. here 2 c, 13 c, 10 c, count 2constant (cr) ( -- a u )
  35. \ here 1 c, 10 c, count 2constant (cr) ( -- a u )
  36. \ here 1 c, 13 c, count 2constant (cr) ( -- a u )
  37.  
  38. \ EOL scanner
  39. : /EOL ( a u -- u' offs )
  40. over swap begin dup while
  41. over c@ dup $0D - while $0A - while 1 /string
  42. repeat \ got LF
  43. drop swap - dup 1+ exit
  44. then \ got CR
  45. 2drop tuck swap - swap 1+ c@ $0A <> over + 2 + exit
  46. then \ neither
  47. drop swap - dup ;
  48.  
  49. 0 [if]
  50.  
  51. \ DTC 8086 version
  52. \ Scan for CR LF CRLF return len & offset to next line
  53. code /EOL ( a u -- u' offs )
  54. cx pop bx pop bx di mov dx dx sub $0A0D # ax mov
  55. 1 $: 4 $ jcxz al 0 [bx] cmp 2 $ jz ah 0 [bx] cmp 3 $ jz
  56. bx inc cx dec 1 $ ju 2 $: dx inc ah 1 [bx] cmp 4 $ jnz
  57. 3 $: dx inc 4 $: di bx sub bx push dx bx add bx push
  58. next
  59. end-code
  60.  
  61. [then]
  62.  
  63. \ buffer size at least u chars and a minimum of one
  64. \ n3: 0 = EOF, -1 = EOL received, 1 = EOL not received
  65. : READ-LINE ( a u1 fid -- u2 n3 ior )
  66. >r 2dup r@ read-file
  67. dup if r> drop nip exit then \ ior
  68. over 0= if r> drop 2swap 2drop 0 exit then \ EOF
  69. drop >r tuck 1- r@ min /eol r> - s>d
  70. r@ file-position drop d+ r> reposition-file drop
  71. tuck 1+ <> 2* 1+ 0 ;
  72.  
  73. : writedata ( a u -- ) ofid write-file throw ;
  74. : readtext ( a u1 -- a u2 flag ) over swap ifid read-line throw ;
  75.  
  76. : open ( a u -- ) r/o bin open-file throw to ifid ;
  77. : make ( a u -- ) r/w bin create-file throw to ofid ;
  78. : closein ( -- ) ifid close-file drop ;
  79. : closeout ( -- ) ofid close-file drop ;
  80.  
  81. : ?cr ( flag -- ) if (cr) writedata then ;
  82.  
  83. 5 value bufsize
  84.  
  85. : read&write ( a u a2 u2 -- )
  86. 2swap 2dup open
  87. cr ." Reading: " type ." using READ-LINE u1 = " bufsize . cr
  88. cr ." Writing: " 2dup type ." u2 chars using WRITE-FILE;"
  89. cr ." write EOL sequence only when n3 = -1" cr
  90. make
  91. 0 begin
  92. ?cr pad bufsize readtext dup while \ not done
  93. ( a u2 flag) >r writedata r> 1-
  94. repeat drop drop drop
  95. closein closeout ;
  96.  
  97. : make-test ( a u -- )
  98. cr ." Making text file: " 2dup type cr make
  99. s" 123456789"
  100. 2dup 8 - writedata (cr) writedata
  101. 2dup 7 - writedata (cr) writedata
  102. 2dup 6 - writedata (cr) writedata
  103. 2dup 5 - writedata (cr) writedata
  104. 2dup 4 - writedata (cr) writedata
  105. 2dup 3 - writedata (cr) writedata
  106. 2dup 2 - writedata (cr) writedata
  107. 2dup 1 - writedata (cr) writedata
  108. writedata (cr) writedata
  109. closeout ;
  110.  
  111. : show ( a u -- )
  112. cr ." Showing: " 2dup type open
  113. cr begin pad 100 readtext0 while type cr repeat 2drop
  114. closein ;
  115.  
  116. : foobar
  117. page
  118. s" FOO" make-test s" FOO" show
  119. s" FOO" s" BAR" read&write s" BAR" show ;
  120.  
  121. foobar
  122.  
Add Comment
Please, Sign In to add comment