Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### Line buffering | |
4 | ### | |
5 | ### (c) 2005 Straylight/Edgeware | |
6 | ### | |
579d0169 | 7 | |
5b1830f3 MW |
8 | ###----- Licensing notice --------------------------------------------------- |
9 | ### | |
10 | ### This file is part of the Python interface to mLib. | |
11 | ### | |
12 | ### mLib/Python is free software; you can redistribute it and/or modify | |
13 | ### it under the terms of the GNU General Public License as published by | |
14 | ### the Free Software Foundation; either version 2 of the License, or | |
15 | ### (at your option) any later version. | |
16 | ### | |
17 | ### mLib/Python is distributed in the hope that it will be useful, | |
18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ### GNU General Public License for more details. | |
21 | ### | |
22 | ### You should have received a copy of the GNU General Public License | |
23 | ### along with mLib/Python; if not, write to the Free Software Foundation, | |
24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
579d0169 | 25 | |
26 | LBUF_CRLF = _LBUF_CRLF | |
27 | LBUF_STRICTCRLF = _LBUF_STRICTCRLF | |
28 | ||
29 | cdef class LineBuffer: | |
addc0c37 MW |
30 | """ |
31 | LineBuffer([lineproc = None], [eofproc = None]) | |
32 | ||
33 | Split an incoming stream into lines. | |
34 | """ | |
579d0169 | 35 | cdef lbuf b |
36 | cdef _line | |
37 | cdef _eof | |
376ad06d | 38 | def __cinit__(me, lineproc = None, eofproc = None, *hunoz, **hukairz): |
579d0169 | 39 | lbuf_init(&me.b, _lbfunc, <void *>me) |
40 | me._line = _checkcallable(lineproc, 'line proc') | |
41 | me._eof = _checkcallable(eofproc, 'eof proc') | |
42 | def __dealloc__(me): | |
43 | lbuf_destroy(&me.b) | |
44 | property activep: | |
addc0c37 | 45 | """LB.activep -> BOOL: is the buffer still active?""" |
579d0169 | 46 | def __get__(me): |
47 | return _tobool(me.b.f & LBUF_ENABLE) | |
48 | property delim: | |
addc0c37 | 49 | """LB.delim -> CHAR | LBUF_...: line-end delimiter""" |
579d0169 | 50 | def __get__(me): |
51 | if me.b.delim == _LBUF_CRLF or me.b.delim == _LBUF_STRICTCRLF: | |
52 | return me.b.delim | |
53 | else: | |
54 | return chr(me.b.delim) | |
55 | def __set__(me, d): | |
56 | if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF: | |
57 | me.b.delim = d | |
58 | else: | |
59 | me.b.delim = ord(d) | |
60 | property size: | |
addc0c37 | 61 | """LB.size -> INT: buffer size limit""" |
579d0169 | 62 | def __get__(me): |
63 | return me.b.sz | |
64 | def __set__(me, sz): | |
65 | if sz <= 0: | |
66 | raise TypeError, 'size must be positive' | |
67 | lbuf_setsize(&me.b, sz) | |
68 | property lineproc: | |
addc0c37 | 69 | """LB.lineproc -> FUNC: call FUNC(LINE) on each line""" |
579d0169 | 70 | def __get__(me): |
71 | return me._line | |
72 | def __set__(me, proc): | |
73 | me._line = _checkcallable(proc, 'line proc') | |
74 | def __del__(me): | |
75 | me._line = None | |
76 | property eofproc: | |
addc0c37 | 77 | """LB.eofproc -> FUNC: call FUNC() at end-of-file""" |
579d0169 | 78 | def __get__(me): |
79 | return me._eof | |
80 | def __set__(me, proc): | |
81 | me._eof = _checkcallable(proc, 'eof proc') | |
82 | def __del__(me): | |
83 | me._eof = None | |
84 | def enable(me): | |
addc0c37 | 85 | """LB.enable(): enable the buffer, allowing lines to be emitted""" |
579d0169 | 86 | if me.b.f & LBUF_ENABLE: |
87 | raise ValueError, 'already enabled' | |
88 | me.b.f = me.b.f | LBUF_ENABLE | |
89 | me.enabled() | |
90 | return me | |
91 | def disable(me): | |
addc0c37 | 92 | """LB.disable(): disable the buffer, suspending line emission""" |
579d0169 | 93 | if not (me.b.f & LBUF_ENABLE): |
94 | raise ValueError, 'already disabled' | |
95 | me.b.f = me.b.f & ~LBUF_ENABLE | |
96 | me.disabled() | |
97 | return me | |
98 | def close(me): | |
addc0c37 | 99 | """LB.close(): report the end of the input stream""" |
579d0169 | 100 | if not (me.b.f & LBUF_ENABLE): |
101 | raise ValueError, 'buffer disabled' | |
102 | lbuf_close(&me.b) | |
103 | return me | |
104 | property free: | |
addc0c37 | 105 | """LB.free -> INT: amount of space remaining in buffer""" |
579d0169 | 106 | def __get__(me): |
107 | cdef char *p | |
108 | return lbuf_free(&me.b, &p) | |
109 | def flush(me, str): | |
addc0c37 | 110 | """LB.flush(STR) -> insert STR into the buffer and emit lines""" |
78911cdb | 111 | cdef Py_ssize_t len |
579d0169 | 112 | cdef char *p |
113 | cdef char *q | |
114 | cdef size_t n | |
115 | PyString_AsStringAndSize(str, &p, &len) | |
116 | while len > 0: | |
117 | n = lbuf_free(&me.b, &q) | |
118 | if n > len: | |
119 | n = len | |
120 | memcpy(q, p, n) | |
121 | p = p + n | |
122 | len = len - n | |
123 | if not (me.b.f & LBUF_ENABLE): | |
124 | break | |
125 | lbuf_flush(&me.b, q, n) | |
126 | return PyString_FromStringAndSize(p, len) | |
127 | def enabled(me): | |
addc0c37 | 128 | """LB.enabled(): called when buffer is enabled""" |
579d0169 | 129 | pass |
130 | def disabled(me): | |
addc0c37 | 131 | """LB.disabled(): called when buffer is disabled""" |
579d0169 | 132 | pass |
133 | def line(me, line): | |
addc0c37 | 134 | """LB.line(LINE): called for each completed line""" |
579d0169 | 135 | return _maybecall(me._line, (line,)) |
136 | def eof(me): | |
addc0c37 | 137 | """LB.eof(): called at end-of-file""" |
579d0169 | 138 | return _maybecall(me._eof, ()) |
139 | ||
140 | cdef void _lbfunc(char *s, size_t n, void *arg): | |
141 | cdef LineBuffer sb | |
142 | sb = <LineBuffer>arg | |
143 | if s is NULL: | |
144 | sb.eof() | |
145 | else: | |
146 | sb.line(PyString_FromStringAndSize(s, n)) | |
147 | ||
5b1830f3 | 148 | ###----- That's all, folks -------------------------------------------------- |