Carp/core/IO.carp

41 lines
1.6 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
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-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.")
2017-09-08 13:24:57 +03:00
(register exit (Fn [Int] a))
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.")
2018-03-18 19:51:23 +03:00
(register fopen (Fn [&String &String] (Ptr FILE)))
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")
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 [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))))
2017-10-20 18:00:47 +03:00
)