Carp/core/IO.carp

90 lines
3.7 KiB
Plaintext
Raw Normal View History

2018-01-24 18:08:18 +03:00
(system-include "carp_io.h")
2018-01-24 17:53:18 +03:00
(register-type FILE)
2017-06-26 12:15:03 +03:00
(defmodule IO
2019-07-29 22:50:08 +03:00
(register stdin (Ptr FILE) "stdin")
(register stdout (Ptr FILE) "stdout")
(register stderr (Ptr FILE) "stderr")
2018-05-09 01:43:07 +03:00
(doc println "prints a string ref to stdout, appends a newline.")
2017-06-26 12:15:03 +03:00
(register println (Fn [(Ref String)] ()))
2018-05-09 01:43:07 +03:00
(doc print "prints a string ref to stdout, does not append a newline.")
2017-06-26 12:15:03 +03:00
(register print (Fn [(Ref String)] ()))
2018-10-23 00:47:40 +03:00
(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.")
2019-10-28 16:28:37 +03:00
(register error (Fn [(Ref String)] ()))
2018-05-09 01:43:07 +03:00
(doc get-line "gets a line from stdin.")
2017-06-26 12:15:03 +03:00
(register get-line (Fn [] String))
2018-05-09 01:43:07 +03:00
(doc get-char "gets a character from stdin.")
2018-03-11 16:53:50 +03:00
(register get-char (Fn [] Char) "getchar")
2018-05-09 01:43:07 +03:00
(doc read-file "returns the contents of a file passed as argument as a string.")
2017-11-28 12:07:46 +03:00
(register read-file (Fn [&String] String))
2018-05-09 01:43:07 +03:00
(doc exit "exit the current program with a return code.")
(register exit (Fn [Int] ()) "exit")
2018-03-23 18:24:32 +03:00
(register EOF Char)
2018-05-09 01:43:07 +03:00
(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.")
2018-03-18 19:51:23 +03:00
(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))))
2018-05-09 01:43:07 +03:00
(doc fclose "closes a file pointer.")
2018-03-23 18:24:32 +03:00
(register fclose (Fn [(Ptr FILE)] ()))
2018-05-09 01:43:07 +03:00
(doc fgetc "gets a character from a file pointer.")
2018-03-23 18:24:32 +03:00
(register fgetc (Fn [(Ptr FILE)] Char))
2018-05-09 01:43:07 +03:00
(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")
2018-10-18 18:22:35 +03:00
(doc rewind "rewinds a file pointer (i.e. puts input and output stream to beginning).")
(register rewind (Fn [(Ptr FILE)] ()) "rewind")
2018-10-18 18:22:35 +03:00
(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")
2018-03-23 18:24:32 +03:00
2018-05-09 01:43:07 +03:00
(doc read->EOF "reads a file given by name until the End-Of-File character is reached.")
2018-03-23 18:24:32 +03:00
(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)))))))
2020-01-25 16:23:14 +03:00
(private getenv-)
(hidden getenv-)
2020-05-11 17:10:35 +03:00
(register getenv- (Fn [String] (Ptr CChar)) "getenv")
2020-01-25 16:23:14 +03:00
(defn getenv [s]
(let [e (getenv- s)]
(if (null? e)
(Maybe.Nothing)
(Maybe.Just (from-cstr e)))))
2017-10-20 18:00:47 +03:00
)
(defmacro println* [:rest forms]
`(IO.println %(build-str* forms)))
(defmacro print* [:rest forms]
`(IO.print %(build-str* forms)))