This repository has been archived on 2026-03-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
splash/lib/libraries/line_reader.js

47 lines
1.3 KiB
JavaScript

/* Simple linereader functionality for Node.js
*
* Allows you to iterate through a file line by line. "Nice" functionality.
*
* @Author: Ryan McGrath (ryan@venodesigns.net)
* @Requires: Nothing
*/
var fs = require("fs");
exports.reader = function(filename) {
var bufferSize = 8192, /* Generic for now */
currentPosition = 0,
buffer = "",
fd = fs.openSync(filename, "r");
function getBuffer(position) {
var res = fs.readSync(fd, bufferSize, position, "ascii");
buffer += res[0];
if(res[1] === 0) return -1;
return position + res[1];
}
currentPosition = getBuffer(0);
this.hasNextLine = function() {
while(buffer.indexOf("\n") === -1) {
currentPosition = getBuffer(currentPosition);
if(currentPosition === -1) return false;
}
if(buffer.indexOf("\n") > -1) return true;
return false;
};
this.getNextLine = function() {
var lineEnd = buffer.indexOf("\n"),
result = buffer.substring(0, lineEnd);
buffer = buffer.substring(result.length + 1, buffer.length);
return result;
};
return this;
};