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 'stddef.h':
33 cdef extern from 'mLib/dstr.h':
38 void dstr_destroy(dstr *d)
40 cdef extern from 'mLib/alloc.h':
41 char *xstrdup(char *p)
44 cdef extern from 'mLib/%PREFIX%.h':
45 ctypedef struct %PREFIX%_ctx:
48 void %PREFIX%_init(%PREFIX%_ctx *b)
49 void %PREFIX%_encode(%PREFIX%_ctx *b, void *p, size_t sz, dstr *d)
50 void %PREFIX%_decode(%PREFIX%_ctx *b, void *p, size_t sz, dstr *d)
52 cdef extern from 'Python.h':
53 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
54 object PyString_FromStringAndSize(char *p, int len)
58 def __new__(me, *hunoz, **hukairz):
59 %PREFIX%_init(&me.ctx)
61 def __init__(me, indent = '\n', maxline = 72):
64 me.ctx.indent = xstrdup(indent)
65 me.ctx.maxline = maxline
72 def __set__(me, indent):
75 me.ctx.indent = xstrdup(indent)
79 def __set__(me, maxline):
80 me.ctx.maxline = maxline
87 PyObject_AsReadBuffer(text, &p, &len)
88 %PREFIX%_encode(&me.ctx, p, len, &d)
89 rc = PyString_FromStringAndSize(d.buf, d.len)
97 %PREFIX%_encode(&me.ctx, NULL, 0, &d)
98 rc = PyString_FromStringAndSize(d.buf, d.len)
103 def encode(text, *arg, **kw):
104 e = Encode(*arg, **kw)
105 return e.encode(text) + e.done()
108 cdef %PREFIX%_ctx ctx
109 def __new__(me, *hunoz, **hukairz):
110 %PREFIX%_init(&me.ctx)
112 def decode(me, text):
118 PyObject_AsReadBuffer(text, &p, &len)
119 %PREFIX%_decode(&me.ctx, p, len, &d)
120 rc = PyString_FromStringAndSize(d.buf, d.len)
128 %PREFIX%_decode(&me.ctx, NULL, 0, &d)
129 rc = PyString_FromStringAndSize(d.buf, d.len)
134 def decode(text, *arg, **kw):
135 d = Decode(*arg, **kw)
136 return e.decode(text) + d.done()
138 #----- That's all, folks ----------------------------------------------------