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 %CLASS%([indent = '\\n'], [maxline = 72])
40 Obsolete %CLASS% encoder.
43 def __cinit__(me, *hunoz, **hukairz):
44 _%PREFIX%_init(&me.ctx)
46 def __init__(me, indent = '\n', maxline = 72):
48 xfree(<void *>me.ctx.indent)
49 me.ctx.indent = xstrdup(indent)
50 me.ctx.maxline = maxline
53 xfree(<void *>me.ctx.indent)
55 """E.indent -> INT: indent level for new lines"""
58 def __set__(me, indent):
60 xfree(<void *>me.ctx.indent)
61 me.ctx.indent = xstrdup(indent)
63 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
66 def __set__(me, maxline):
67 me.ctx.maxline = maxline
69 """E.encode(IN) -> OUT: continue encoding"""
75 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
76 _%PREFIX%_encode(&me.ctx, p, len, &d)
77 rc = PyString_FromStringAndSize(d.buf, d.len)
82 """E.done() -> OUT: finish encoding, returning final output"""
86 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
87 rc = PyString_FromStringAndSize(d.buf, d.len)
92 def %PREFIX%_encode(text, *arg, **kw):
93 """%PREFIX%_encode(IN, [ARGS...]) -> OUT: %CLASS%-encode the string IN"""
94 e = %CLASS%Encode(*arg, **kw)
95 return e.encode(text) + e.done()
97 cdef class %CLASS%Decode:
101 Obsolete %CLASS% decoder.
103 cdef %PREFIX%_ctx ctx
104 def __cinit__(me, *hunoz, **hukairz):
105 _%PREFIX%_init(&me.ctx)
107 def decode(me, text):
108 """D.encode(IN) -> OUT: continue decoding"""
114 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
115 _%PREFIX%_decode(&me.ctx, p, len, &d)
116 rc = PyString_FromStringAndSize(d.buf, d.len)
121 """D.done() -> OUT: finish decoding, returning final output"""
125 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
126 rc = PyString_FromStringAndSize(d.buf, d.len)
131 def %PREFIX%_decode(text, *arg, **kw):
132 """%PREFIX%_decode(IN) -> OUT: %CLASS%-decode the string IN"""
133 d = %CLASS%Decode(*arg, **kw)
134 return d.decode(text) + d.done()
136 ###----- That's all, folks --------------------------------------------------