chiark / gitweb /
debian/control: Fix the Build-Depends.
[mLib-python] / lbuf.pyx
CommitLineData
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
26LBUF_CRLF = _LBUF_CRLF
27LBUF_STRICTCRLF = _LBUF_STRICTCRLF
28
29cdef class LineBuffer:
30 cdef lbuf b
31 cdef _line
32 cdef _eof
376ad06d 33 def __cinit__(me, lineproc = None, eofproc = None, *hunoz, **hukairz):
579d0169 34 lbuf_init(&me.b, _lbfunc, <void *>me)
35 me._line = _checkcallable(lineproc, 'line proc')
36 me._eof = _checkcallable(eofproc, 'eof proc')
37 def __dealloc__(me):
38 lbuf_destroy(&me.b)
39 property activep:
40 def __get__(me):
41 return _tobool(me.b.f & LBUF_ENABLE)
42 property delim:
43 def __get__(me):
44 if me.b.delim == _LBUF_CRLF or me.b.delim == _LBUF_STRICTCRLF:
45 return me.b.delim
46 else:
47 return chr(me.b.delim)
48 def __set__(me, d):
49 if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF:
50 me.b.delim = d
51 else:
52 me.b.delim = ord(d)
53 property size:
54 def __get__(me):
55 return me.b.sz
56 def __set__(me, sz):
57 if sz <= 0:
58 raise TypeError, 'size must be positive'
59 lbuf_setsize(&me.b, sz)
60 property lineproc:
61 def __get__(me):
62 return me._line
63 def __set__(me, proc):
64 me._line = _checkcallable(proc, 'line proc')
65 def __del__(me):
66 me._line = None
67 property eofproc:
68 def __get__(me):
69 return me._eof
70 def __set__(me, proc):
71 me._eof = _checkcallable(proc, 'eof proc')
72 def __del__(me):
73 me._eof = None
74 def enable(me):
75 if me.b.f & LBUF_ENABLE:
76 raise ValueError, 'already enabled'
77 me.b.f = me.b.f | LBUF_ENABLE
78 me.enabled()
79 return me
80 def disable(me):
81 if not (me.b.f & LBUF_ENABLE):
82 raise ValueError, 'already disabled'
83 me.b.f = me.b.f & ~LBUF_ENABLE
84 me.disabled()
85 return me
86 def close(me):
87 if not (me.b.f & LBUF_ENABLE):
88 raise ValueError, 'buffer disabled'
89 lbuf_close(&me.b)
90 return me
91 property free:
92 def __get__(me):
93 cdef char *p
94 return lbuf_free(&me.b, &p)
95 def flush(me, str):
96 cdef int len
97 cdef char *p
98 cdef char *q
99 cdef size_t n
100 PyString_AsStringAndSize(str, &p, &len)
101 while len > 0:
102 n = lbuf_free(&me.b, &q)
103 if n > len:
104 n = len
105 memcpy(q, p, n)
106 p = p + n
107 len = len - n
108 if not (me.b.f & LBUF_ENABLE):
109 break
110 lbuf_flush(&me.b, q, n)
111 return PyString_FromStringAndSize(p, len)
112 def enabled(me):
113 pass
114 def disabled(me):
115 pass
116 def line(me, line):
117 return _maybecall(me._line, (line,))
118 def eof(me):
119 return _maybecall(me._eof, ())
120
121cdef void _lbfunc(char *s, size_t n, void *arg):
122 cdef LineBuffer sb
123 sb = <LineBuffer>arg
124 if s is NULL:
125 sb.eof()
126 else:
127 sb.line(PyString_FromStringAndSize(s, n))
128
5b1830f3 129###----- That's all, folks --------------------------------------------------