20bce5e9 |
1 | # -*-pyrex-*- |
2 | # |
3 | # $Id$ |
4 | # |
5 | # Generic encoder/decoder |
6 | # |
7 | # (c) 2005 Straylight/Edgeware |
8 | # |
9 | |
10 | #----- Licensing notice ----------------------------------------------------- |
11 | # |
12 | # This file is part of the Python interface to mLib. |
13 | # |
14 | # mLib/Python is free software; you can redistribute it and/or modify |
15 | # it under the terms of the GNU General Public License as published by |
16 | # the Free Software Foundation; either version 2 of the License, or |
17 | # (at your option) any later version. |
d8d81d1b |
18 | # |
20bce5e9 |
19 | # mLib/Python is distributed in the hope that it will be useful, |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | # GNU General Public License for more details. |
d8d81d1b |
23 | # |
20bce5e9 |
24 | # You should have received a copy of the GNU General Public License |
25 | # along with mLib/Python; if not, write to the Free Software Foundation, |
26 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | |
28 | #----- External dependencies ------------------------------------------------ |
29 | |
20bce5e9 |
30 | cdef extern from 'mLib/%PREFIX%.h': |
31 | ctypedef struct %PREFIX%_ctx: |
32 | char *indent |
33 | int maxline |
579d0169 |
34 | void _%PREFIX%_init "%PREFIX%_init"(%PREFIX%_ctx *b) |
35 | void _%PREFIX%_encode "%PREFIX%_encode"(%PREFIX%_ctx *b, |
36 | void *p, size_t sz, dstr *d) |
37 | void _%PREFIX%_decode"%PREFIX%_decode"(%PREFIX%_ctx *b, |
38 | void *p, size_t sz, dstr *d) |
20bce5e9 |
39 | |
579d0169 |
40 | cdef class %CLASS%Encode: |
20bce5e9 |
41 | cdef %PREFIX%_ctx ctx |
42 | def __new__(me, *hunoz, **hukairz): |
579d0169 |
43 | _%PREFIX%_init(&me.ctx) |
20bce5e9 |
44 | me.ctx.indent = NULL |
45 | def __init__(me, indent = '\n', maxline = 72): |
46 | if me.ctx.indent: |
47 | xfree(me.ctx.indent) |
48 | me.ctx.indent = xstrdup(indent) |
49 | me.ctx.maxline = maxline |
50 | def __dealloc__(me): |
51 | if me.ctx.indent: |
d8d81d1b |
52 | xfree(me.ctx.indent) |
20bce5e9 |
53 | property indent: |
54 | def __get__(me): |
55 | return me.ctx.indent |
56 | def __set__(me, indent): |
57 | if me.ctx.indent: |
58 | xfree(me.ctx.indent) |
59 | me.ctx.indent = xstrdup(indent) |
60 | property maxline: |
61 | def __get__(me): |
62 | return me.ctx.maxline |
63 | def __set__(me, maxline): |
64 | me.ctx.maxline = maxline |
65 | def encode(me, text): |
66 | cdef void *p |
67 | cdef int len |
68 | cdef dstr d |
69 | DCREATE(&d) |
70 | try: |
71 | PyObject_AsReadBuffer(text, &p, &len) |
579d0169 |
72 | _%PREFIX%_encode(&me.ctx, p, len, &d) |
20bce5e9 |
73 | rc = PyString_FromStringAndSize(d.buf, d.len) |
74 | finally: |
75 | dstr_destroy(&d) |
76 | return rc |
77 | def done(me): |
78 | cdef dstr d |
79 | DCREATE(&d) |
80 | try: |
579d0169 |
81 | _%PREFIX%_encode(&me.ctx, NULL, 0, &d) |
20bce5e9 |
82 | rc = PyString_FromStringAndSize(d.buf, d.len) |
83 | finally: |
84 | dstr_destroy(&d) |
85 | return rc |
86 | |
579d0169 |
87 | def %PREFIX%_encode(text, *arg, **kw): |
88 | e = %CLASS%Encode(*arg, **kw) |
20bce5e9 |
89 | return e.encode(text) + e.done() |
90 | |
579d0169 |
91 | cdef class %CLASS%Decode: |
20bce5e9 |
92 | cdef %PREFIX%_ctx ctx |
93 | def __new__(me, *hunoz, **hukairz): |
579d0169 |
94 | _%PREFIX%_init(&me.ctx) |
20bce5e9 |
95 | me.ctx.indent = NULL |
96 | def decode(me, text): |
97 | cdef void *p |
98 | cdef int len |
99 | cdef dstr d |
100 | DCREATE(&d) |
101 | try: |
102 | PyObject_AsReadBuffer(text, &p, &len) |
579d0169 |
103 | _%PREFIX%_decode(&me.ctx, p, len, &d) |
20bce5e9 |
104 | rc = PyString_FromStringAndSize(d.buf, d.len) |
105 | finally: |
106 | dstr_destroy(&d) |
107 | return rc |
108 | def done(me): |
109 | cdef dstr d |
110 | DCREATE(&d) |
111 | try: |
579d0169 |
112 | _%PREFIX%_decode(&me.ctx, NULL, 0, &d) |
20bce5e9 |
113 | rc = PyString_FromStringAndSize(d.buf, d.len) |
114 | finally: |
115 | dstr_destroy(&d) |
d8d81d1b |
116 | return rc |
20bce5e9 |
117 | |
579d0169 |
118 | def %PREFIX%_decode(text, *arg, **kw): |
119 | d = %CLASS%Decode(*arg, **kw) |
f3946a80 |
120 | return d.decode(text) + d.done() |
20bce5e9 |
121 | |
122 | #----- That's all, folks ---------------------------------------------------- |
123 | |