A LineReader, because this kind of stuff should just exist already. ;P
This commit is contained in:
parent
2aa050b815
commit
5476c463c8
3 changed files with 57 additions and 14 deletions
|
|
@ -187,3 +187,40 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.LineReader = function(filename, bufferSize) {
|
||||||
|
this.bufferSize = bufferSize || 8192;
|
||||||
|
this.buffer = "";
|
||||||
|
this.fd = fs.openSync(filename, "r");
|
||||||
|
this.currentPosition = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.LineReader.prototype = {
|
||||||
|
getBufferAndSetCurrentPosition: function(position) {
|
||||||
|
var res = fs.readSync(this.fd, this.bufferSize, position, "ascii");
|
||||||
|
|
||||||
|
this.buffer += res[0];
|
||||||
|
if(res[1] === 0) return -1;
|
||||||
|
|
||||||
|
this.currentPosition = position + res[1];
|
||||||
|
return this.currentPosition;
|
||||||
|
},
|
||||||
|
|
||||||
|
hasNextLine: function() {
|
||||||
|
while(this.buffer.indexOf('\n') === -1) {
|
||||||
|
this.getBufferAndSetCurrentPosition(this.currentPosition);
|
||||||
|
if(this.currentPosition === -1) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.buffer.indexOf("\n") > -1) return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
getNextLine: function() {
|
||||||
|
var lineEnd = this.buffer.indexOf("\n"),
|
||||||
|
result = this.buffer.substring(0, lineEnd);
|
||||||
|
|
||||||
|
this.buffer = this.buffer.substring(result.length + 1, this.buffer.length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "wrench",
|
"name": "wrench",
|
||||||
"description": "Recursive filesystem operations that Node *should* have.",
|
"description": "Recursive filesystem (and other) operations that Node *should* have.",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"author": "Ryan McGrath <ryan@venodesigns.net>",
|
"author": "Ryan McGrath <ryan@venodesigns.net>",
|
||||||
|
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
||||||
30
readme.md
30
readme.md
|
|
@ -1,8 +1,8 @@
|
||||||
wrench.js - Recursive file operations in Node.js
|
wrench.js - Recursive file operations in Node.js
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
While I love Node.js, I've found myself missing some functions. Things like
|
While I love Node.js, I've found myself missing some functions. Things like
|
||||||
recursively deleting/chmodding a directory (or even deep copying a directory)
|
recursively deleting/chmodding a directory (or even deep copying a directory),
|
||||||
shouldn't need to be re-invented time and time again.
|
or even a basic line reader, shouldn't need to be re-invented time and time again.
|
||||||
|
|
||||||
That said, here's my attempt at a re-usable solution, at least until something
|
That said, here's my attempt at a re-usable solution, at least until something
|
||||||
more formalized gets integrated into Node.js (*hint hint*). wrench.js is fairly simple
|
more formalized gets integrated into Node.js (*hint hint*). wrench.js is fairly simple
|
||||||
|
|
@ -15,19 +15,25 @@ Installation
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
var wrench = require("./wrench");
|
``` javascript
|
||||||
|
var wrench = require('./wrench'),
|
||||||
|
util = require('util');
|
||||||
|
|
||||||
// Recursively delete the entire sub-tree of a directory, then kill the directory
|
// Recursively delete the entire sub-tree of a directory, then kill the directory
|
||||||
wrench.rmdirSyncRecursive("my_directory_name");
|
wrench.rmdirSyncRecursive('my_directory_name');
|
||||||
|
|
||||||
// Recursively chmod the entire sub-tree of a directory
|
// Recursively chmod the entire sub-tree of a directory
|
||||||
wrench.chmodSyncRecursive("my_directory_name", 0755);
|
wrench.chmodSyncRecursive('my_directory_name', 0755);
|
||||||
|
|
||||||
// Deep-copy an existing directory
|
// Deep-copy an existing directory
|
||||||
wrench.copyDirSyncRecursive("directory_to_copy", "location_where_copy_should_end_up");
|
wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up');
|
||||||
|
|
||||||
It should be noted that these are all currently synchronous operations. I'll be adding
|
// Read lines in from a file until you hit the end
|
||||||
asynchronous versions of chmod/copy in the near future, but rmdir is one that really can't
|
var f = new wrench.LineReader('x.txt');
|
||||||
exist in an asynchronous fashion.
|
while(f.hasNextLine()) {
|
||||||
|
util.puts(x.getNextLine());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
It should be noted that these are all currently synchronous operations.
|
||||||
|
|
||||||
Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath)
|
Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath)
|
||||||
|
|
|
||||||
Reference in a new issue