File: stringfilter.c

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (89 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 *  Copyright (C) 1998, 1999 by Bernhard Herzog.
 *
 *			All Rights Reserved
 *
 *  Permission to use, copy, modify, and distribute this software and
 *  its documentation for any purpose and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear in
 *  supporting documentation, and that the name of the author not be
 *  used in advertising or publicity pertaining to distribution of the
 *  software without specific, written prior permission.
 *
 *  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 *  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 *  OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 *  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 *  WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "stringfilter.h"

#define FBUFLEN 1024

typedef struct {
    PyObject * string;
    char * pos;
    size_t left;
} StringDecodeState;


static size_t
read_string(void* clientdata, PyObject * source, char * buf, size_t length)
{
    StringDecodeState * state = (StringDecodeState*)clientdata;
    size_t copy;
    
    if (state->left > 0)
    {
	if (state->left > length)
	    copy = length;
	else
	    copy = state->left;
	memcpy(buf, state->pos, copy);
	state->left -= copy;
	state->pos += copy;
    }
    else if (source != Py_None)
    {
	copy = Filter_Read(source, buf, length);
    }
    else
    {
	copy = 0;
    }
    return copy;
}


static void
string_state_dealloc(void * clientdata)
{
    Py_DECREF(((StringDecodeState*)clientdata)->string);
    free(clientdata);
}

PyObject *
Filter_StringDecode(PyObject * self, PyObject * args)
{
    PyObject * source;
    PyObject * string;
    StringDecodeState * state;

    if (!PyArg_ParseTuple(args, "SO", &string, &source))
	return NULL;

    state = malloc(sizeof (StringDecodeState));
    if (!state)
	return PyErr_NoMemory();
    state->string = string;
    Py_INCREF(state->string);
    state->pos = PyString_AsString(string);
    state->left = PyString_Size(string);

    return Filter_NewDecoder(source, "StringDecode", 0, read_string, NULL,
			     string_state_dealloc, state);
}