1
|
|
2
|
'''
|
3
|
Created on Dec 11, 2015
|
4
|
|
5
|
@author: dewey dunnington <[email protected]>
|
6
|
'''
|
7
|
|
8
|
|
9
|
from PyQt4.QtCore import QTextStream, QString
|
10
|
|
11
|
|
12
|
class MacAwareQTextStream(QTextStream):
|
13
|
|
14
|
def __init__(self, qstring):
|
15
|
QTextStream.__init__(self, qstring)
|
16
|
self.newline = None
|
17
|
|
18
|
|
19
|
|
20
|
def readLineWithNewline(self, newline, bufferlen=128, prefix=QString("")):
|
21
|
start = self.pos()
|
22
|
buf = self.read(bufferlen)
|
23
|
if buf.contains(newline):
|
24
|
pos = buf.indexOf(newline)
|
25
|
line = buf.left(pos)
|
26
|
if not self.seek(start+pos+newline.length()):
|
27
|
self.seek(line.length())
|
28
|
return line.prepend(prefix)
|
29
|
elif buf.length() < bufferlen:
|
30
|
|
31
|
return buf.prepend(prefix)
|
32
|
else:
|
33
|
|
34
|
return self.readLineWithNewline(newline, bufferlen*2, buf.prepend(prefix))
|
35
|
|
36
|
def findNewline(self, bufferlen):
|
37
|
buf = self.read(bufferlen)
|
38
|
if buf.contains("\r\n"):
|
39
|
return QString("\r\n")
|
40
|
elif buf.contains("\n"):
|
41
|
return QString("\n")
|
42
|
elif buf.contains("\r"):
|
43
|
buf2 = self.read(1)
|
44
|
if buf2 == "\n":
|
45
|
return QString("\r\n")
|
46
|
else:
|
47
|
return QString("\r")
|
48
|
elif self.atEnd():
|
49
|
return QString("\n")
|
50
|
else:
|
51
|
return self.findNewline(bufferlen*2)
|
52
|
|
53
|
def readLine(self):
|
54
|
bufferlen = 1024
|
55
|
if self.newline is None:
|
56
|
|
57
|
self.newline = self.findNewline(bufferlen)
|
58
|
self.seek(0)
|
59
|
return self.readLineWithNewline(self.newline, bufferlen)
|
60
|
|
61
|
|
62
|
maclines = QString("line1\rline2\rline3")
|
63
|
windowslines = QString("line1\r\nline2\r\nline3")
|
64
|
unixlines = QString("line1\nline2\nline3")
|
65
|
|
66
|
|
67
|
print "Windows lines"
|
68
|
qts = MacAwareQTextStream(windowslines)
|
69
|
while not qts.atEnd():
|
70
|
print qts.readLineWithNewline(QString("\r\n"))
|
71
|
print "end"
|
72
|
qts.seek(0)
|
73
|
while not qts.atEnd():
|
74
|
print qts.readLine()
|
75
|
print "end\n"
|
76
|
|
77
|
print "mac lines"
|
78
|
qts = MacAwareQTextStream(maclines)
|
79
|
while not qts.atEnd():
|
80
|
print qts.readLineWithNewline(QString("\r"))
|
81
|
print "end"
|
82
|
|
83
|
qts.seek(0)
|
84
|
while not qts.atEnd():
|
85
|
print qts.readLine()
|
86
|
print "end\n"
|
87
|
|
88
|
print "unix lines"
|
89
|
qts = MacAwareQTextStream(unixlines)
|
90
|
while not qts.atEnd():
|
91
|
print qts.readLineWithNewline(QString("\n"))
|
92
|
print "end"
|
93
|
qts.seek(0)
|
94
|
while not qts.atEnd():
|
95
|
print qts.readLine()
|
96
|
print "end\n"
|
97
|
|