codec: Use the correct variable name in the decoder convenience function!
[mLib-python] / codec.pyx.in
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # Generic encoder/decoder
6 #
7 # (c) 2005 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Python interface to mLib.
13 #
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.
18 #
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.
23 #
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.
27
28 #----- External dependencies ------------------------------------------------
29
30 cdef extern from 'mLib/%PREFIX%.h':
31 ctypedef struct %PREFIX%_ctx:
32 char *indent
33 int maxline
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)
39
40 cdef class %CLASS%Encode:
41 cdef %PREFIX%_ctx ctx
42 def __new__(me, *hunoz, **hukairz):
43 _%PREFIX%_init(&me.ctx)
44 me.ctx.indent = NULL
45 def __init__(me, indent = '\n', maxline = 72):
46 if me.ctx.indent:
47 xfree(me.ctx.indent)
48 me.ctx.indent = xstrdup(indent)
49 me.ctx.maxline = maxline
50 def __dealloc__(me):
51 if me.ctx.indent:
52 xfree(me.ctx.indent)
53 property indent:
54 def __get__(me):
55 return me.ctx.indent
56 def __set__(me, indent):
57 if me.ctx.indent:
58 xfree(me.ctx.indent)
59 me.ctx.indent = xstrdup(indent)
60 property maxline:
61 def __get__(me):
62 return me.ctx.maxline
63 def __set__(me, maxline):
64 me.ctx.maxline = maxline
65 def encode(me, text):
66 cdef void *p
67 cdef int len
68 cdef dstr d
69 DCREATE(&d)
70 try:
71 PyObject_AsReadBuffer(text, &p, &len)
72 _%PREFIX%_encode(&me.ctx, p, len, &d)
73 rc = PyString_FromStringAndSize(d.buf, d.len)
74 finally:
75 dstr_destroy(&d)
76 return rc
77 def done(me):
78 cdef dstr d
79 DCREATE(&d)
80 try:
81 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
82 rc = PyString_FromStringAndSize(d.buf, d.len)
83 finally:
84 dstr_destroy(&d)
85 return rc
86
87 def %PREFIX%_encode(text, *arg, **kw):
88 e = %CLASS%Encode(*arg, **kw)
89 return e.encode(text) + e.done()
90
91 cdef class %CLASS%Decode:
92 cdef %PREFIX%_ctx ctx
93 def __new__(me, *hunoz, **hukairz):
94 _%PREFIX%_init(&me.ctx)
95 me.ctx.indent = NULL
96 def decode(me, text):
97 cdef void *p
98 cdef int len
99 cdef dstr d
100 DCREATE(&d)
101 try:
102 PyObject_AsReadBuffer(text, &p, &len)
103 _%PREFIX%_decode(&me.ctx, p, len, &d)
104 rc = PyString_FromStringAndSize(d.buf, d.len)
105 finally:
106 dstr_destroy(&d)
107 return rc
108 def done(me):
109 cdef dstr d
110 DCREATE(&d)
111 try:
112 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
113 rc = PyString_FromStringAndSize(d.buf, d.len)
114 finally:
115 dstr_destroy(&d)
116 return rc
117
118 def %PREFIX%_decode(text, *arg, **kw):
119 d = %CLASS%Decode(*arg, **kw)
120 return d.decode(text) + d.done()
121
122 #----- That's all, folks ----------------------------------------------------
123