/tests/inputs/issues/183/file.fth

https://github.com/AlDanial/cloc · Forth · 161 lines · 137 code · 16 blank · 8 comment · 5 complexity · c1a8bf9fb21814b1cb62712d75b370be MD5 · raw file

  1. \ https://github.com/philburk/pforth/fth/file.fth
  2. \ READ-LINE and WRITE-LINE
  3. \
  4. \ This code is part of pForth.
  5. \
  6. \ The pForth software code is dedicated to the public domain,
  7. \ and any third party may reproduce, distribute and modify
  8. \ the pForth software code or any derivative works thereof
  9. \ without any compensation or license. The pForth software
  10. \ code is provided on an "as is" basis without any warranty
  11. \ of any kind, including, without limitation, the implied
  12. \ warranties of merchantability and fitness for a particular
  13. \ purpose and their equivalents under the laws of any jurisdiction.
  14. private{
  15. 10 constant \N
  16. 13 constant \R
  17. \ Unread one char from file FILEID.
  18. : UNREAD { fileid -- ior }
  19. fileid file-position ( ud ior )
  20. ?dup
  21. IF nip nip \ IO error
  22. ELSE 1 s>d d- fileid reposition-file
  23. THEN
  24. ;
  25. \ Read the next available char from file FILEID and if it is a \n then
  26. \ skip it; otherwise unread it. IOR is non-zero if an error occured.
  27. \ C-ADDR is a buffer that can hold at least one char.
  28. : SKIP-\N { c-addr fileid -- ior }
  29. c-addr 1 fileid read-file ( u ior )
  30. ?dup
  31. IF \ Read error?
  32. nip
  33. ELSE ( u )
  34. 0=
  35. IF \ End of file?
  36. 0
  37. ELSE
  38. c-addr c@ \n = ( is-it-a-\n? )
  39. IF 0
  40. ELSE fileid unread
  41. THEN
  42. THEN
  43. THEN
  44. ;
  45. \ This is just s\" \n" but s\" isn't yet available.
  46. create (LINE-TERMINATOR) \n c,
  47. : LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ;
  48. \ Standard throw code
  49. \ See: http://lars.nocrew.org/forth2012/exception.html#table:throw
  50. -72 constant THROW_RENAME_FILE
  51. \ Copy the string C-ADDR/U1 to C-ADDR2 and append a NUL.
  52. : PLACE-CSTR ( c-addr1 u1 c-addr2 -- )
  53. 2dup 2>r ( c-addr1 u1 c-addr2 ) ( r: u1 c-addr2 )
  54. swap cmove ( ) ( r: u1 c-addr2 )
  55. 0 2r> + c! ( )
  56. ;
  57. : MULTI-LINE-COMMENT ( "comment<rparen>" -- )
  58. BEGIN
  59. >in @ ')' parse ( >in c-addr len )
  60. nip + >in @ = ( delimiter-not-found? )
  61. WHILE ( )
  62. refill 0= IF EXIT THEN ( )
  63. REPEAT
  64. ;
  65. }private
  66. \ This treats \n, \r\n, and \r as line terminator. Reading is done
  67. \ one char at a time with READ-FILE hence READ-FILE should probably do
  68. \ some form of buffering for good efficiency.
  69. : READ-LINE ( c-addr u1 fileid -- u2 flag ior )
  70. { a u f }
  71. u 0 ?DO
  72. a i chars + 1 f read-file ( u ior' )
  73. ?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u )
  74. 0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( )
  75. a i chars + c@
  76. CASE
  77. \n OF i true 0 UNLOOP EXIT ENDOF
  78. \r OF
  79. \ Detect \r\n
  80. a i chars + f skip-\n ( ior )
  81. ?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( )
  82. i true 0 UNLOOP EXIT
  83. ENDOF
  84. ENDCASE
  85. LOOP
  86. \ Line doesn't fit in buffer
  87. u true 0
  88. ;
  89. : WRITE-LINE ( c-addr u fileid -- ior )
  90. { f }
  91. f write-file ( ior )
  92. ?dup
  93. IF \ IO error
  94. ELSE line-terminator f write-file
  95. THEN
  96. ;
  97. : RENAME-FILE ( c-addr1 u1 c-addr2 u2 -- ior )
  98. { a1 u1 a2 u2 | new }
  99. \ Convert the file-names to C-strings by copying them after HERE.
  100. a1 u1 here place-cstr
  101. here u1 1+ chars + to new
  102. a2 u2 new place-cstr
  103. here new (rename-file) 0=
  104. IF 0
  105. ELSE throw_rename_file
  106. THEN
  107. ;
  108. \ A limit used to perform a sanity check on the size argument for
  109. \ RESIZE-FILE.
  110. 2variable RESIZE-FILE-LIMIT
  111. 10000000 0 resize-file-limit 2! \ 10MB is somewhat arbitrarily chosen
  112. : RESIZE-FILE ( ud fileid -- ior )
  113. -rot 2dup resize-file-limit 2@ d> ( fileid ud big? )
  114. IF
  115. ." Argument (" 0 d.r ." ) is larger then RESIZE-FILE-LIMIT." cr
  116. ." (You can increase RESIZE-FILE-LIMIT with 2!)" cr
  117. abort
  118. ELSE
  119. rot (resize-file)
  120. THEN
  121. ;
  122. : ( ( "comment<rparen>" -- )
  123. source-id
  124. CASE
  125. -1 OF postpone ( ENDOF
  126. 0 OF postpone ( ENDOF
  127. \ for input from files
  128. multi-line-comment
  129. ENDCASE
  130. ; immediate
  131. \ We basically try to open the file in read-only mode. That seems to
  132. \ be the best that we can do with ANSI C. If we ever want to do
  133. \ something more sophisticated, like calling access(2), we must create
  134. \ a proper primitive. (OTOH, portable programs can't assume much
  135. \ about FILE-STATUS and non-portable programs could create a custom
  136. \ function for access(2).)
  137. : FILE-STATUS ( c-addr u -- 0 ior )
  138. r/o bin open-file ( fileid ior1 )
  139. ?dup
  140. IF nip 0 swap ( 0 ior1 )
  141. ELSE close-file 0 swap ( 0 ior2 )
  142. THEN
  143. ;
  144. privatize