01: // Copyright (c) 2001 Finn Bock.
02:
03: package org.python.modules;
04:
05: import org.python.core.*;
06:
07: public class xreadlines {
08: private final static int CHUNKSIZE = 8192;
09:
10: public static PyString __doc__xreadlines = new PyString(
11: "xreadlines(f)\n" + "\n"
12: + "Return an xreadlines object for the file f.");
13:
14: public static PyObject xreadlines$(PyObject file) {
15: return new XReadlineObj(file);
16: }
17:
18: public static class XReadlineObj extends PyObject {
19: private PyObject file;
20: private PyObject lines = null;
21: private int lineslen = 0;
22: private int lineno = 0;
23: private int abslineno = 0;
24:
25: public XReadlineObj(PyObject file) {
26: this .file = file;
27: }
28:
29: public PyObject __iter__() {
30: return new PySequenceIter(this );
31: }
32:
33: public PyObject __finditem__(PyObject idx) {
34: return __finditem__(((PyInteger) idx.__int__()).getValue());
35: }
36:
37: public PyObject __finditem__(int idx) {
38: if (idx != abslineno) {
39: throw Py
40: .RuntimeError("xreadlines object accessed out of order");
41: }
42:
43: if (lineno >= lineslen) {
44: lines = file.invoke("readlines", Py
45: .newInteger(CHUNKSIZE));
46: lineno = 0;
47: lineslen = lines.__len__();
48: }
49: abslineno++;
50: return lines.__finditem__(lineno++);
51: }
52:
53: public String toString() {
54: return "<xreadlines object " + Py.idstr(this ) + ">";
55: }
56:
57: }
58: }
|