Comparing version 0.0.6 to 0.0.7
@@ -24,8 +24,12 @@ /** | ||
if (typeof stream === 'string') stream = new FileReader(stream); | ||
stream.pause(); | ||
this.fp = stream; | ||
this._readbuf = ""; | ||
this._readoffset = 0; | ||
this._eof = false; | ||
var self = this; | ||
if (typeof stream === 'string') this.fp = new FileReader(stream); | ||
stream.pause(); | ||
stream.on('error', function(err) { throw err; }); | ||
stream.on('end', function() { self._eof = true; }); | ||
} | ||
@@ -43,2 +47,6 @@ //util.inherits(Fgets, EventEmitter); // TBD | ||
} | ||
else if (this._eof) { | ||
// last line of data is not newline terminated... | ||
// FIXME: return it? discard it? | ||
} | ||
else { | ||
@@ -50,2 +58,6 @@ this._fillbuf(); | ||
Fgets.prototype.feof = function feof( ) { | ||
return this._eof && this._readoffset >= this._readbuf.length; | ||
}; | ||
Fgets.prototype._fillbuf = function _fillbuf( ) { | ||
@@ -58,3 +70,3 @@ if (this._readoffset > 0) { | ||
var data = this.fp.read(); | ||
if (data) this._readbuf += data.toString(); | ||
if (data) this._readbuf += data + ""; | ||
}; |
@@ -13,6 +13,14 @@ /** | ||
fs = require('fs'); | ||
util = require('util'); | ||
EventEmitter = require('events').EventEmitter; | ||
function FileReader( filename ) { | ||
'use strict'; | ||
EventEmitter.call(this); | ||
// outperforms default createReadStream 50%, but is only 10% faster | ||
// when created with highWaterMark: 409600. | ||
//return new fs.createReadStream(filename, {highWaterMark: 409600}); | ||
//return new fs.createReadStream(filename); | ||
if (!this instanceof FileReader) return new FileReader(filename); | ||
@@ -26,2 +34,3 @@ | ||
this.read = function() { return null; }; | ||
this._eof = false; | ||
@@ -41,3 +50,7 @@ var self = this; | ||
if (err) throw err; | ||
self.str += buffer.toString(); | ||
self.str += buffer.toString('utf8', 0, nBytes); | ||
if (nBytes === 0 && !self._eof) { | ||
self._eof = true; | ||
self.emit('end'); | ||
} | ||
}); | ||
@@ -50,6 +63,6 @@ var ret = self.str; | ||
// streams-like pause and resume stubs, for fgets plug-in compatibility | ||
// FileReader only emits 'end' events | ||
this.pause = function() { }; | ||
this.resume = function() { }; | ||
this.on = function() { }; | ||
} | ||
util.inherits(FileReader, EventEmitter); |
{ | ||
"name": "arlib", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Andras' Utility Functions", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -27,5 +27,5 @@ # arlib | ||
set as properties. Like traditional unix, getopt only checks for option | ||
switches at the beginning of the argument list, preceding non-switch | ||
arguments. It recognizes '-' as a filename and '--' as the special marker | ||
that stops further argument scanning. | ||
switches at the beginning of the argument list, before non-switch arguments. | ||
It recognizes '-' as a filename and '--' as the special marker that stops | ||
further argument scanning. | ||
@@ -47,6 +47,4 @@ var getopt = require('arlib/getopt').getopt; | ||
var mongoid = require('arlib').mongoid; | ||
id = mongoid(); | ||
id = mongoid(); | ||
// 543f376340e2816497000001 | ||
// 543f376340e2816497000002 | ||
id = mongoid(); // 543f376340e2816497000001 | ||
id = mongoid(); // 543f376340e2816497000002 | ||
@@ -56,11 +54,11 @@ // id factory, configured for the unique system identifier 1 | ||
var idFactory = new MongoId(1); | ||
id = idFactory.fetch(); | ||
id = idFactory.fetch(); | ||
// 543f3789000001649f000001 | ||
// 543f3789000001649f000002 | ||
id = idFactory.fetch(); // 543f3789000001649f000001 | ||
id = idFactory.fetch(); // 543f3789000001649f000002 | ||
### Fgets | ||
synchronous line-at-a-time stream reader. Returns the next buffered line | ||
or the empty string "" when the buffer is empty. | ||
synchronous line-at-a-time stream reader. Returns the next buffered line or | ||
the empty string "" if the buffer is currently empty. 3x faster than | ||
require('readline'), and works like C fgets(), it doesn't modify the input. | ||
Note: the caller must periodically yield to allow the buffer to fill. | ||
@@ -72,5 +70,15 @@ var fs = require('fs'); | ||
#### feof | ||
returns true when fgets has no more lines to return | ||
var Fgets = require('arlib').Fgets; | ||
var fp = new Fgets('/etc/motd'); // use buit-in FileReader | ||
var contents = ""; | ||
while (!fp.feof()) contents += fp.fgets(); | ||
#### FileReader | ||
fast file reader to feed data to fgets, 30% faster than read streams. | ||
fast file reader to feed data to fgets. A smidge faster than a read stream | ||
created with a reasonable highWaterMark (50% faster than with defaults) | ||
@@ -77,0 +85,0 @@ var FileReader = require('arlib').FileReader; |
21398
471
84