chiark / gitweb /
*.pyx: Add some rather laconic docstrings.
[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
376ad06d 43 def __cinit__(me, *hunoz, **hukairz):
579d0169 44 _%PREFIX%_init(&me.ctx)
20bce5e9 45 me.ctx.indent = NULL
46 def __init__(me, indent = '\n', maxline = 72):
47 if me.ctx.indent:
f9d8a427 48 xfree(<void *>me.ctx.indent)
20bce5e9 49 me.ctx.indent = xstrdup(indent)
50 me.ctx.maxline = maxline
51 def __dealloc__(me):
52 if me.ctx.indent:
f9d8a427 53 xfree(<void *>me.ctx.indent)
20bce5e9 54 property indent:
addc0c37 55 """E.indent -> INT: indent level for new lines"""
20bce5e9 56 def __get__(me):
57 return me.ctx.indent
58 def __set__(me, indent):
59 if me.ctx.indent:
f9d8a427 60 xfree(<void *>me.ctx.indent)
20bce5e9 61 me.ctx.indent = xstrdup(indent)
62 property maxline:
addc0c37 63 """E.maxline -> INT: maximum length of line, or 0 to prevent splitting"""
20bce5e9 64 def __get__(me):
65 return me.ctx.maxline
66 def __set__(me, maxline):
67 me.ctx.maxline = maxline
68 def encode(me, text):
addc0c37 69 """E.encode(IN) -> OUT: continue encoding"""
20bce5e9 70 cdef void *p
78911cdb 71 cdef Py_ssize_t len
20bce5e9 72 cdef dstr d
73 DCREATE(&d)
74 try:
704500e1 75 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
579d0169 76 _%PREFIX%_encode(&me.ctx, p, len, &d)
20bce5e9 77 rc = PyString_FromStringAndSize(d.buf, d.len)
78 finally:
79 dstr_destroy(&d)
80 return rc
81 def done(me):
addc0c37 82 """E.done() -> OUT: finish encoding, returning final output"""
20bce5e9 83 cdef dstr d
84 DCREATE(&d)
85 try:
579d0169 86 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
20bce5e9 87 rc = PyString_FromStringAndSize(d.buf, d.len)
88 finally:
89 dstr_destroy(&d)
90 return rc
91
579d0169 92def %PREFIX%_encode(text, *arg, **kw):
addc0c37 93 """%PREFIX%_encode(IN, [ARGS...]) -> OUT: %CLASS%-encode the string IN"""
579d0169 94 e = %CLASS%Encode(*arg, **kw)
20bce5e9 95 return e.encode(text) + e.done()
96
579d0169 97cdef class %CLASS%Decode:
addc0c37
MW
98 """
99 %CLASS%()
100
101 Obsolete %CLASS% decoder.
102 """
20bce5e9 103 cdef %PREFIX%_ctx ctx
376ad06d 104 def __cinit__(me, *hunoz, **hukairz):
579d0169 105 _%PREFIX%_init(&me.ctx)
20bce5e9 106 me.ctx.indent = NULL
107 def decode(me, text):
addc0c37 108 """D.encode(IN) -> OUT: continue decoding"""
20bce5e9 109 cdef void *p
78911cdb 110 cdef Py_ssize_t len
20bce5e9 111 cdef dstr d
112 DCREATE(&d)
113 try:
704500e1 114 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
579d0169 115 _%PREFIX%_decode(&me.ctx, p, len, &d)
20bce5e9 116 rc = PyString_FromStringAndSize(d.buf, d.len)
117 finally:
118 dstr_destroy(&d)
119 return rc
120 def done(me):
addc0c37 121 """D.done() -> OUT: finish decoding, returning final output"""
20bce5e9 122 cdef dstr d
123 DCREATE(&d)
124 try:
579d0169 125 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
20bce5e9 126 rc = PyString_FromStringAndSize(d.buf, d.len)
127 finally:
128 dstr_destroy(&d)
d8d81d1b 129 return rc
20bce5e9 130
579d0169 131def %PREFIX%_decode(text, *arg, **kw):
addc0c37 132 """%PREFIX%_decode(IN) -> OUT: %CLASS%-decode the string IN"""
579d0169 133 d = %CLASS%Decode(*arg, **kw)
f3946a80 134 return d.decode(text) + d.done()
20bce5e9 135
5b1830f3 136###----- That's all, folks --------------------------------------------------