3 ### Generic encoder/decoder
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
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.
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.
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.
26 cdef extern from 'mLib/%PREFIX%.h':
27 ctypedef struct %PREFIX%_ctx:
30 void _%PREFIX%_init "%PREFIX%_init"(%PREFIX%_ctx *b)
31 void _%PREFIX%_encode "%PREFIX%_encode"(%PREFIX%_ctx *b,
32 void *p, size_t sz, dstr *d)
33 void _%PREFIX%_decode"%PREFIX%_decode"(%PREFIX%_ctx *b,
34 void *p, size_t sz, dstr *d)
36 cdef class %CLASS%Encode:
38 def __cinit__(me, *hunoz, **hukairz):
39 _%PREFIX%_init(&me.ctx)
41 def __init__(me, indent = '\n', maxline = 72):
44 me.ctx.indent = xstrdup(indent)
45 me.ctx.maxline = maxline
52 def __set__(me, indent):
55 me.ctx.indent = xstrdup(indent)
59 def __set__(me, maxline):
60 me.ctx.maxline = maxline
67 PyObject_AsReadBuffer(text, &p, &len)
68 _%PREFIX%_encode(&me.ctx, p, len, &d)
69 rc = PyString_FromStringAndSize(d.buf, d.len)
77 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
78 rc = PyString_FromStringAndSize(d.buf, d.len)
83 def %PREFIX%_encode(text, *arg, **kw):
84 e = %CLASS%Encode(*arg, **kw)
85 return e.encode(text) + e.done()
87 cdef class %CLASS%Decode:
89 def __cinit__(me, *hunoz, **hukairz):
90 _%PREFIX%_init(&me.ctx)
98 PyObject_AsReadBuffer(text, &p, &len)
99 _%PREFIX%_decode(&me.ctx, p, len, &d)
100 rc = PyString_FromStringAndSize(d.buf, d.len)
108 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
109 rc = PyString_FromStringAndSize(d.buf, d.len)
114 def %PREFIX%_decode(text, *arg, **kw):
115 d = %CLASS%Decode(*arg, **kw)
116 return d.decode(text) + d.done()
118 ###----- That's all, folks --------------------------------------------------