@@@ cython and python 3 wip
[mLib-python] / codec.pyx.in
1 ### -*-pyrex-*-
2 ###
3 ### Generic encoder/decoder
4 ###
5 ### (c) 2005 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
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.
16 ###
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.
21 ###
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.
25
26 cdef extern from 'mLib/%PREFIX%.h':
27 ctypedef struct %PREFIX%_ctx:
28 char *indent
29 int maxline
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)
35
36 cdef class %CLASS%Encode:
37 """
38 %CLASS%([indent = '\\n'], [maxline = 72])
39
40 Obsolete %CLASS% encoder.
41 """
42 cdef %PREFIX%_ctx ctx
43 def __cinit__(me):
44 _%PREFIX%_init(&me.ctx)
45 me.ctx.indent = NULL
46 def __dealloc__(me):
47 if me.ctx.indent:
48 xfree(<void *>me.ctx.indent)
49 def __init__(me, indent = '\n', maxline = 72):
50 if me.ctx.indent:
51 xfree(<void *>me.ctx.indent)
52 me.ctx.indent = xstrdup(TEXT_PTR(indent))
53 me.ctx.maxline = maxline
54 @property
55 def indent(me):
56 """E.indent -> INT: indent level for new lines"""
57 return me.ctx.indent
58 @indent.setter
59 def indent(me, indent):
60 if me.ctx.indent:
61 xfree(<void *>me.ctx.indent)
62 if indent is None:
63 me.ctx.indent = NULL
64 else:
65 me.ctx.indent = xstrdup(TEXT_PTR(indent))
66 @property
67 def maxline(me):
68 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
69 return me.ctx.maxline
70 @maxline.setter
71 def maxline(me, maxline):
72 me.ctx.maxline = maxline
73 def encode(me, input):
74 """E.encode(IN) -> OUT: continue encoding"""
75 cdef const void *p
76 cdef Py_ssize_t sz
77 cdef dstr d
78 DCREATE(&d)
79 try:
80 PyObject_AsReadBuffer(input, &p, &sz)
81 _%PREFIX%_encode(&me.ctx, p, sz, &d)
82 return TEXT_FROMSTRLEN(d.buf, d.len)
83 finally:
84 dstr_destroy(&d)
85 def done(me):
86 """E.done() -> OUT: finish encoding, returning final output"""
87 cdef dstr d
88 DCREATE(&d)
89 try:
90 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
91 return TEXT_FROMSTRLEN(d.buf, d.len)
92 finally:
93 dstr_destroy(&d)
94
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()
99
100 cdef class %CLASS%Decode:
101 """
102 %CLASS%()
103
104 Obsolete %CLASS% decoder.
105 """
106 cdef %PREFIX%_ctx ctx
107 def __cinit__(me):
108 _%PREFIX%_init(&me.ctx)
109 me.ctx.indent = NULL
110 def decode(me, input):
111 """D.encode(IN) -> OUT: continue decoding"""
112 cdef const char *p
113 cdef Py_ssize_t sz
114 cdef dstr d
115 DCREATE(&d)
116 try:
117 TEXT_PTRLEN(input, &p, &sz)
118 _%PREFIX%_decode(&me.ctx, p, sz, &d)
119 return BIN_FROMSTRLEN(d.buf, d.len)
120 finally:
121 dstr_destroy(&d)
122 def done(me):
123 """D.done() -> OUT: finish decoding, returning final output"""
124 cdef dstr d
125 DCREATE(&d)
126 try:
127 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
128 return BIN_FROMSTRLEN(d.buf, d.len)
129 finally:
130 dstr_destroy(&d)
131
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()
136
137 ###----- That's all, folks --------------------------------------------------