5 # Generic encoder/decoder
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 #----- External dependencies ------------------------------------------------
30 cdef extern from 'mLib/%PREFIX%.h':
31 ctypedef struct %PREFIX%_ctx:
34 void _%PREFIX%_init "%PREFIX%_init"(%PREFIX%_ctx *b)
35 void _%PREFIX%_encode "%PREFIX%_encode"(%PREFIX%_ctx *b,
36 void *p, size_t sz, dstr *d)
37 void _%PREFIX%_decode"%PREFIX%_decode"(%PREFIX%_ctx *b,
38 void *p, size_t sz, dstr *d)
40 cdef class %CLASS%Encode:
42 def __new__(me, *hunoz, **hukairz):
43 _%PREFIX%_init(&me.ctx)
45 def __init__(me, indent = '\n', maxline = 72):
48 me.ctx.indent = xstrdup(indent)
49 me.ctx.maxline = maxline
56 def __set__(me, indent):
59 me.ctx.indent = xstrdup(indent)
63 def __set__(me, maxline):
64 me.ctx.maxline = maxline
71 PyObject_AsReadBuffer(text, &p, &len)
72 _%PREFIX%_encode(&me.ctx, p, len, &d)
73 rc = PyString_FromStringAndSize(d.buf, d.len)
81 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
82 rc = PyString_FromStringAndSize(d.buf, d.len)
87 def %PREFIX%_encode(text, *arg, **kw):
88 e = %CLASS%Encode(*arg, **kw)
89 return e.encode(text) + e.done()
91 cdef class %CLASS%Decode:
93 def __new__(me, *hunoz, **hukairz):
94 _%PREFIX%_init(&me.ctx)
102 PyObject_AsReadBuffer(text, &p, &len)
103 _%PREFIX%_decode(&me.ctx, p, len, &d)
104 rc = PyString_FromStringAndSize(d.buf, d.len)
112 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
113 rc = PyString_FromStringAndSize(d.buf, d.len)
118 def %PREFIX%_decode(text, *arg, **kw):
119 d = %CLASS%Decode(*arg, **kw)
120 return d.decode(text) + d.done()
122 #----- That's all, folks ----------------------------------------------------