Skip to content

Commit

Permalink
added sys.io.File.write support
Browse files Browse the repository at this point in the history
  • Loading branch information
ncannasse committed May 31, 2018
1 parent 4ce6632 commit aad90f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/sys/io/File.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import js.node.Fs;
// @:coreApi
class File {

public static inline function write( path : String, binary : Bool = true ) : FileOutput {
return new FileOutput(Fs.openSync(path, WriteCreate));
}

public static inline function read( path : String, binary : Bool = true ) : FileInput {
return new FileInput(Fs.openSync(path, Read));
}
Expand Down
53 changes: 53 additions & 0 deletions src/sys/io/FileOutput.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sys.io;

import haxe.io.Bytes;
import haxe.io.Eof;
import haxe.io.Error;
import js.node.Buffer;
import js.node.Fs;

@:coreApi
class FileOutput extends haxe.io.Output {
var fd:Int;
var pos:Int;

@:allow(sys.io.File)
function new(fd:Int) {
this.fd = fd;
pos = 0;
}

override public function writeByte( b : Int ) : Void {
var buf = new Buffer(1);
buf[0] = b;
Fs.writeSync(fd, buf, 0, 1, pos);
pos++;
}

override public function writeBytes( s : Bytes, pos : Int, len : Int ) : Int {
var buf = Buffer.hxFromBytes(s);
var wrote = Fs.writeSync(fd, buf, pos, len, this.pos);
this.pos += wrote;
return wrote;
}

override public function close() : Void {
Fs.closeSync(fd);
}

public function seek( p : Int, pos : FileSeek ) : Void {
switch (pos) {
case SeekBegin:
this.pos = p;
case SeekEnd:
this.pos = cast Fs.fstatSync(fd).size + p;
case SeekCur:
this.pos += p;
}
}

public function tell() : Int {
return pos;
}

}

0 comments on commit aad90f2

Please sign in to comment.