Carp/core/IO.carp
2018-05-12 16:08:48 +02:00

41 lines
1.6 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 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 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))))
)