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.
44 _%PREFIX%_init(&me.ctx)
48 xfree(<void *>me.ctx.indent)
49 def __init__(me, indent = '\n', maxline = 72):
51 xfree(<void *>me.ctx.indent)
52 me.ctx.indent = xstrdup(TEXT_PTR(indent))
53 me.ctx.maxline = maxline
56 """E.indent -> INT: indent level for new lines"""
59 def indent(me, indent):
61 xfree(<void *>me.ctx.indent)
65 me.ctx.indent = xstrdup(TEXT_PTR(indent))
68 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
71 def maxline(me, maxline):
72 me.ctx.maxline = maxline
73 def encode(me, input):
74 """E.encode(IN) -> OUT: continue encoding"""
80 PyObject_AsReadBuffer(input, &p, &sz)
81 _%PREFIX%_encode(&me.ctx, p, sz, &d)
82 return TEXT_FROMSTRLEN(d.buf, d.len)
86 """E.done() -> OUT: finish encoding, returning final output"""
90 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
91 return TEXT_FROMSTRLEN(d.buf, d.len)
95 def %PREFIX%_encode(input, *arg, **kw):
96 """%PREFIX%_encode(IN, [ARGS...]) -> OUT: %CLASS%-encode the string IN"""
97 e = %CLASS%Encode(*arg, **kw)
98 return e.encode(input) + e.done()
100 cdef class %CLASS%Decode:
104 Obsolete %CLASS% decoder.
106 cdef %PREFIX%_ctx ctx
108 _%PREFIX%_init(&me.ctx)
110 def decode(me, input):
111 """D.encode(IN) -> OUT: continue decoding"""
117 TEXT_PTRLEN(input, &p, &sz)
118 _%PREFIX%_decode(&me.ctx, p, sz, &d)
119 return BIN_FROMSTRLEN(d.buf, d.len)
123 """D.done() -> OUT: finish decoding, returning final output"""
127 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
128 return BIN_FROMSTRLEN(d.buf, d.len)
132 def %PREFIX%_decode(input, *arg, **kw):
133 """%PREFIX%_decode(IN) -> OUT: %CLASS%-decode the string IN"""
134 d = %CLASS%Decode(*arg, **kw)
135 return d.decode(input) + d.done()
137 ###----- That's all, folks --------------------------------------------------