chiark / gitweb /
fdutils.pyx, str.pyx: Fix some stupid bugs.
[mLib-python] / pkbuf.pyx
CommitLineData
579d0169 1# -*-pyrex-*-
2#
3# $Id$
4#
5# Packet 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
28cdef class PacketBuffer:
29 cdef pkbuf pk
30 cdef _packet
31 cdef _eof
32 def __new__(me, packetproc = None, eofproc = None, *hunoz, **hukairz):
33 pkbuf_init(&me.pk, _pkfunc, <void *>me)
34 me._packet = _checkcallable(packetproc, 'packet proc')
35 me._eof = _checkcallable(eofproc, 'eof proc')
36 def __dealloc__(me):
37 pkbuf_destroy(&me.pk)
38 property activep:
39 def __get__(me):
40 return _tobool(me.pk.f & PKBUF_ENABLE)
41 property want:
42 def __get__(me):
43 return me.pk.want
44 def __set__(me, want):
45 if want <= 0:
46 raise TypeError, 'want must be positive'
47 pkbuf_want(&me.pk, pk)
48 property packetproc:
49 def __get__(me):
50 return me._packet
51 def __set__(me, proc):
52 me._line = _checkcallable(proc, 'packet proc')
53 def __del__(me):
54 me._line = None
55 property eofproc:
56 def __get__(me):
57 return me._eof
58 def __set__(me, proc):
59 me._eof = _checkcallable(proc, 'eof proc')
60 def __del__(me):
61 me._eof = None
62 def enable(me):
63 if me.pk.f & PKBUF_ENABLE:
64 raise ValueError, 'already enabled'
65 me.pk.f = me.pk.f | PKBUF_ENABLE
66 me.enabled()
67 return me
68 def disable(me):
69 if not (me.pk.f & PKBUF_ENABLE):
70 raise ValueError, 'already disabled'
71 me.pk.f = me.pk.f & ~PKBUF_ENABLE
72 me.disabled()
73 return me
74 def close(me):
75 if not (me.pk.f & PKBUF_ENABLE):
76 raise ValueError, 'buffer disabled'
77 pkbuf_close(&me.pk)
78 return me
79 property free:
80 def __get__(me):
81 cdef unsigned char *p
82 return pkbuf_free(&me.pk, &p)
83 def flush(me, str):
84 cdef int len
85 cdef unsigned char *p
86 cdef unsigned char *q
87 cdef size_t n
88 PyObject_AsReadBuffer(str, <void **>&p, &len)
89 while len > 0:
90 n = pkbuf_free(&me.pk, &q)
91 if n > len:
92 n = len
93 memcpy(q, p, n)
94 p = p + n
95 len = len - n
96 if not (me.pk.f & PKBUF_ENABLE):
97 break
98 pkbuf_flush(&me.pk, q, n)
99 return PyString_FromStringAndSize(<char *>p, len)
100 def enabled(me):
101 pass
102 def disabled(me):
103 pass
104 def packet(me, pk):
105 return _maybecall(me._packet, (pk,))
106 def eof(me):
107 return _maybecall(me._eof, ())
108
109cdef void _pkfunc(unsigned char *p, size_t n, pkbuf *pk,
110 size_t *keep, void *arg):
111 cdef PacketBuffer pb
112 cdef void *rp
113 cdef int rn
114 pb = <PacketBuffer>arg
115 if p is NULL:
116 pb.eof()
117 else:
118 r = pb.packet(PyString_FromStringAndSize(<char *>p, n))
119 if r is not None:
120 PyObject_AsReadBuffer(r, &rp, &rn)
121 if rn > n:
122 raise ValueError, 'remaining buffer too large'
123 if rn:
124 memcpy(p + n - rn, rp, rn)
125 keep[0] = rn
126
127#----- That's all, folks ----------------------------------------------------