chiark / gitweb /
array.c: Add fake initializations in `pop' and `shift' to muffle warnings.
[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:
20bce5e9 37 cdef %PREFIX%_ctx ctx
376ad06d 38 def __cinit__(me, *hunoz, **hukairz):
579d0169 39 _%PREFIX%_init(&me.ctx)
20bce5e9 40 me.ctx.indent = NULL
41 def __init__(me, indent = '\n', maxline = 72):
42 if me.ctx.indent:
43 xfree(me.ctx.indent)
44 me.ctx.indent = xstrdup(indent)
45 me.ctx.maxline = maxline
46 def __dealloc__(me):
47 if me.ctx.indent:
d8d81d1b 48 xfree(me.ctx.indent)
20bce5e9 49 property indent:
50 def __get__(me):
51 return me.ctx.indent
52 def __set__(me, indent):
53 if me.ctx.indent:
54 xfree(me.ctx.indent)
55 me.ctx.indent = xstrdup(indent)
56 property maxline:
57 def __get__(me):
58 return me.ctx.maxline
59 def __set__(me, maxline):
60 me.ctx.maxline = maxline
61 def encode(me, text):
62 cdef void *p
63 cdef int len
64 cdef dstr d
65 DCREATE(&d)
66 try:
67 PyObject_AsReadBuffer(text, &p, &len)
579d0169 68 _%PREFIX%_encode(&me.ctx, p, len, &d)
20bce5e9 69 rc = PyString_FromStringAndSize(d.buf, d.len)
70 finally:
71 dstr_destroy(&d)
72 return rc
73 def done(me):
74 cdef dstr d
75 DCREATE(&d)
76 try:
579d0169 77 _%PREFIX%_encode(&me.ctx, NULL, 0, &d)
20bce5e9 78 rc = PyString_FromStringAndSize(d.buf, d.len)
79 finally:
80 dstr_destroy(&d)
81 return rc
82
579d0169 83def %PREFIX%_encode(text, *arg, **kw):
84 e = %CLASS%Encode(*arg, **kw)
20bce5e9 85 return e.encode(text) + e.done()
86
579d0169 87cdef class %CLASS%Decode:
20bce5e9 88 cdef %PREFIX%_ctx ctx
376ad06d 89 def __cinit__(me, *hunoz, **hukairz):
579d0169 90 _%PREFIX%_init(&me.ctx)
20bce5e9 91 me.ctx.indent = NULL
92 def decode(me, text):
93 cdef void *p
94 cdef int len
95 cdef dstr d
96 DCREATE(&d)
97 try:
98 PyObject_AsReadBuffer(text, &p, &len)
579d0169 99 _%PREFIX%_decode(&me.ctx, p, len, &d)
20bce5e9 100 rc = PyString_FromStringAndSize(d.buf, d.len)
101 finally:
102 dstr_destroy(&d)
103 return rc
104 def done(me):
105 cdef dstr d
106 DCREATE(&d)
107 try:
579d0169 108 _%PREFIX%_decode(&me.ctx, NULL, 0, &d)
20bce5e9 109 rc = PyString_FromStringAndSize(d.buf, d.len)
110 finally:
111 dstr_destroy(&d)
d8d81d1b 112 return rc
20bce5e9 113
579d0169 114def %PREFIX%_decode(text, *arg, **kw):
115 d = %CLASS%Decode(*arg, **kw)
f3946a80 116 return d.decode(text) + d.done()
20bce5e9 117
5b1830f3 118###----- That's all, folks --------------------------------------------------
20bce5e9 119