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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/* Copyright (C) 2009, 2010 D. V. Wiebe
*
***************************************************************************
*
* This file is part of the GetData project.
*
* GetData is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* GetData is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GetData; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define NO_IMPORT_ARRAY
#include "pygetdata.h"
/* Fragment */
static void gdpy_fragment_delete(struct gdpy_fragment_t* self)
{
dtrace("%p", self);
Py_XDECREF(self->dirfile);
dreturnvoid();
}
static PyObject* gdpy_fragment_create(PyTypeObject *type, PyObject *args,
PyObject *keys)
{
dtrace("%p, %p, %p", type, args, keys);
struct gdpy_fragment_t *self = (struct gdpy_fragment_t*)type->tp_alloc(type,
0);
if (self)
self->dirfile = NULL;
dreturn("%p", self);
return (PyObject*)self;
}
static int gdpy_fragment_init(struct gdpy_fragment_t* self, PyObject *args,
PyObject *keys)
{
dtrace("%p, %p, %p", self, args, keys);
char *keywords[] = {"dirifle", "index", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keys,
"O!i:pygetdata.fragment.__init__", keywords, &gdpy_dirfile,
&self->dirfile, self->n))
{
dreturn("%i", -1);
return -1;
}
Py_INCREF(self->dirfile);
dreturn("%i", 0);
return 0;
}
static PyObject* gdpy_fragment_getindex(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
PyObject* obj = PyInt_FromLong(self->n);
dreturn("%p", obj);
return obj;
}
static PyObject* gdpy_fragment_getname(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
const char* name = gd_fragmentname(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* pyname = PyString_FromString(name);
dreturn("%p", pyname);
return pyname;
}
static PyObject* gdpy_fragment_getencoding(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
unsigned long enc = gd_encoding(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* obj = PyLong_FromUnsignedLong(enc);
dreturn("%p", obj);
return obj;
}
static PyObject* gdpy_fragment_setencoding(struct gdpy_fragment_t* self,
PyObject* args, PyObject* keys)
{
dtrace("%p, %p, %p", self, args, keys);
char* keywords[] = { "encoding", "recode", NULL };
unsigned long enc;
int recode = 0;
if (!PyArg_ParseTupleAndKeywords(args, keys,
"k|i:pygetdata.fragment.alter_encoding", keywords, &enc, &recode))
{
dreturn("%p", NULL);
return NULL;
}
gd_alter_encoding(self->dirfile->D, enc, self->n, recode);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
Py_INCREF(Py_None);
dreturn("%p", Py_None);
return Py_None;
}
static PyObject* gdpy_fragment_getendianness(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
unsigned long end = gd_endianness(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* obj = PyLong_FromUnsignedLong(end);
dreturn("%p", obj);
return obj;
}
static PyObject* gdpy_fragment_setendianness(struct gdpy_fragment_t* self,
PyObject* args, PyObject* keys)
{
dtrace("%p, %p, %p", self, args, keys);
char* keywords[] = { "endianness", "recode", NULL };
unsigned long end;
int recode = 0;
if (!PyArg_ParseTupleAndKeywords(args, keys,
"k|i:pygetdata.fragment.alter_endianness", keywords, &end, &recode))
{
dreturn("%p", NULL);
return NULL;
}
gd_alter_endianness(self->dirfile->D, end, self->n, recode);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
Py_INCREF(Py_None);
dreturn("%p", Py_None);
return Py_None;
}
static PyObject* gdpy_fragment_rewrite(struct gdpy_fragment_t* self)
{
dtrace("%p", self);
gd_rewrite_fragment(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
Py_INCREF(Py_None);
dreturn("%p", Py_None);
return Py_None;
}
static PyObject* gdpy_fragment_getoffset(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
off_t offset = gd_frameoffset(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* obj = PyLong_FromLongLong((long long)offset);
dreturn("%p", obj);
return obj;
}
static PyObject* gdpy_fragment_setoffset(struct gdpy_fragment_t* self,
PyObject* args, PyObject* keys)
{
dtrace("%p, %p, %p", self, args, keys);
char* keywords[] = { "frameoffset", "recode", NULL };
long long offset;
int recode = 0;
if (!PyArg_ParseTupleAndKeywords(args, keys,
"L|i:pygetdata.fragment.alter_frameoffset", keywords, &offset, &recode))
{
dreturn("%p", NULL);
return NULL;
}
gd_alter_frameoffset(self->dirfile->D, (off_t)offset, self->n,
recode);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
Py_INCREF(Py_None);
dreturn("%p", Py_None);
return Py_None;
}
static PyObject* gdpy_fragment_getparent(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
int parent = gd_parent_fragment(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* obj = PyInt_FromLong(parent);
dreturn("%p", obj);
return obj;
}
static PyObject* gdpy_fragment_getprotection(struct gdpy_fragment_t* self,
void* closure)
{
dtrace("%p, %p", self, closure);
int prot = gd_protection(self->dirfile->D, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, NULL);
PyObject* obj = PyInt_FromLong(prot);
dreturn("%p", obj);
return obj;
}
static int gdpy_fragment_setprotection(struct gdpy_fragment_t* self,
PyObject *value, void *closure)
{
dtrace("%p, %p, %p", self, value, closure);
int p = PyInt_AsLong(value);
if (PyErr_Occurred()) {
dreturn("%i", -1);
return -1;
}
gd_alter_protection(self->dirfile->D, p, self->n);
PYGD_CHECK_ERROR(self->dirfile->D, -1);
dreturn("%i", 0);
return 0;
}
static PyGetSetDef gdpy_fragment_getset[] = {
{ "encoding", (getter)gdpy_fragment_getencoding, NULL,
"The encoding scheme of this fragment. This will be one of the\n"
"pygetdata.*_ENCODED symbols. To change this value, use the\n"
"alter_encoding method.",
NULL },
{ "endianness", (getter)gdpy_fragment_getendianness, NULL,
"The byte sex of this fragment. This will be either\n"
"pygetdata.BIG_ENDIAN or pygetdata.LITTLE_ENDIAN, bitwise or'd with\n"
"pygetdata.ARM_ENDIAN or pygetdata.NOT_ARM_ENDIAN. To change this\n"
"value, use the alter_endianness method.",
NULL },
{ "frameoffset", (getter)gdpy_fragment_getoffset, NULL,
"The frame offset of this fragment. To change this value, use the\n"
"alter_frameoffset method.",
NULL },
{ "index", (getter)gdpy_fragment_getindex, NULL,
"The index number of this fragment. This is simply the index value\n"
"passed to the constructor.",
NULL },
{ "name", (getter)gdpy_fragment_getname, NULL,
"The pathname of this fragment. This attribute cannot be changed.\n"
"See gd_fragmentname(3).",
NULL },
{ "parent", (getter)gdpy_fragment_getparent, NULL,
"The fragment index of this fragment's parent. Since the primary\n"
"format file has no parent, an error will occur if an attempt is made\n"
"to read this attribute for the primary format file. This value\n"
"cannot be changed. To move a format file fragment to a different\n"
"parent fragment, use dirfile.uninclude() and dirfile.include().",
NULL },
{ "protection", (getter)gdpy_fragment_getprotection,
(setter)gdpy_fragment_setprotection,
"The (advisory) protection level of this fragment. This will be one\n"
"the pygetdata.PROTECT_* symbols. The protection level of this\n"
"fragment can be changed by assigning to this attribute. See\n"
"gd_protection(3) and gd_alter_protection(3).",
NULL },
{ NULL }
};
static PyMethodDef gdpy_fragment_methods[] = {
{"alter_encoding", (PyCFunction)gdpy_fragment_setencoding,
METH_VARARGS | METH_KEYWORDS,
"alter_encoding(encoding [, recode])\n\n"
"Change the encoding scheme of this fragment. The 'encoding'\n"
"parameter should be one of the pygetdata.*_ENCODED symbols\n"
"(excluding pygetdata.AUTO_ENCODED). If 'recode' is given, and is\n"
"non-zero, the RAW files affected by this change will be converted\n"
"to the new encoding. See gd_alter_encoding(3)."
},
{"alter_endianness", (PyCFunction)gdpy_fragment_setendianness,
METH_VARARGS | METH_KEYWORDS,
"alter_endianness(endianness [, recode])\n\n"
"Change the byte sex of this fragment. The 'endianness' parameter\n"
"should be pygetdata.LITTLE_ENDIAN, pygetdata.BIG_ENDIAN, or some\n"
"combination of these two as described in the gd_alter_endianness\n"
/* -----------------------------------------------------------------| */
"manual page, and possibly bitwise or'd with pygetdata.ARM_ENDIAN or\n"
"pygetdata.NOT_ARM_ENDIAN. If 'recode' is given, and is non-zero,\n"
"the RAW files affected by this change will be converted to the byte\n"
"sex. See gd_alter_endianness(3)."
},
{"alter_frameoffset", (PyCFunction)gdpy_fragment_setoffset,
METH_VARARGS | METH_KEYWORDS,
"alter_frameoffset(frameoffset [, recode])\n\n"
"Change the frame offset of this fragment. The 'frameoffset'\n"
"parameter should specify the new frame offset. If 'recode' is\n"
"given, and is non-zero, the RAW files affected bt this change will\n"
"be shifted appropriately for the new frame offset. See\n"
"gd_alter_frameoffset(3)."
},
{"rewrite", (PyCFunction)gdpy_fragment_rewrite, METH_NOARGS,
"rewrite()\n\n"
"Force re-writing of this fragment on disc, regardless of whether\n"
"it has been modified or not. See gd_rewrite_fragment(3)."
},
{ NULL, NULL, 0, NULL }
};
#define FRAGMENT_DOC \
"fragment(dirfile, index)\n\n"\
"Returns a fragment object which describes the fragment-specific metadata\n"\
"of one format file fragment in a dirfile. The 'dirfile' parameter\n"\
"should be a dirfile object. Index is the fragment index of the desired\n"\
"format file fragment. This constructor is equivalent to calling\n\n"\
" dirfile.fragment(index)\n\n"\
"The fragment object remains connected to its dirfile, and changes made\n"\
"to the fragment object are propagated back to the dirfile. This object\n"\
/* ---------------------------------------------------------------------| */\
"creates a new reference to the supplied dirfile. Calling\n"\
"close() or discard() on the dirfile and then attempting to access a\n"\
"fragment object previously derived from that dirfile will result in an\n"\
"error."
PyTypeObject gdpy_fragment = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"pygetdata.fragment", /* tp_name */
sizeof(struct gdpy_fragment_t),/* tp_basicsize */
0, /* tp_itemsize */
(destructor)gdpy_fragment_delete, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
FRAGMENT_DOC, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
gdpy_fragment_methods, /* tp_methods */
0, /* tp_members */
gdpy_fragment_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)gdpy_fragment_init, /* tp_init */
0, /* tp_alloc */
gdpy_fragment_create, /* tp_new */
};
|