Browse Source

add position arguments to File#read and File#write

v0.7.4-release
Ryan 16 years ago
parent
commit
0b1e3240be
  1. 20
      src/file.cc
  2. 14
      src/file.js

20
src/file.cc

@ -351,8 +351,8 @@ File::AfterOpen (eio_req *req)
Handle<Value> Handle<Value>
File::Write (const Arguments& args) File::Write (const Arguments& args)
{ {
if (args.Length() < 1) return Undefined(); if (args.Length() < 2) return Undefined();
if (!args[0]->IsString()) if (!args[1]->IsNumber()) return Undefined();
HandleScope scope; HandleScope scope;
@ -362,14 +362,14 @@ File::Write (const Arguments& args)
size_t length = 0; size_t length = 0;
if (args[0]->IsString()) { if (args[0]->IsString()) {
// utf8 encoded data // utf8 encoding
Local<String> string = args[0]->ToString(); Local<String> string = args[0]->ToString();
length = string->Utf8Length(); length = string->Utf8Length();
buf = static_cast<char*>(malloc(length)); buf = static_cast<char*>(malloc(length));
string->WriteUtf8(buf, length); string->WriteUtf8(buf, length);
} else if (args[0]->IsArray()) { } else if (args[0]->IsArray()) {
// binary data // raw encoding
Local<Array> array = Local<Array>::Cast(args[0]); Local<Array> array = Local<Array>::Cast(args[0]);
length = array->Length(); length = array->Length();
buf = static_cast<char*>(malloc(length)); buf = static_cast<char*>(malloc(length));
@ -383,6 +383,8 @@ File::Write (const Arguments& args)
return Undefined(); return Undefined();
} }
size_t pos = args[1]->Uint32Value();
if (file->handle_->Has(FD_SYMBOL) == false) { if (file->handle_->Has(FD_SYMBOL) == false) {
printf("trying to write to a bad fd!\n"); printf("trying to write to a bad fd!\n");
return Undefined(); return Undefined();
@ -390,9 +392,8 @@ File::Write (const Arguments& args)
int fd = file->GetFD(); int fd = file->GetFD();
// NOTE: -1 offset in eio_write() invokes write() instead of pwrite()
node_eio_warmup(); node_eio_warmup();
eio_req *req = eio_write(fd, buf, length, -1, EIO_PRI_DEFAULT, File::AfterWrite, file); eio_req *req = eio_write(fd, buf, length, pos, EIO_PRI_DEFAULT, File::AfterWrite, file);
return Undefined(); return Undefined();
} }
@ -422,17 +423,18 @@ File::Read (const Arguments& args)
{ {
if (args.Length() < 1) return Undefined(); if (args.Length() < 1) return Undefined();
if (!args[0]->IsNumber()) return Undefined(); if (!args[0]->IsNumber()) return Undefined();
if (!args[1]->IsNumber()) return Undefined();
HandleScope scope; HandleScope scope;
File *file = File::Unwrap(args.Holder()); File *file = File::Unwrap(args.Holder());
size_t length = args[0]->IntegerValue(); size_t length = args[0]->IntegerValue();
size_t pos = args[1]->Uint32Value();
int fd = file->GetFD(); int fd = file->GetFD();
// NOTE: -1 offset in eio_read() invokes read() instead of pread() // NOTE: NULL pointer tells eio to allocate it itself
// NULL pointer tells eio to allocate it itself
node_eio_warmup(); node_eio_warmup();
eio_req *req = eio_read(fd, NULL, length, -1, EIO_PRI_DEFAULT, File::AfterRead, file); eio_req *req = eio_read(fd, NULL, length, pos, EIO_PRI_DEFAULT, File::AfterRead, file);
assert(req); assert(req);
return Undefined(); return Undefined();

14
src/file.js

@ -15,11 +15,14 @@ File.exists = function (path, callback) {
File.cat = function (path, callback) { File.cat = function (path, callback) {
var content = ""; var content = "";
var file = new File; var file = new File;
var pos = 0;
var chunkSize = 10*1024;
function readChunk () { function readChunk () {
file.read(1024, function (status, chunk) { file.read(chunkSize, pos, function (status, chunk) {
if (chunk) { if (chunk) {
content += chunk.encodeUtf8(); content += chunk.encodeUtf8();
pos += chunk.length;
readChunk(); readChunk();
} else { } else {
callback(0, content); callback(0, content);
@ -48,12 +51,12 @@ File.prototype.close = function (callback) {
this._addAction("close", [], callback); this._addAction("close", [], callback);
}; };
File.prototype.write = function (buf, callback) { File.prototype.write = function (buf, pos, callback) {
this._addAction("write", [buf], callback); this._addAction("write", [buf, pos], callback);
}; };
File.prototype.read = function (length, callback) { File.prototype.read = function (length, pos, callback) {
this._addAction("read", [length], callback); this._addAction("read", [length, pos], callback);
}; };
// Some explanation of the File binding. // Some explanation of the File binding.
@ -92,6 +95,7 @@ File._addAction = File.prototype._addAction = function (method, args, callback)
File._act = File.prototype._act = function () { File._act = File.prototype._act = function () {
var action = this._actionQueue[0]; var action = this._actionQueue[0];
if (action) if (action)
// TODO FIXME what if the action throws an error?
this["_ffi_" + action.method].apply(this, action.args); this["_ffi_" + action.method].apply(this, action.args);
}; };

Loading…
Cancel
Save