Carp/core/IO.carp

90 lines
3.7 KiB
Plaintext

(system-include "carp_io.h")
(register-type FILE)
(defmodule IO
(register stdin (Ptr FILE) "stdin")
(register stdout (Ptr FILE) "stdout")
(register stderr (Ptr FILE) "stderr")
(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 error (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] ()) "exit")
(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. 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.")
(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 [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)))))))
(private getenv-)
(hidden getenv-)
(register getenv- (Fn [String] (Ptr CChar)) "getenv")
(defn getenv [s]
(let [e (getenv- s)]
(if (null? e)
(Maybe.Nothing)
(Maybe.Just (from-cstr e)))))
)
(defmacro println* [:rest forms]
`(IO.println %(build-str* forms)))
(defmacro print* [:rest forms]
`(IO.print %(build-str* forms)))