-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.coffee
39 lines (31 loc) · 897 Bytes
/
reader.coffee
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
fs = require 'fs'
class LineReader
# Read lines from a file
constructor: (filename) ->
@_fd = fs.openSync filename, 'r'
@_buffer = ''
@_isEof = false
@_isLoaded = false
readLine: () ->
while @_buffer.indexOf('\n') == -1 and not @_isLoaded
[value, bytesRead] = fs.readSync @_fd, 1024, null
@_buffer += value
if bytesRead is 0
@_isLoaded = true
start = @_buffer.indexOf('\n')
line = ''
if start isnt -1
if start == 0
line = ''
else
line = @_buffer[0..start-1]
@_buffer = @_buffer[start+1..@_buffer.length]
else
line = @_buffer
@_isEof = true
line
isEof: () ->
@_isEof
close: () ->
fs.closeSync @_fd
exports.LineReader = LineReader