-
Notifications
You must be signed in to change notification settings - Fork 3
/
grep4.js
37 lines (34 loc) · 1.09 KB
/
grep4.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
//#/usr/bin/env node
//hace un grep de stdin hasta EOF
//uso: node grep.js fileName 'regExp'
var sys= require("sys");
var fs= require('fs');
var fd= fs.openSync(process.argv[2], "r");
var pos= 0;
var buffer= [];
var veces= 0;
var ctr= 0;
(function loop () {
fs.read(fd, 2*65536, pos, "utf8", function (err, data, bytesRead) {
if (!bytesRead) {
procesarBuffer();
ctr+= ER.test(buffer);
process.stdout.write([ctr, veces, pos]+ "\n");
return;
}
//sys.puts([ctr, veces++, pos]);
veces++;
pos+= bytesRead;
//buffer.push(data);
//process.nextTick(loop);
loop();
//process.nextTick(procesarBuffer);
});
})();
var ER= new RegExp(process.argv[3], "g"); //La RegExp que nos han pasado
function procesarBuffer (newLinePos) { //Esto se ejecuta mientras estamos esperando a que lleguen más datos
while ((newLinePos= buffer.indexOf("\n")) >= 0) { //Vamos cortando por los saltos de línea: esto es una especie de readLine()
ctr+= ER.test(buffer.substr(0, newLinePos));
buffer= buffer.substr(newLinePos+1);
}
}