@@@ cython and python 3 wip
[mLib-python] / codec.pyx.in
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Generic encoder/decoder
4###
5### (c) 2005 Straylight/Edgeware
6###
20bce5e9 7
5b1830f3
MW
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.
20bce5e9 25
20bce5e9 26cdef extern from 'mLib/%PREFIX%.h':
27 ctypedef struct %PREFIX%_ctx:
28 char *indent
29 int maxline
579d0169 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)
20bce5e9 35
579d0169 36cdef class %CLASS%Encode:
addc0c37
MW
37 """
38 %CLASS%([indent = '\\n'], [maxline = 72])
39
40 Obsolete %CLASS% encoder.
41 """
20bce5e9 42 cdef %PREFIX%_ctx ctx
965caf5f 43 def __cinit__(me):
579d0169 44 _%PREFIX%_init(&me.ctx)
20bce5e9 45 me.ctx.indent = NULL
965caf5f
MW
46 def __dealloc__(me):
47 if me.ctx.indent:
48 xfree(<void *>me.ctx.indent)
20bce5e9 49 def __init__(me, indent = '\n', maxline = 72):
50 if me.ctx.indent:
f9d8a427 51 xfree(<void *>me.ctx.indent)
965caf5f 52 me.ctx.indent = xstrdup(TEXT_PTR(indent))
20bce5e9 53 me.ctx.maxline = maxline
965caf5f
MW
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):
20bce5e9 60 if me.ctx.indent:
f9d8a427 61 xfree(<void *>me.ctx.indent)
965caf5f
MW
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):
addc0c37 68 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
965caf5f
MW
69 return me.ctx.maxline
70 @maxline.setter
71 def maxline(me, maxline):
20bce5e9 72 me.ctx.maxline = maxline
965caf5f 73 def encode(me, input):
addc0c37 74 """E.encode(IN) -> OUT: continue encoding"""
965caf5f
MW
75 cdef const void *p
76 cdef Py_ssize_t sz
20bce5e9 77 cdef dstr d
78 DCREATE(&d)
79 try:
965caf5f
MW
80 PyObject_AsReadBuffer(input, &p, &sz)
81 _%PREFIX%_encode(&me.ctx, p, sz, &d)
82 return TEXT_FROMSTRLEN(d.buf, d.len)
20bce5e9 83 finally:
84 dstr_destroy(&d)
20bce5e9 85 def done(me):
addc0c37 86 """E.done() -> OUT: finish encoding, returning final output"""
20bce5e9 87 cdef dstr d
88 DCREATE(&d)
89 try:
579d0169 90 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
965caf5f 91 return TEXT_FROMSTRLEN(d.buf, d.len)
20bce5e9 92 finally:
93 dstr_destroy(&d)
20bce5e9 94
965caf5f 95def %PREFIX%_encode(input, *arg, **kw):
addc0c37 96 """%PREFIX%_encode(IN, [ARGS...]) -> OUT: %CLASS%-encode the string IN"""
579d0169 97 e = %CLASS%Encode(*arg, **kw)
965caf5f 98 return e.encode(input) + e.done()
20bce5e9 99
579d0169 100cdef class %CLASS%Decode:
addc0c37
MW
101 """
102 %CLASS%()
103
104 Obsolete %CLASS% decoder.
105 """
20bce5e9 106 cdef %PREFIX%_ctx ctx
965caf5f 107 def __cinit__(me):
579d0169 108 _%PREFIX%_init(&me.ctx)
20bce5e9 109 me.ctx.indent = NULL
965caf5f 110 def decode(me, input):
addc0c37 111 """D.encode(IN) -> OUT: continue decoding"""
965caf5f
MW
112 cdef const char *p
113 cdef Py_ssize_t sz
20bce5e9 114 cdef dstr d
115 DCREATE(&d)
116 try:
965caf5f
MW
117 TEXT_PTRLEN(input, &p, &sz)
118 _%PREFIX%_decode(&me.ctx, p, sz, &d)
119 return BIN_FROMSTRLEN(d.buf, d.len)
20bce5e9 120 finally:
121 dstr_destroy(&d)
20bce5e9 122 def done(me):
addc0c37 123 """D.done() -> OUT: finish decoding, returning final output"""
20bce5e9 124 cdef dstr d
125 DCREATE(&d)
126 try:
579d0169 127 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
965caf5f 128 return BIN_FROMSTRLEN(d.buf, d.len)
20bce5e9 129 finally:
130 dstr_destroy(&d)
20bce5e9 131
965caf5f 132def %PREFIX%_decode(input, *arg, **kw):
addc0c37 133 """%PREFIX%_decode(IN) -> OUT: %CLASS%-decode the string IN"""
579d0169 134 d = %CLASS%Decode(*arg, **kw)
965caf5f 135 return d.decode(input) + d.done()
20bce5e9 136
5b1830f3 137###----- That's all, folks --------------------------------------------------