chiark / gitweb /
Initial import.
[mLib-python] / codec.pyx.in
CommitLineData
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.
18#
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.
23#
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
30cdef extern from 'stddef.h':
31 ctypedef int size_t
32
33cdef extern from 'mLib/dstr.h':
34 ctypedef struct dstr:
35 char *buf
36 int len
37 void DCREATE(dstr *d)
38 void dstr_destroy(dstr *d)
39
40cdef extern from 'mLib/alloc.h':
41 char *xstrdup(char *p)
42 void xfree(void *p)
43
44cdef extern from 'mLib/%PREFIX%.h':
45 ctypedef struct %PREFIX%_ctx:
46 char *indent
47 int maxline
48 void %PREFIX%_init(%PREFIX%_ctx *b)
49 void %PREFIX%_encode(%PREFIX%_ctx *b, void *p, size_t sz, dstr *d)
50 void %PREFIX%_decode(%PREFIX%_ctx *b, void *p, size_t sz, dstr *d)
51
52cdef extern from 'Python.h':
53 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
54 object PyString_FromStringAndSize(char *p, int len)
55
56cdef class Encode:
57 cdef %PREFIX%_ctx ctx
58 def __new__(me, *hunoz, **hukairz):
59 %PREFIX%_init(&me.ctx)
60 me.ctx.indent = NULL
61 def __init__(me, indent = '\n', maxline = 72):
62 if me.ctx.indent:
63 xfree(me.ctx.indent)
64 me.ctx.indent = xstrdup(indent)
65 me.ctx.maxline = maxline
66 def __dealloc__(me):
67 if me.ctx.indent:
68 xfree(me.ctx.indent)
69 property indent:
70 def __get__(me):
71 return me.ctx.indent
72 def __set__(me, indent):
73 if me.ctx.indent:
74 xfree(me.ctx.indent)
75 me.ctx.indent = xstrdup(indent)
76 property maxline:
77 def __get__(me):
78 return me.ctx.maxline
79 def __set__(me, maxline):
80 me.ctx.maxline = maxline
81 def encode(me, text):
82 cdef void *p
83 cdef int len
84 cdef dstr d
85 DCREATE(&d)
86 try:
87 PyObject_AsReadBuffer(text, &p, &len)
88 %PREFIX%_encode(&me.ctx, p, len, &d)
89 rc = PyString_FromStringAndSize(d.buf, d.len)
90 finally:
91 dstr_destroy(&d)
92 return rc
93 def done(me):
94 cdef dstr d
95 DCREATE(&d)
96 try:
97 %PREFIX%_encode(&me.ctx, NULL, 0, &d)
98 rc = PyString_FromStringAndSize(d.buf, d.len)
99 finally:
100 dstr_destroy(&d)
101 return rc
102
103def encode(text, *arg, **kw):
104 e = Encode(*arg, **kw)
105 return e.encode(text) + e.done()
106
107cdef class Decode:
108 cdef %PREFIX%_ctx ctx
109 def __new__(me, *hunoz, **hukairz):
110 %PREFIX%_init(&me.ctx)
111 me.ctx.indent = NULL
112 def decode(me, text):
113 cdef void *p
114 cdef int len
115 cdef dstr d
116 DCREATE(&d)
117 try:
118 PyObject_AsReadBuffer(text, &p, &len)
119 %PREFIX%_decode(&me.ctx, p, len, &d)
120 rc = PyString_FromStringAndSize(d.buf, d.len)
121 finally:
122 dstr_destroy(&d)
123 return rc
124 def done(me):
125 cdef dstr d
126 DCREATE(&d)
127 try:
128 %PREFIX%_decode(&me.ctx, NULL, 0, &d)
129 rc = PyString_FromStringAndSize(d.buf, d.len)
130 finally:
131 dstr_destroy(&d)
132 return rc
133
134def decode(text, *arg, **kw):
135 d = Decode(*arg, **kw)
136 return e.decode(text) + d.done()
137
138#----- That's all, folks ----------------------------------------------------
139