chiark / gitweb /
setup.py: Update to use pkg-config.
[mLib-python] / lbuf.pyx
CommitLineData
579d0169 1# -*-pyrex-*-
2#
3# $Id$
4#
5# Line buffering
6#
7# (c) 2005 Straylight/Edgeware
8#
9
10#----- Licensing notice -----------------------------------------------------
11#
12# This file is part of the Python interface to mLib.
13#
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.
d8d81d1b 18#
579d0169 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.
d8d81d1b 23#
579d0169 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.
27
28LBUF_CRLF = _LBUF_CRLF
29LBUF_STRICTCRLF = _LBUF_STRICTCRLF
30
31cdef class LineBuffer:
32 cdef lbuf b
33 cdef _line
34 cdef _eof
35 def __new__(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')
39 def __dealloc__(me):
40 lbuf_destroy(&me.b)
41 property activep:
42 def __get__(me):
43 return _tobool(me.b.f & LBUF_ENABLE)
44 property delim:
45 def __get__(me):
46 if me.b.delim == _LBUF_CRLF or me.b.delim == _LBUF_STRICTCRLF:
47 return me.b.delim
48 else:
49 return chr(me.b.delim)
50 def __set__(me, d):
51 if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF:
52 me.b.delim = d
53 else:
54 me.b.delim = ord(d)
55 property size:
56 def __get__(me):
57 return me.b.sz
58 def __set__(me, sz):
59 if sz <= 0:
60 raise TypeError, 'size must be positive'
61 lbuf_setsize(&me.b, sz)
62 property lineproc:
63 def __get__(me):
64 return me._line
65 def __set__(me, proc):
66 me._line = _checkcallable(proc, 'line proc')
67 def __del__(me):
68 me._line = None
69 property eofproc:
70 def __get__(me):
71 return me._eof
72 def __set__(me, proc):
73 me._eof = _checkcallable(proc, 'eof proc')
74 def __del__(me):
75 me._eof = None
76 def enable(me):
77 if me.b.f & LBUF_ENABLE:
78 raise ValueError, 'already enabled'
79 me.b.f = me.b.f | LBUF_ENABLE
80 me.enabled()
81 return me
82 def disable(me):
83 if not (me.b.f & LBUF_ENABLE):
84 raise ValueError, 'already disabled'
85 me.b.f = me.b.f & ~LBUF_ENABLE
86 me.disabled()
87 return me
88 def close(me):
89 if not (me.b.f & LBUF_ENABLE):
90 raise ValueError, 'buffer disabled'
91 lbuf_close(&me.b)
92 return me
93 property free:
94 def __get__(me):
95 cdef char *p
96 return lbuf_free(&me.b, &p)
97 def flush(me, str):
98 cdef int len
99 cdef char *p
100 cdef char *q
101 cdef size_t n
102 PyString_AsStringAndSize(str, &p, &len)
103 while len > 0:
104 n = lbuf_free(&me.b, &q)
105 if n > len:
106 n = len
107 memcpy(q, p, n)
108 p = p + n
109 len = len - n
110 if not (me.b.f & LBUF_ENABLE):
111 break
112 lbuf_flush(&me.b, q, n)
113 return PyString_FromStringAndSize(p, len)
114 def enabled(me):
115 pass
116 def disabled(me):
117 pass
118 def line(me, line):
119 return _maybecall(me._line, (line,))
120 def eof(me):
121 return _maybecall(me._eof, ())
122
123cdef void _lbfunc(char *s, size_t n, void *arg):
124 cdef LineBuffer sb
125 sb = <LineBuffer>arg
126 if s is NULL:
127 sb.eof()
128 else:
129 sb.line(PyString_FromStringAndSize(s, n))
130
131#----- That's all, folks ----------------------------------------------------