mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
commit
34c314e6fe
@ -31,8 +31,8 @@
|
||||
(load "Array.carp")
|
||||
(load "Char.carp")
|
||||
(load "String.carp")
|
||||
(load "IO.carp")
|
||||
(load "System.carp")
|
||||
(load "IO.carp")
|
||||
(load "Pattern.carp")
|
||||
(load "Debug.carp")
|
||||
(load "Pointer.carp")
|
||||
|
33
core/IO.carp
33
core/IO.carp
@ -24,8 +24,17 @@
|
||||
(register exit (Fn [Int] a))
|
||||
(register EOF Char)
|
||||
(doc EOF "the End-Of-File character as a literal.")
|
||||
(doc fopen "opens a file by name using a mode (one or multiple of [r]ead, [w]rite, and [a]ppend), returns a file pointer.")
|
||||
(doc fopen "opens a file by name using a mode (one or multiple of [r]ead, [w]rite, and [a]ppend), returns a file pointer. Consider using the function open-file instead.")
|
||||
(register fopen (Fn [&String &String] (Ptr FILE)))
|
||||
|
||||
(doc open-file "opens a file by name using a mode (one or multiple of [r]ead, [w]rite, and [a]ppend), returns a Result type that contains an error string or a file pointer.")
|
||||
(defn open-file [filename mode]
|
||||
(let [ptr (IO.fopen filename mode)]
|
||||
(if (null? ptr)
|
||||
(do
|
||||
(Result.Error System.errno))
|
||||
(Result.Success ptr))))
|
||||
|
||||
(doc fclose "closes a file pointer.")
|
||||
(register fclose (Fn [(Ptr FILE)] ()))
|
||||
(doc fgetc "gets a character from a file pointer.")
|
||||
@ -51,15 +60,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")]
|
||||
(if (null? f)
|
||||
(Result.Error (fmt "File “%s” couldn’t be opened!" filename))
|
||||
(let [c (zero)
|
||||
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)))))))
|
||||
(let [maybe (IO.open-file filename "rb")]
|
||||
(match maybe
|
||||
(Result.Error x) (Result.Error x)
|
||||
(Result.Success f) (let [c (zero)
|
||||
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)))))))
|
||||
)
|
||||
|
@ -1,4 +1,5 @@
|
||||
(system-include "carp_system.h")
|
||||
(system-include "errno.h")
|
||||
|
||||
(defmodule System
|
||||
(doc free "Frees an object. Should not be called except in direst circumstances.")
|
||||
@ -28,4 +29,24 @@
|
||||
(register signal-segv Int "SIGSEGV")
|
||||
(register signal-term Int "SIGTERM")
|
||||
(register abort (Fn [] ()) "abort")
|
||||
|
||||
(register errno Int "errno")
|
||||
(register EACCES Int "EACCES")
|
||||
(register EEXIST Int "EEXIST")
|
||||
(register EINVAL Int "EINVAL")
|
||||
(register EIO Int "EIO")
|
||||
(register EISDIR Int "EISDIR")
|
||||
(register ELOOP Int "ELOOP")
|
||||
(register EMFILE Int "EMFILE")
|
||||
(register ENAMETOOLONG Int "ENAMETOOLONG")
|
||||
(register ENOENT Int "ENOENT")
|
||||
(register ENOMEM Int "EINVAL")
|
||||
(register ENOSPC Int "ENOSPC")
|
||||
(register ENOSR Int "ENOSR")
|
||||
(register ENOTDIR Int "ENOTDIR")
|
||||
(register ENXIO Int "ENXIO")
|
||||
(register EOVERFLOW Int "EOVERFLOW")
|
||||
(register EROFS Int "EROFS")
|
||||
(register EINTR Int "EINTR")
|
||||
|
||||
)
|
||||
|
@ -34,7 +34,7 @@
|
||||
@&Int.MAX
|
||||
"test that the address of Int.MAX can be taken")
|
||||
(assert-equal test
|
||||
&(Result.Error @"File “foobar” couldn’t be opened!")
|
||||
&(Result.Error System.ENOENT)
|
||||
&(IO.read->EOF "foobar")
|
||||
"test that reading from an undefined file works")
|
||||
(assert-equal test
|
||||
|
19
test/system.carp
Normal file
19
test/system.carp
Normal file
@ -0,0 +1,19 @@
|
||||
(load "Test.carp")
|
||||
|
||||
(use-all Test IO Result)
|
||||
|
||||
(defn open-existing-file? [filename]
|
||||
(let [f (open-file filename "r")]
|
||||
(match f
|
||||
(Error _) false
|
||||
(Success x) (do
|
||||
(fclose x)
|
||||
true))))
|
||||
|
||||
(deftest test
|
||||
(assert-true test
|
||||
(error? &(open-file "asfasdfafs.txt" "r"))
|
||||
"An error is given when the file doesn't exist")
|
||||
(assert-true test
|
||||
(open-existing-file? "test/system.carp")
|
||||
"files that exist are opened properly"))
|
Loading…
Reference in New Issue
Block a user