File: pyfragment.c

package info (click to toggle)
libgetdata 0.11.0-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,144 kB
  • sloc: ansic: 100,814; cpp: 4,843; fortran: 4,548; f90: 2,561; python: 2,406; perl: 2,274; makefile: 1,487; php: 1,465; sh: 86
file content (624 lines) | stat: -rw-r--r-- 18,236 bytes parent folder | download | duplicates (5)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* Copyright (C) 2009-2016 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
 */
#include "gdpy_intern.h"

/* Fragment */
static void gdpy_fragment_delete(struct gdpy_fragment_t *self)
{
  dtrace("%p", self);

  Py_XDECREF(self->dirfile);
  PyObject_Del(self);

  dreturnvoid();
}

static PyObject *gdpy_fragment_create(PyTypeObject *type, PyObject *args,
    PyObject *keys)
{
  struct gdpy_fragment_t *self;

  dtrace("%p, %p, %p", type, args, keys);

  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)
{
  char *keywords[] = {"dirfile", "index", NULL};

  dtrace("%p, %p, %p", self, args, keys);

  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)
{
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  pyobj = gdpyint_fromlong(self->n);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_getname(struct gdpy_fragment_t *self,
    void *closure)
{
  const char *name;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  name = gd_fragmentname(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = gdpyobj_from_path(name);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_getencoding(struct gdpy_fragment_t *self,
    void *closure)
{
  unsigned long enc;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  enc = gd_encoding(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = PyLong_FromUnsignedLong(enc);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_setencoding(struct gdpy_fragment_t *self,
    PyObject *args, PyObject *keys)
{
  char *keywords[] = { "encoding", "recode", NULL };
  unsigned long enc;
  int recode = 0;

  dtrace("%p, %p, %p", self, args, keys);

  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);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  Py_INCREF(Py_None);
  dreturn("%p", Py_None);
  return Py_None;
}

static PyObject *gdpy_fragment_getendianness(struct gdpy_fragment_t *self,
    void *closure)
{
  unsigned long end;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  end = gd_endianness(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = PyLong_FromUnsignedLong(end);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_setendianness(struct gdpy_fragment_t *self,
    PyObject *args, PyObject *keys)
{
  char *keywords[] = { "endianness", "recode", NULL };
  unsigned long end;
  int recode = 0;

  dtrace("%p, %p, %p", self, args, keys);

  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);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  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);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  Py_INCREF(Py_None);
  dreturn("%p", Py_None);
  return Py_None;
}

static PyObject *gdpy_fragment_getoffset(struct gdpy_fragment_t *self,
    void *closure)
{
  gd_off64_t offset;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  offset = gd_frameoffset64(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = PyLong_FromLongLong((PY_LONG_LONG)offset);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_setoffset(struct gdpy_fragment_t *self,
    PyObject *args, PyObject *keys)
{
  char *keywords[] = { "frameoffset", "recode", NULL };
  PY_LONG_LONG offset;
  int recode = 0;

  dtrace("%p, %p, %p", self, args, keys);

  if (!PyArg_ParseTupleAndKeywords(args, keys,
        "L|i:pygetdata.fragment.alter_frameoffset", keywords, &offset, &recode))
  {
    dreturn("%p", NULL);
    return NULL;
  }

  gd_alter_frameoffset64(self->dirfile->D, (gd_off64_t)offset, self->n,
      recode);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  Py_INCREF(Py_None);
  dreturn("%p", Py_None);
  return Py_None;
}

static PyObject *gdpy_fragment_getparent(struct gdpy_fragment_t *self,
    void *closure)
{
  int parent;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  parent = gd_parent_fragment(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = gdpyint_fromlong(parent);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_getprotection(struct gdpy_fragment_t *self,
    void *closure)
{
  int prot;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  prot = gd_protection(self->dirfile->D, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = gdpyint_fromlong(prot);

  dreturn("%p", pyobj);
  return pyobj;
}

static int gdpy_fragment_setprotection(struct gdpy_fragment_t *self,
    PyObject *value, void *closure)
{
  int p = GD_PROTECT_NONE;

  dtrace("%p, %p, %p", self, value, closure);

  if (value)
    p = (int)gdpy_long_from_pyobj(value);

  if (PyErr_Occurred()) {
    dreturn("%i", -1);
    return -1;
  }

  gd_alter_protection(self->dirfile->D, p, self->n);

  GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);

  dreturn("%i", 0);
  return 0;
}

static PyObject *gdpy_fragment_getns(struct gdpy_fragment_t *self,
    void *closure)
{
  const char *ns;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  ns = gd_fragment_namespace(self->dirfile->D, self->n, NULL);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  pyobj = gdpyobj_from_string(ns, self->dirfile->char_enc);

  dreturn("%p", pyobj);
  return pyobj;
}

static PyObject *gdpy_fragment_getprefix(struct gdpy_fragment_t *self,
    void *closure)
{
  char *prefix, *suffix;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  gd_fragment_affixes(self->dirfile->D, self->n, &prefix, &suffix);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  PyMem_Free(suffix);
  if (prefix == NULL) {
    Py_INCREF(Py_None);
    dreturn("%p", Py_None);
    return Py_None;
  }

  pyobj = gdpyobj_from_string(prefix, self->dirfile->char_enc);
  PyMem_Free(prefix);

  dreturn("%p", pyobj);
  return pyobj;
}

static int gdpy_fragment_setns(struct gdpy_fragment_t *self,
    PyObject *value, void *closure)
{
  char *ns;

  dtrace("%p, %p, %p", self, value, closure);

  /* deleting namespace is equivalent to setting it to "" */
  if (value == NULL)
    ns = gdpy_strdup("");
  else
    ns = gdpy_string_from_pyobj(value, self->dirfile->char_enc,
        "namespace must be string");

  if (ns == NULL) {
    dreturn("%i", -1);
    return -1;
  }

  gd_fragment_namespace(self->dirfile->D, self->n, ns);

  PyMem_Free(ns);

  GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);

  dreturn("%i", 0);
  return 0;
}

static int gdpy_fragment_setprefix(struct gdpy_fragment_t *self,
    PyObject *value, void *closure)
{
  char *prefix;

  dtrace("%p, %p, %p", self, value, closure);

  if (value == NULL) {
    if (self->n == 0) {
      prefix = gdpy_strdup("");
      if (prefix == NULL)
        PyErr_NoMemory();
    } else {
      char *suffix = NULL;
      gd_fragment_affixes(self->dirfile->D, self->n, &prefix, &suffix);
      PyMem_Free(suffix);

      GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);
    }
  } else
    prefix = gdpy_string_from_pyobj(value, self->dirfile->char_enc,
        "prefix must be string");

  if (prefix == NULL) {
    dreturn("%i", -1);
    return -1;
  }

  gd_alter_affixes(self->dirfile->D, self->n, prefix, NULL);

  PyMem_Free(prefix);

  GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);

  dreturn("%i", 0);
  return 0;
}

static PyObject *gdpy_fragment_getsuffix(struct gdpy_fragment_t *self,
    void *closure)
{
  char *prefix, *suffix;
  PyObject *pyobj;

  dtrace("%p, %p", self, closure);

  gd_fragment_affixes(self->dirfile->D, self->n, &prefix, &suffix);

  GDPY_CHECK_ERROR(self->dirfile->D, NULL, self->dirfile->char_enc);

  PyMem_Free(prefix);
  if (suffix == NULL) {
    Py_INCREF(Py_None);
    dreturn("%p", Py_None);
    return Py_None;
  }

  pyobj = gdpyobj_from_string(suffix, self->dirfile->char_enc);
  PyMem_Free(suffix);

  dreturn("%p", pyobj);
  return pyobj;
}

static int gdpy_fragment_setsuffix(struct gdpy_fragment_t *self,
    PyObject *value, void *closure)
{
  char *suffix;

  dtrace("%p, %p, %p", self, value, closure);

  if (value == NULL) {
    if (self->n == 0) {
      suffix = gdpy_strdup("");
      if (suffix == NULL)
        PyErr_NoMemory();
    } else {
      char *prefix = NULL;
      gd_fragment_affixes(self->dirfile->D, self->n, &prefix, &suffix);
      PyMem_Free(prefix);

      GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);
    }
  } else
    suffix = gdpy_string_from_pyobj(value, self->dirfile->char_enc,
        "suffix must be string");

  if (suffix == NULL) {
    dreturn("%i", -1);
    return -1;
  }

  gd_alter_affixes(self->dirfile->D, self->n, NULL, suffix);

  PyMem_Free(suffix);

  GDPY_CHECK_ERROR(self->dirfile->D, -1, self->dirfile->char_enc);

  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 },
  { "namespace", (getter)gdpy_fragment_getns, (setter)gdpy_fragment_setns,
    "The root namespace of the fragment.  See gd_fragment_affix(3).\n",
    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 },
  { "prefix", (getter)gdpy_fragment_getprefix, (setter)gdpy_fragment_setprefix,
    "The prefix applied to all fields defined in this fragment and\n"
      "subfragments.  See gd_fragment_affixes(3).",
    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 },
  { "suffix", (getter)gdpy_fragment_getsuffix, (setter)gdpy_fragment_setsuffix,
    "The suffix applied to all fields defined in this fragment and\n"
      "subfragments.  See gd_fragment_affixes(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 = {
  PyVarObject_HEAD_INIT(NULL, 0)
  "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 */
};