7 # (c) 2005 Straylight/Edgeware
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of the Python interface to mLib.
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 LBUF_CRLF = _LBUF_CRLF
29 LBUF_STRICTCRLF = _LBUF_STRICTCRLF
31 cdef class LineBuffer:
35 def __cinit__(me, lineproc = None, eofproc = None, *hunoz, **hukairz):
36 lbuf_init(&me.b, _lbfunc, <void *>me)
37 me._line = _checkcallable(lineproc, 'line proc')
38 me._eof = _checkcallable(eofproc, 'eof proc')
43 return _tobool(me.b.f & LBUF_ENABLE)
46 if me.b.delim == _LBUF_CRLF or me.b.delim == _LBUF_STRICTCRLF:
49 return chr(me.b.delim)
51 if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF:
60 raise TypeError, 'size must be positive'
61 lbuf_setsize(&me.b, sz)
65 def __set__(me, proc):
66 me._line = _checkcallable(proc, 'line proc')
72 def __set__(me, proc):
73 me._eof = _checkcallable(proc, 'eof proc')
77 if me.b.f & LBUF_ENABLE:
78 raise ValueError, 'already enabled'
79 me.b.f = me.b.f | LBUF_ENABLE
83 if not (me.b.f & LBUF_ENABLE):
84 raise ValueError, 'already disabled'
85 me.b.f = me.b.f & ~LBUF_ENABLE
89 if not (me.b.f & LBUF_ENABLE):
90 raise ValueError, 'buffer disabled'
96 return lbuf_free(&me.b, &p)
102 PyString_AsStringAndSize(str, &p, &len)
104 n = lbuf_free(&me.b, &q)
110 if not (me.b.f & LBUF_ENABLE):
112 lbuf_flush(&me.b, q, n)
113 return PyString_FromStringAndSize(p, len)
119 return _maybecall(me._line, (line,))
121 return _maybecall(me._eof, ())
123 cdef void _lbfunc(char *s, size_t n, void *arg):
129 sb.line(PyString_FromStringAndSize(s, n))
131 #----- That's all, folks ----------------------------------------------------