|
3423
|
1 # port of StringIO.py using arrays
|
|
|
2 # ArrayIO.py
|
|
|
3 # jjk 02/28/96 001 direct mod of StringIO, test suite checks out
|
|
|
4 # jjk 02/28/96 002 inherit from StringIO, test suite checks out
|
|
|
5 # jjk 02/28/96 003 add __xx__() functions
|
|
|
6 #
|
|
|
7 # class ArrayIO implements file-like objects that read/write a
|
|
|
8 # string buffer (a.k.a. "memory files").
|
|
|
9 #
|
|
|
10 # all methods that interface with ArrayIO pass strings. Internally, however,
|
|
|
11 # ArrayIO uses an array object.
|
|
|
12 #
|
|
|
13 # the interface is the same as StringIO.py
|
|
|
14 # also handles len(a), a[i], a[i]='x', a[i:j], a[i:j] = aString
|
|
|
15 #
|
|
|
16
|
|
|
17 import string
|
|
|
18 import array
|
|
|
19 import StringIO
|
|
|
20
|
|
|
21 class ArrayIO(StringIO.StringIO):
|
|
|
22 # jjk 02/28/96
|
|
|
23 def __init__(self, buf = ''):
|
|
|
24 #jjk 02/28/96
|
|
|
25 self.buf = array.array('c', buf)
|
|
|
26 self.pos = 0
|
|
|
27 self.closed = 0
|
|
|
28 def __len__(self):
|
|
|
29 #jjk 02/28/96
|
|
|
30 return len(self.buf)
|
|
|
31 def __getitem__(self, key):
|
|
|
32 #jjk 02/28/96
|
|
|
33 return self.buf[key]
|
|
|
34 def __setitem__(self, key, item):
|
|
|
35 #jjk 02/28/96
|
|
|
36 self.buf[key] = item
|
|
|
37 def __getslice__(self, i, j):
|
|
|
38 #jjk 02/28/96
|
|
|
39 return self.buf[i:j].tostring()
|
|
|
40 def __setslice__(self, i, j, aString):
|
|
|
41 #jjk 02/28/96
|
|
|
42 self.buf[i:j] = array.array('c', aString)
|
|
|
43 def read(self, n = 0):
|
|
|
44 #jjk 02/28/96
|
|
|
45 r = StringIO.StringIO.read(self, n)
|
|
|
46 return (r.tostring())
|
|
|
47 def _findCharacter(self, char, start):
|
|
|
48 #probably very slow
|
|
|
49 #jjk 02/28/96
|
|
|
50 for i in range(max(start, 0), len(self.buf)):
|
|
|
51 if (self.buf[i] == char):
|
|
|
52 return(i)
|
|
|
53 return(-1)
|
|
|
54 def readline(self):
|
|
|
55 #jjk 02/28/96
|
|
|
56 i = self._findCharacter('\n', self.pos)
|
|
|
57 if i < 0:
|
|
|
58 newpos = len(self.buf)
|
|
|
59 else:
|
|
|
60 newpos = i+1
|
|
|
61 r = self.buf[self.pos:newpos].tostring()
|
|
|
62 self.pos = newpos
|
|
|
63 return r
|
|
|
64 def write(self, s):
|
|
|
65 #jjk 02/28/96
|
|
|
66 if not s: return
|
|
|
67 if self.pos > len(self.buf):
|
|
|
68 self.buf.fromstring('\0'*(self.pos - len(self.buf)))
|
|
|
69 newpos = self.pos + len(s)
|
|
|
70 self.buf[self.pos:newpos] = array.array('c', s)
|
|
|
71 self.pos = newpos
|
|
|
72 def getvalue(self):
|
|
|
73 #jjk 02/28/96
|
|
|
74 return self.buf.tostring()
|
|
|
75
|
|
|
76
|
|
|
77 # A little test suite
|
|
|
78 # identical to test suite in StringIO.py , except for "f = ArrayIO()"
|
|
|
79 # too bad I couldn't inherit this :-)
|
|
|
80
|
|
|
81 def test():
|
|
|
82 import sys
|
|
|
83 if sys.argv[1:]:
|
|
|
84 file = sys.argv[1]
|
|
|
85 else:
|
|
|
86 file = '/etc/passwd'
|
|
|
87 lines = open(file, 'r').readlines()
|
|
|
88 text = open(file, 'r').read()
|
|
|
89 f = ArrayIO()
|
|
|
90 for line in lines[:-2]:
|
|
|
91 f.write(line)
|
|
|
92 f.writelines(lines[-2:])
|
|
|
93 if f.getvalue() != text:
|
|
|
94 raise RuntimeError, 'write failed'
|
|
|
95 length = f.tell()
|
|
|
96 print 'File length =', length
|
|
|
97 f.seek(len(lines[0]))
|
|
|
98 f.write(lines[1])
|
|
|
99 f.seek(0)
|
|
|
100 print 'First line =', `f.readline()`
|
|
|
101 here = f.tell()
|
|
|
102 line = f.readline()
|
|
|
103 print 'Second line =', `line`
|
|
|
104 f.seek(-len(line), 1)
|
|
|
105 line2 = f.read(len(line))
|
|
|
106 if line != line2:
|
|
|
107 raise RuntimeError, 'bad result after seek back'
|
|
|
108 f.seek(len(line2), 1)
|
|
|
109 list = f.readlines()
|
|
|
110 line = list[-1]
|
|
|
111 f.seek(f.tell() - len(line))
|
|
|
112 line2 = f.read()
|
|
|
113 if line != line2:
|
|
|
114 raise RuntimeError, 'bad result after seek back from EOF'
|
|
|
115 print 'Read', len(list), 'more lines'
|
|
|
116 print 'File length =', f.tell()
|
|
|
117 if f.tell() != length:
|
|
|
118 raise RuntimeError, 'bad length'
|
|
|
119 f.close()
|
|
|
120
|
|
|
121 if __name__ == '__main__':
|
|
|
122 test()
|