-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathparser.js
More file actions
118 lines (104 loc) · 3.8 KB
/
parser.js
File metadata and controls
118 lines (104 loc) · 3.8 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Generated by CoffeeScript 1.7.1
(function() {
var EventEmitter, Header, Parser, fs,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
Header = require('./header');
fs = require('fs');
Parser = (function(_super) {
__extends(Parser, _super);
function Parser(filename) {
this.filename = filename;
this.parseField = __bind(this.parseField, this);
this.parseRecord = __bind(this.parseRecord, this);
this.resume = __bind(this.resume, this);
this.pause = __bind(this.pause, this);
this.parse = __bind(this.parse, this);
}
Parser.prototype.parse = function() {
this.emit('start', this);
this.header = new Header(this.filename);
this.header.parse((function(_this) {
return function(err) {
var bufLoc, loc, overflow, sequenceNumber, stream;
_this.emit('header', _this.header);
sequenceNumber = 0;
loc = _this.header.start;
bufLoc = _this.header.start;
overflow = null;
_this.paused = false;
stream = fs.createReadStream(_this.filename);
_this.readBuf = function() {
var buffer;
if (_this.paused) {
_this.emit('paused');
return;
}
while (buffer = stream.read()) {
if (bufLoc !== _this.header.start) {
bufLoc = 0;
}
if (overflow !== null) {
buffer = overflow + buffer;
}
while (loc < (_this.header.start + _this.header.numberOfRecords * _this.header.recordLength) && (bufLoc + _this.header.recordLength) <= buffer.length) {
_this.emit('record', _this.parseRecord(++sequenceNumber, buffer.slice(bufLoc, bufLoc += _this.header.recordLength)));
}
loc += bufLoc;
if (bufLoc < buffer.length) {
overflow = buffer.slice(bufLoc, buffer.length);
} else {
overflow = null;
}
return _this;
}
};
stream.on('readable', _this.readBuf);
return stream.on('end', function() {
return _this.emit('end');
});
};
})(this));
return this;
};
Parser.prototype.pause = function() {
return this.paused = true;
};
Parser.prototype.resume = function() {
this.paused = false;
this.emit('resuming');
return this.readBuf();
};
Parser.prototype.parseRecord = function(sequenceNumber, buffer) {
var field, loc, record, _fn, _i, _len, _ref;
record = {
'@sequenceNumber': sequenceNumber,
'@deleted': (buffer.slice(0, 1))[0] !== 32
};
loc = 1;
_ref = this.header.fields;
_fn = (function(_this) {
return function(field) {
return record[field.name] = _this.parseField(field, buffer.slice(loc, loc += field.length));
};
})(this);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
field = _ref[_i];
_fn(field);
}
return record;
};
Parser.prototype.parseField = function(field, buffer) {
var value;
value = (buffer.toString('utf-8')).replace(/^\x20+|\x20+$/g, '');
if (field.type === 'N') {
value = parseInt(value, 10);
}
return value;
};
return Parser;
})(EventEmitter);
module.exports = Parser;
}).call(this);