core: fix IO.read->EOF

This commit is contained in:
hellerve 2019-05-10 11:21:46 +02:00
parent 9b9b0df90e
commit 992b0c5fc5
3 changed files with 17 additions and 10 deletions

View File

@ -48,13 +48,15 @@
(doc read->EOF "reads a file given by name until the End-Of-File character is reached.")
(defn read->EOF [filename]
(let [f (IO.fopen filename "rb")
c (Char.from-int 0)
r []]
(do
(while (do (set! c (IO.fgetc f))
(/= c IO.EOF))
(set! r (Array.push-back r c)))
(IO.fclose f)
(String.from-chars &r))))
(let [f (IO.fopen filename "rb")]
(if (null? f)
(Result.Error (fmt "File “%s” couldnt be opened!" filename))
(let [c (Char.from-int 0)
r []]
(do
(while (do (set! c (IO.fgetc f))
(/= c IO.EOF))
(set! r (Array.push-back r c)))
(IO.fclose f)
(Result.Success (String.from-chars &r)))))))
)

View File

@ -565,7 +565,7 @@
defn
</div>
<p class="sig">
(λ [&amp;String] String)
(λ [&amp;String] (Result String String))
</p>
<pre class="args">
(read-&gt;EOF filename)

View File

@ -25,4 +25,9 @@
@&Int.MAX
"test that the address of Int.MAX can be taken"
)
(assert-equal test
&(Result.Error @"File “foobar” couldnt be opened!")
&(IO.read->EOF "foobar")
"test that reading from an undefined file works"
)
)