| 1 | ### -*-pyrex-*- |
| 2 | ### |
| 3 | ### Generic encoder/decoder |
| 4 | ### |
| 5 | ### (c) 2005 Straylight/Edgeware |
| 6 | ### |
| 7 | |
| 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. |
| 25 | |
| 26 | cdef extern from 'mLib/%PREFIX%.h': |
| 27 | ctypedef struct %PREFIX%_ctx: |
| 28 | char *indent |
| 29 | int maxline |
| 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) |
| 35 | |
| 36 | cdef class %CLASS%Encode: |
| 37 | cdef %PREFIX%_ctx ctx |
| 38 | def __cinit__(me, *hunoz, **hukairz): |
| 39 | _%PREFIX%_init(&me.ctx) |
| 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: |
| 48 | xfree(me.ctx.indent) |
| 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 Py_ssize_t len |
| 64 | cdef dstr d |
| 65 | DCREATE(&d) |
| 66 | try: |
| 67 | PyObject_AsReadBuffer(text, <cvp *>&p, &len) |
| 68 | _%PREFIX%_encode(&me.ctx, p, len, &d) |
| 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: |
| 77 | _%PREFIX%_encode(&me.ctx, NULL, 0, &d) |
| 78 | rc = PyString_FromStringAndSize(d.buf, d.len) |
| 79 | finally: |
| 80 | dstr_destroy(&d) |
| 81 | return rc |
| 82 | |
| 83 | def %PREFIX%_encode(text, *arg, **kw): |
| 84 | e = %CLASS%Encode(*arg, **kw) |
| 85 | return e.encode(text) + e.done() |
| 86 | |
| 87 | cdef class %CLASS%Decode: |
| 88 | cdef %PREFIX%_ctx ctx |
| 89 | def __cinit__(me, *hunoz, **hukairz): |
| 90 | _%PREFIX%_init(&me.ctx) |
| 91 | me.ctx.indent = NULL |
| 92 | def decode(me, text): |
| 93 | cdef void *p |
| 94 | cdef Py_ssize_t len |
| 95 | cdef dstr d |
| 96 | DCREATE(&d) |
| 97 | try: |
| 98 | PyObject_AsReadBuffer(text, <cvp *>&p, &len) |
| 99 | _%PREFIX%_decode(&me.ctx, p, len, &d) |
| 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: |
| 108 | _%PREFIX%_decode(&me.ctx, NULL, 0, &d) |
| 109 | rc = PyString_FromStringAndSize(d.buf, d.len) |
| 110 | finally: |
| 111 | dstr_destroy(&d) |
| 112 | return rc |
| 113 | |
| 114 | def %PREFIX%_decode(text, *arg, **kw): |
| 115 | d = %CLASS%Decode(*arg, **kw) |
| 116 | return d.decode(text) + d.done() |
| 117 | |
| 118 | ###----- That's all, folks -------------------------------------------------- |
| 119 | |