diff --git a/doc/site/modules/io/directory.markdown b/doc/site/modules/io/directory.markdown new file mode 100644 index 00000000..0e41a33b --- /dev/null +++ b/doc/site/modules/io/directory.markdown @@ -0,0 +1,10 @@ +^title Directory Class + +A directory on the file system. + +## Static Methods + +### Directory.**list**(path) + +Lists the contents of the directory at `path`. Returns a sorted list of path +strings for all of the contents of the directory. diff --git a/doc/site/modules/io/file.markdown b/doc/site/modules/io/file.markdown index b9b690cd..60a23f63 100644 --- a/doc/site/modules/io/file.markdown +++ b/doc/site/modules/io/file.markdown @@ -1,7 +1,66 @@ ^title File Class -**TODO** +Lets you work with files on the file system. An instance of this class +represents an open file with a file descriptor. + +When you are done with a file object, it's a good idea to explicitly close it. +If you don't, the GC will close it when the file is no longer used and gets +finalized, but that may take a while. In the meantime, leaving it open wastes +a file descriptor. + +## Static Methods + +### File.**open**(path, fn) + +Opens the file at [path] and passes it to [fn]. After the function returns, the +file is automatically closed. + + :::wren + File.open("words.txt") {|file| + file.readBytes(5) + } + +### File.**read**(path) + +Reads the entire contents of the file at [path] and returns it as a string. + + :::wren + File.read("words.txt") + +The encoding or decoding is done. If the file is UTF-8, then the resulting +string will be a UTF-8 string. Otherwise, it will be a string of bytes in +whatever encoding the file uses. + +### File.**size**(path) + +Returns the size in bytes of the contents of the file at [path]. + +## Constructors + +### File.**open**(path) + +Opens the file at [path] for reading. ## Methods -**TODO** +### **descriptor** + +The numeric file descriptor used to access the file. + +### **isOpen** + +Whether the file is still open or has been closed. + +### **size** + +The size of the contents of the file in bytes. + +### **close**() + +Closes the file. After calling this, you can read or write from it. + +### **readBytes**(count) + +Reads up to [count] bytes starting from the beginning of the file. + +(Allowing an offset to read elsewhere from the file isn't implemented yet.) diff --git a/doc/site/modules/io/index.markdown b/doc/site/modules/io/index.markdown index ba41efbd..1dc81f49 100644 --- a/doc/site/modules/io/index.markdown +++ b/doc/site/modules/io/index.markdown @@ -1,6 +1,7 @@ ^title Module "io" -**TODO** +Provides access to operating system streams and the file system. +* [Directory](directory.html) * [File](file.html) * [Stdin](stdin.html) diff --git a/doc/site/modules/io/stdin.markdown b/doc/site/modules/io/stdin.markdown index 399a9af5..66377f85 100644 --- a/doc/site/modules/io/stdin.markdown +++ b/doc/site/modules/io/stdin.markdown @@ -1,7 +1,12 @@ ^title Stdin Class -**TODO** +The standard input stream. -## Methods +## Static Methods -**TODO** +### **readLine**() + +Reads one line of input from stdin. Blocks the current fiber until a full line +of input has been received. + +Returns the string of input or `null` if stdin is closed. diff --git a/doc/site/modules/io/template.html b/doc/site/modules/io/template.html index ab36a796..ebce39a1 100644 --- a/doc/site/modules/io/template.html +++ b/doc/site/modules/io/template.html @@ -27,6 +27,7 @@

io classes

diff --git a/src/module/io.wren b/src/module/io.wren index 9694d2f9..a4135a15 100644 --- a/src/module/io.wren +++ b/src/module/io.wren @@ -51,6 +51,8 @@ foreign class File { Scheduler.runNextScheduled_() } + foreign descriptor + isOpen { descriptor != -1 } size { @@ -74,7 +76,6 @@ foreign class File { foreign static sizePath_(path, fiber) foreign close_(fiber) - foreign descriptor foreign readBytes_(count, fiber) foreign size_(fiber) }