Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Implemented file io streams. Wrapped fs library.
- Loading branch information
James King
committed
Aug 11, 2013
1 parent
4c35829
commit 2a94714
Showing
8 changed files
with
148 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --@native fs.open | ||
|
|
||
| --@import see.io.InputStream | ||
| --@import see.io.IOException | ||
|
|
||
| --@extends see.io.InputStream | ||
|
|
||
| function FileInputStream:init(path) | ||
| self.handle = fs.open(path.pathString:lstr(), "rb") | ||
| end | ||
|
|
||
| function FileInputStream.__meta:__gc() | ||
| self:close() | ||
| end | ||
|
|
||
| function FileInputStream:read() | ||
| local ret | ||
| try (function() | ||
| ret = self.handle.read() | ||
| end, function(e) | ||
| throw(IOException.new("Could not read file.")) | ||
| end) | ||
| return ret | ||
| end | ||
|
|
||
| function FileInputStream:flush() | ||
| self.handle.flush() | ||
| end | ||
|
|
||
| function FileInputStream:close() | ||
| self.handle.close() | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --@native fs.open | ||
|
|
||
| --@import see.io.InputStream | ||
| --@import see.io.IOException | ||
|
|
||
| --@extends see.io.InputStream | ||
|
|
||
| function FileOutputStream:init(path) | ||
| self.handle = fs.open(path.pathString:lstr(), "wb") | ||
| end | ||
|
|
||
| function FileOutputStream.__meta:__gc() | ||
| self:close() | ||
| end | ||
|
|
||
| function FileOutputStream:write(b) | ||
| try (function() | ||
| self.handle.write(b) | ||
| end, function(e) | ||
| throw(IOException.new("Could not write to file.")) | ||
| end) | ||
| end | ||
|
|
||
| function FileOutputStream:flush() | ||
| self.handle.flush() | ||
| end | ||
|
|
||
| function FileOutputStream:close() | ||
| self.handle.close() | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --@native fs | ||
| --@native unpack | ||
|
|
||
| function Files.list(path) | ||
| return Array.new(unpack(fs.list(path.pathString:lstr()))) | ||
| end | ||
|
|
||
| function Files.exists(path) | ||
| return fs.exists(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.isDir(path) | ||
| return fs.isDir(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.isReadOnly(path) | ||
| return fs.isReadOnly(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.getDrive(path) | ||
| local drive = fs.getDrive(path.pathString:lstr()) | ||
| return drive and String.new(drive) or nil | ||
| end | ||
|
|
||
| function Files.getSize(path) | ||
| return fs.getSize(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.getFreeSpace(path) | ||
| return fs.getFreeSpace(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.makeDir(path) | ||
| fs.makeDir(path.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.move(src, dst) | ||
| fs.move(src.pathString:lstr(), dst.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.copy(src, dst) | ||
| fs.copy(src.pathString:lstr(), dst.pathString:lstr()) | ||
| end | ||
|
|
||
| function Files.delete(path) | ||
| fs.delete(path.pathString:lstr()) | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --@import see.base.Exception | ||
|
|
||
| --@extends see.base.Exception | ||
|
|
||
| function IOException(message) | ||
| Exception.init(self, message) | ||
| end |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --@native fs | ||
|
|
||
| function Path:init(pathString) | ||
| self.pathString = cast(pathString, String) | ||
| end | ||
|
|
||
| function Path:combine(p) | ||
| return Path.new(String.new(fs.combine(self.pathString, p.pathString))) | ||
| end | ||
|
|
||
| function Path:getName() | ||
| return String.new(fs.getName(self.pathString)) | ||
| end | ||
|
|
||
| function Path:toString() | ||
| return String.new(self.pathString) | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| --@import see.hook.Hooks | ||
| --@import see.io.FileOutputStream | ||
| --@import see.io.FileInputStream | ||
| --@import see.io.Path | ||
| --@import see.io.Files | ||
|
|
||
| function Test.main() | ||
| local hook | ||
| local input = true | ||
| local str = String.new() | ||
| hook = Hooks.add("char", function(c) | ||
| if c == "z" then | ||
| input = false | ||
| Hooks.remove(hook) | ||
| else | ||
| str:add(c) | ||
| end | ||
| end) | ||
| local path = Path.new("/fostest") | ||
|
|
||
| while input do | ||
| Hooks.run() | ||
| if not Files.exists(path) then | ||
| System.print("File does not exist!") | ||
| return | ||
| end | ||
|
|
||
| System.print(str) | ||
| local fos = FileOutputStream.new(path) | ||
| fos:write(133) | ||
| fos:close() | ||
|
|
||
| local fis = FileInputStream.new(path) | ||
| System.print(fis:read()) | ||
| fis:close() | ||
| end |