Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

lineReader does not read last line of file if not terminated with \n #49

Closed
dylanb opened this issue Jan 25, 2013 · 1 comment
Closed

Comments

@dylanb
Copy link

dylanb commented Jan 25, 2013

Or if you have a file with a single line that is not terminated with a \n, then lineReader will behave as if the file is empty.

The following code generates hash values for all files in a file directory. If there are two single line files, their hash will be the same value and this value will also be the same as a file that is totally empty.

(function () {
    'use strict';
    var fs = require('fs'),
        wrench = require('wrench'),
        util = require('util'),
        crypto = require('crypto'),
        allFiles,
        fd,
        ws;

allFiles = wrench.readdirSyncRecursive('.');
allFiles.sort();
allFiles = allFiles.filter(function (element) {
return (element.indexOf('.git') === -1 && element.indexOf('.log') === -1);
});
if (allFiles.indexOf('review') === -1) {
// create the review directory
wrench.mkdirSyncRecursive('review', '0700');
} else {
// don't want to create hashes for ourselves or git files
allFiles.splice(allFiles.indexOf('review'), 1);
allFiles.splice(allFiles.indexOf('review/hashes.txt'), 1);
}
ws = fs.createWriteStream('review/hashes.txt', { flags: 'w', encoding: null, mode: '0600' });
ws.on('open', function (fd) {
var i,
ilen,
name,
stats,
f,
content,
hash,
buffer;
for (i = 0, ilen = allFiles.length; i < ilen; i += 1) {
name = allFiles[i];
stats = fs.statSync(name);
console.log('processing file [' + name + ']');
if (stats.isFile()) {
f = new wrench.LineReader(name);
content = '';
while (f.hasNextLine()) {
content += f.getNextLine();
}
fs.closeSync( f.fd);
hash = crypto
.createHash('md5')
.update(content)
.digest('hex');
buffer = name + '\n' + hash + '\n\n';
console.log(buffer);
ws.write(buffer);
}
}
ws.end();
});


}());

@ryanmcgrath
Copy link
Owner

Months late, but should work. Let me know if you have issues, thanks and sorry!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants