chiark / gitweb /
debian/control: Add Build-Depends for `dh-python'.
[mLib-python] / codec.pyx.in
... / ...
CommitLineData
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
26cdef 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
36cdef 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, *hunoz, **hukairz):
44 _%PREFIX%_init(&me.ctx)
45 me.ctx.indent = NULL
46 def __init__(me, indent = '\n', maxline = 72):
47 if me.ctx.indent:
48 xfree(<void *>me.ctx.indent)
49 me.ctx.indent = xstrdup(indent)
50 me.ctx.maxline = maxline
51 def __dealloc__(me):
52 if me.ctx.indent:
53 xfree(<void *>me.ctx.indent)
54 property indent:
55 """E.indent -> INT: indent level for new lines"""
56 def __get__(me):
57 return me.ctx.indent
58 def __set__(me, indent):
59 if me.ctx.indent:
60 xfree(<void *>me.ctx.indent)
61 me.ctx.indent = xstrdup(indent)
62 property maxline:
63 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
64 def __get__(me):
65 return me.ctx.maxline
66 def __set__(me, maxline):
67 me.ctx.maxline = maxline
68 def encode(me, text):
69 """E.encode(IN) -> OUT: continue encoding"""
70 cdef void *p
71 cdef Py_ssize_t len
72 cdef dstr d
73 DCREATE(&d)
74 try:
75 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
76 _%PREFIX%_encode(&me.ctx, p, len, &d)
77 rc = PyString_FromStringAndSize(d.buf, d.len)
78 finally:
79 dstr_destroy(&d)
80 return rc
81 def done(me):
82 """E.done() -> OUT: finish encoding, returning final output"""
83 cdef dstr d
84 DCREATE(&d)
85 try:
86 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
87 rc = PyString_FromStringAndSize(d.buf, d.len)
88 finally:
89 dstr_destroy(&d)
90 return rc
91
92def %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()
96
97cdef class %CLASS%Decode:
98 """
99 %CLASS%()
100
101 Obsolete %CLASS% decoder.
102 """
103 cdef %PREFIX%_ctx ctx
104 def __cinit__(me, *hunoz, **hukairz):
105 _%PREFIX%_init(&me.ctx)
106 me.ctx.indent = NULL
107 def decode(me, text):
108 """D.encode(IN) -> OUT: continue decoding"""
109 cdef void *p
110 cdef Py_ssize_t len
111 cdef dstr d
112 DCREATE(&d)
113 try:
114 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
115 _%PREFIX%_decode(&me.ctx, p, len, &d)
116 rc = PyString_FromStringAndSize(d.buf, d.len)
117 finally:
118 dstr_destroy(&d)
119 return rc
120 def done(me):
121 """D.done() -> OUT: finish decoding, returning final output"""
122 cdef dstr d
123 DCREATE(&d)
124 try:
125 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
126 rc = PyString_FromStringAndSize(d.buf, d.len)
127 finally:
128 dstr_destroy(&d)
129 return rc
130
131def %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()
135
136###----- That's all, folks --------------------------------------------------