mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 13:37:57 +03:00
core: fix IO.read->EOF
This commit is contained in:
parent
9b9b0df90e
commit
992b0c5fc5
20
core/IO.carp
20
core/IO.carp
@ -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” couldn’t 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)))))))
|
||||
)
|
||||
|
@ -565,7 +565,7 @@
|
||||
defn
|
||||
</div>
|
||||
<p class="sig">
|
||||
(λ [&String] String)
|
||||
(λ [&String] (Result String String))
|
||||
</p>
|
||||
<pre class="args">
|
||||
(read->EOF filename)
|
||||
|
@ -25,4 +25,9 @@
|
||||
@&Int.MAX
|
||||
"test that the address of Int.MAX can be taken"
|
||||
)
|
||||
(assert-equal test
|
||||
&(Result.Error @"File “foobar” couldn’t be opened!")
|
||||
&(IO.read->EOF "foobar")
|
||||
"test that reading from an undefined file works"
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user