Permalink
Browse files

Merge branch 'master' of ssh://github.com/nddrylliog/rock

  • Loading branch information...
2 parents ef1dff9 + 0df52d1 commit 38c85ab1e0f1ba31ab5567c194a3327ffd996f27 @fredreichbier fredreichbier committed Sep 9, 2011
Showing with 28 additions and 4 deletions.
  1. +14 −4 sdk/io/FileWriter.ooc
  2. +14 −0 sdk/lang/IO.ooc
View
@@ -29,6 +29,10 @@ FileWriter: class extends Writer {
init(fileObject, false)
}
+ init: func ~withFileAndFlags (fileObject: File, flags: Int) {
+ init(fileObject getPath(), "wb", flags)
+ }
+
/**
Create a new file writer on the given file path.
@param append If true, appends to the file. If false, overwrites it.
@@ -40,19 +44,25 @@ FileWriter: class extends Writer {
init(fileName, append ? "ab" : "wb")
}
- init: func ~withMode (fileName: String, mode: String) {
+ init: func ~withMode (fileName, mode: String) {
file = FStream open(fileName, mode)
if (!file) {
+ // TODO: that's wrong - many other things could happen. Use strerror instead.
+ Exception new(This, "File not found: " + fileName) throw()
+ }
+ }
+
+ init: func ~withModeAndFlags (fileName, mode: String, flags: Int) {
+ file = FStream open(fileName, mode, flags)
+ if (!file) {
Exception new(This, "File not found: " + fileName) throw()
}
}
/**
Create a new file writer from a given FStream
*/
- init: func ~withFStream (=file) {
-
- }
+ init: func ~withFStream (=file) {}
/**
Create a new file writer on the given file path, overwriting it.
View
@@ -15,6 +15,9 @@ println: func {
}
// input/output
+open: extern func (Char*, Int, ...) -> Int
+fdopen: extern func (Int, Char*) -> FStream
+
printf: extern func (Char*, ...) -> Int
fprintf: extern func (FStream, Char*, ...) -> Int
@@ -58,13 +61,19 @@ FILE: extern cover
EOF: extern Int
+EAGAIN, EWOULDBLOCK, EBADF, EDESTADDRREQ, EFAULT, EFBIG, EINTR, EINVAL, EIO, ENOSPC, EPIPE: extern Int
+
/**
* Low-level interface with the C I/O API.
*
* FileWriter and FileReader use this cover to implement their functionality,
* so that for non-C backends, it will be easier to reimplement them
*/
FStream: cover from FILE* {
+
+ NON_BLOCKING: extern(O_NONBLOCK) static Int
+ READ_ONLY: extern(O_RDONLY) static Int
+ WRITE_ONLY: extern(O_WRONLY) static Int
/**
* Open a file with the given mode
@@ -81,6 +90,11 @@ FStream: cover from FILE* {
fopen(filename, mode)
}
+ open: static func ~withFlags (filename, mode: const String, flags: Int) -> This {
+ fd := open(filename, flags)
+ fdopen(fd, mode)
+ }
+
/**
* Close this file descriptor
*/

0 comments on commit 38c85ab

Please sign in to comment.