Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / ogg-python / src / pyoggpackbuff.c
blobaebfad401233a2e8b7b39fadd3b26e0275286038
1 #include "pyoggpackbuff.h"
2 #include "general.h"
3 #include "_oggmodule.h"
5 /************************************************************
6 OggPackBuffer Object
7 ************************************************************/
9 char py_oggpack_buffer_doc[] = "";
11 static void py_oggpack_buffer_dealloc(PyObject *);
12 static PyObject* py_oggpack_buffer_getattr(PyObject *, char *);
13 static PyObject *py_oggpack_repr(PyObject *self);
15 FDEF(oggpack_reset) "";
16 FDEF(oggpack_writeclear) "";
18 FDEF(oggpack_look) "";
19 FDEF(oggpack_look1) "";
21 FDEF(oggpack_bytes) "";
22 FDEF(oggpack_bits) "";
24 FDEF(oggpack_read) "Return the value of n bits";
25 FDEF(oggpack_read1) "";
27 FDEF(oggpack_adv) "Advance the read location by n bits";
28 FDEF(oggpack_adv1) "";
30 FDEF(oggpack_write) "Write bits to the buffer.\n\n\
31 The first parameterf is an integer from which the bits will be extracted.\n\
32 The second parameter is the number of bits to write (defaults to 32)";
34 PyTypeObject py_oggpack_buffer_type = {
35 PyObject_HEAD_INIT(NULL)
37 "Oggpack_Buffer",
38 sizeof(py_oggpack_buffer),
41 /* Standard Methods */
42 /* (destructor) */ py_oggpack_buffer_dealloc,
43 /* (printfunc) */ 0,
44 /* (getattrfunc) */ py_oggpack_buffer_getattr,
45 /* (setattrfunc) */ 0,
46 /* (cmpfunc) */ 0,
47 /* (reprfunc) */ py_oggpack_repr,
49 /* Type Categories */
50 0, /* as number */
51 0, /* as sequence */
52 0, /* as mapping */
53 0, /* hash */
54 0, /* binary */
55 0, /* repr */
56 0, /* getattro */
57 0, /* setattro */
58 0, /* as buffer */
59 0, /* tp_flags */
60 py_oggpack_buffer_doc
63 static PyMethodDef py_oggpack_buffer_methods[] = {
64 {"reset", py_oggpack_reset,
65 METH_VARARGS, py_oggpack_reset_doc},
66 {"writeclear", py_oggpack_writeclear,
67 METH_VARARGS, py_oggpack_writeclear_doc},
68 {"look", py_oggpack_look,
69 METH_VARARGS, py_oggpack_look_doc},
70 {"look1", py_oggpack_look1,
71 METH_VARARGS, py_oggpack_look1_doc},
72 {"bytes", py_oggpack_bytes,
73 METH_VARARGS, py_oggpack_bytes_doc},
74 {"bits", py_oggpack_bits,
75 METH_VARARGS, py_oggpack_bits_doc},
76 {"read", py_oggpack_read,
77 METH_VARARGS, py_oggpack_read_doc},
78 {"read1", py_oggpack_read1,
79 METH_VARARGS, py_oggpack_read1_doc},
80 {"write", py_oggpack_write,
81 METH_VARARGS, py_oggpack_write_doc},
82 {"adv", py_oggpack_adv,
83 METH_VARARGS, py_oggpack_adv_doc},
84 {"adv1", py_oggpack_adv1,
85 METH_VARARGS, py_oggpack_adv1_doc},
86 {NULL, NULL}
89 static void
90 py_oggpack_buffer_dealloc(PyObject *self)
92 PyMem_DEL(self);
95 static PyObject*
96 py_oggpack_buffer_getattr(PyObject *self, char *name)
98 return Py_FindMethod(py_oggpack_buffer_methods, self, name);
101 PyObject *
102 py_oggpack_buffer_new(PyObject *self, PyObject *args)
104 py_oggpack_buffer *ret;
106 if (!PyArg_ParseTuple(args, ""))
107 return NULL;
109 ret = (py_oggpack_buffer *) PyObject_NEW(py_oggpack_buffer,
110 &py_oggpack_buffer_type);
111 if (ret == NULL)
112 return NULL;
114 oggpack_writeinit(&ret->ob);
115 return (PyObject *)ret;
118 static PyObject *
119 py_oggpack_reset(PyObject *self, PyObject *args)
121 if (!PyArg_ParseTuple(args, ""))
122 return NULL;
124 oggpack_reset(PY_OGGPACK_BUFF(self));
125 Py_INCREF(Py_None);
126 return Py_None;
129 static PyObject *
130 py_oggpack_writeclear(PyObject *self, PyObject *args)
132 if (!PyArg_ParseTuple(args, ""))
133 return NULL;
135 oggpack_writeclear(PY_OGGPACK_BUFF(self));
136 Py_INCREF(Py_None);
137 return Py_None;
140 static PyObject *
141 py_oggpack_look(PyObject *self, PyObject *args)
143 int bits = 32;
144 long ret;
145 if (!PyArg_ParseTuple(args, "l", &bits))
146 return NULL;
148 if (bits > 32) {
149 PyErr_SetString(PyExc_ValueError, "Cannot look at more than 32 bits");
150 return NULL;
153 ret = oggpack_look(PY_OGGPACK_BUFF(self), bits);
154 return PyLong_FromLong(ret);
157 static PyObject *
158 py_oggpack_look1(PyObject *self, PyObject *args)
160 long ret;
162 if (!PyArg_ParseTuple(args, ""))
163 return NULL;
165 ret = oggpack_look1(PY_OGGPACK_BUFF(self));
166 return PyLong_FromLong(ret);
169 static PyObject *
170 py_oggpack_bytes(PyObject *self, PyObject *args)
172 long ret;
174 if (!PyArg_ParseTuple(args, ""))
175 return NULL;
177 ret = oggpack_bytes(PY_OGGPACK_BUFF(self));
178 return PyLong_FromLong(ret);
181 static PyObject *
182 py_oggpack_bits(PyObject *self, PyObject *args)
184 long ret;
186 if (!PyArg_ParseTuple(args, ""))
187 return NULL;
189 ret = oggpack_bits(PY_OGGPACK_BUFF(self));
190 return PyLong_FromLong(ret);
193 static PyObject *
194 py_oggpack_write(PyObject *self, PyObject *args)
196 long val;
197 int bits = 32;
199 if (!PyArg_ParseTuple(args, "l|l", &val, &bits))
200 return NULL;
202 if (bits > 32) {
203 PyErr_SetString(PyExc_ValueError, "Cannot write more than 32 bits");
204 return NULL;
207 oggpack_write(PY_OGGPACK_BUFF(self), val, bits);
209 Py_INCREF(Py_None);
210 return Py_None;
213 static PyObject *
214 py_oggpack_read(PyObject *self, PyObject *args)
216 int bits = 32;
217 long ret;
219 if (!PyArg_ParseTuple(args, "|i", &bits))
220 return NULL;
222 if (bits > 32) {
223 PyErr_SetString(PyExc_ValueError, "Cannot read more than 32 bits");
224 return NULL;
227 ret = oggpack_read(PY_OGGPACK_BUFF(self), bits);
229 return PyInt_FromLong(ret);
232 static PyObject *
233 py_oggpack_read1(PyObject *self, PyObject *args)
235 long ret;
237 if (!PyArg_ParseTuple(args, ""))
238 return NULL;
240 ret = oggpack_read1(PY_OGGPACK_BUFF(self));
241 return PyInt_FromLong(ret);
244 static PyObject *
245 py_oggpack_adv(PyObject *self, PyObject *args)
247 int bits;
249 if (!PyArg_ParseTuple(args, "i", &bits))
250 return NULL;
252 oggpack_adv(PY_OGGPACK_BUFF(self), bits);
254 Py_INCREF(Py_None);
255 return Py_None;
258 static PyObject *
259 py_oggpack_adv1(PyObject *self, PyObject *args)
261 if (!PyArg_ParseTuple(args, ""))
262 return NULL;
264 oggpack_adv1(PY_OGGPACK_BUFF(self));
265 Py_INCREF(Py_None);
266 return Py_None;
269 static PyObject *
270 py_oggpack_repr(PyObject *self)
272 oggpack_buffer *ob = PY_OGGPACK_BUFF(self);
273 char buf[256];
275 sprintf(buf, "<OggPackBuff, endbyte = %ld, endbit = %d at %p>", ob->endbyte,
276 ob->endbit, self);
277 return PyString_FromString(buf);