mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 13:37:57 +03:00
63 lines
2.7 KiB
Plaintext
63 lines
2.7 KiB
Plaintext
(system-include "carp_io.h")
|
||
|
||
(register-type FILE)
|
||
|
||
(defmodule IO
|
||
(doc println "prints a string ref to stdout, appends a newline.")
|
||
(register println (Fn [(Ref String)] ()))
|
||
(doc print "prints a string ref to stdout, does not append a newline.")
|
||
(register print (Fn [(Ref String)] ()))
|
||
(doc errorln "prints a string ref to stderr, appends a newline.")
|
||
(register errorln (Fn [(Ref String)] ()))
|
||
(doc error "prints a string ref to stderr, does not append a newline.")
|
||
(register print (Fn [(Ref String)] ()))
|
||
(doc get-line "gets a line from stdin.")
|
||
(register get-line (Fn [] String))
|
||
(doc get-char "gets a character from stdin.")
|
||
(register get-char (Fn [] Char) "getchar")
|
||
(doc read-file "returns the contents of a file passed as argument as a string.")
|
||
(register read-file (Fn [&String] String))
|
||
(doc exit "exit the current program with a return code.")
|
||
(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.")
|
||
(register fopen (Fn [&String &String] (Ptr FILE)))
|
||
(doc fclose "closes a file pointer.")
|
||
(register fclose (Fn [(Ptr FILE)] ()))
|
||
(doc fgetc "gets a character from a file pointer.")
|
||
(register fgetc (Fn [(Ptr FILE)] Char))
|
||
(doc fwrite "writes to a file pointer.")
|
||
(register fwrite (Fn [a Int Int (Ptr FILE)] ()) "fwrite")
|
||
(doc fread "reads from a file pointer into a pointer.")
|
||
(register fread (Fn [a Int Int (Ptr FILE)] Int) "fread")
|
||
(doc fflush "flushes a file pointer (i.e. commits every write).")
|
||
(register fflush (Fn [(Ptr FILE)] ()) "fflush")
|
||
(doc rewind "rewinds a file pointer (i.e. puts input and output stream to beginning).")
|
||
(register rewind (Fn [(Ptr FILE)] ()) "rewind")
|
||
(doc unlink "unlinks a file (i.e. deletes it).")
|
||
(register unlink (Fn [String] ()) "unlink")
|
||
(doc fseek "sets the position indicator of a file.")
|
||
(register fseek (Fn [(Ptr FILE) Int Int] ()) "fseek")
|
||
(doc ftell "gets the position indicator of a file.")
|
||
(register ftell (Fn [(Ptr FILE)] Int) "ftell")
|
||
|
||
(register SEEK-SET Int "SEEK_SET")
|
||
(register SEEK-CUR Int "SEEK_CUR")
|
||
(register SEEK-END Int "SEEK_END")
|
||
|
||
(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)))))))
|
||
)
|