-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (43 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var fs = require('fs'), path = require('path');
function readFile(fd, buffer, ins, str) {
fs.read(fd, buffer, 0, buffer.length, null, function (err, bytes, buf) {
var s = buf.slice(0, bytes).toString(), i;
while((i = s.indexOf('\n')) >-1) {
str += s.substr(0, i);
ins.emit('data', str);
str = '';
s = s.substr(i+1);
}
if (bytes === 0) { // end of file
ins.emit('data', str);
ins.emit('end');
fs.close(fd);
} else {
str += s;
readFile(fd, buf, ins, str);
}
});
}
function ReadFileByLine (fname, buffersize) {
if (!fname) {
throw new Error('filename is required.');
}
if (fname[0] !== '/') {
fname = __dirname + '/' + fname;
}
fname = path.normalize(fname);
if (!fs.existsSync(fname)) {
throw new Error('file not exists.');
}
if (!fs.statSync(fname).isFile()) {
throw new Error('Not a file');
}
var fd = fs.openSync(fname, 'r'),
buffer = new Buffer(buffersize || 20);
var that = this;
process.nextTick(function () {
readFile(fd, buffer, that, '');
});
}
require('util').inherits(ReadFileByLine, require('events').EventEmitter);
module.exports = ReadFileByLine;