| 1 | ### -*-pyrex-*- |
| 2 | ### |
| 3 | ### Line buffering |
| 4 | ### |
| 5 | ### (c) 2005 Straylight/Edgeware |
| 6 | ### |
| 7 | |
| 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. |
| 25 | |
| 26 | LBUF_CRLF = _LBUF_CRLF |
| 27 | LBUF_STRICTCRLF = _LBUF_STRICTCRLF |
| 28 | |
| 29 | cdef class LineBuffer: |
| 30 | """ |
| 31 | LineBuffer([lineproc = None], [eofproc = None]) |
| 32 | |
| 33 | Split an incoming stream into lines. |
| 34 | """ |
| 35 | cdef lbuf b |
| 36 | cdef _line |
| 37 | cdef _eof |
| 38 | def __cinit__(me, lineproc = None, eofproc = None, *hunoz, **hukairz): |
| 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: |
| 45 | """LB.activep -> BOOL: is the buffer still active?""" |
| 46 | def __get__(me): |
| 47 | return _tobool(me.b.f & LBUF_ENABLE) |
| 48 | property delim: |
| 49 | """LB.delim -> CHAR | LBUF_...: line-end delimiter""" |
| 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: |
| 61 | """LB.size -> INT: buffer size limit""" |
| 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: |
| 69 | """LB.lineproc -> FUNC: call FUNC(LINE) on each line""" |
| 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: |
| 77 | """LB.eofproc -> FUNC: call FUNC() at end-of-file""" |
| 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): |
| 85 | """LB.enable(): enable the buffer, allowing lines to be emitted""" |
| 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): |
| 92 | """LB.disable(): disable the buffer, suspending line emission""" |
| 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): |
| 99 | """LB.close(): report the end of the input stream""" |
| 100 | if not (me.b.f & LBUF_ENABLE): |
| 101 | raise ValueError, 'buffer disabled' |
| 102 | lbuf_close(&me.b) |
| 103 | return me |
| 104 | property free: |
| 105 | """LB.free -> INT: amount of space remaining in buffer""" |
| 106 | def __get__(me): |
| 107 | cdef char *p |
| 108 | return lbuf_free(&me.b, &p) |
| 109 | def flush(me, str): |
| 110 | """LB.flush(STR) -> insert STR into the buffer and emit lines""" |
| 111 | cdef Py_ssize_t len |
| 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): |
| 128 | """LB.enabled(): called when buffer is enabled""" |
| 129 | pass |
| 130 | def disabled(me): |
| 131 | """LB.disabled(): called when buffer is disabled""" |
| 132 | pass |
| 133 | def line(me, line): |
| 134 | """LB.line(LINE): called for each completed line""" |
| 135 | return _maybecall(me._line, (line,)) |
| 136 | def eof(me): |
| 137 | """LB.eof(): called at end-of-file""" |
| 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 | |
| 148 | ###----- That's all, folks -------------------------------------------------- |