File: pydbtoc.cpp

package info (click to toggle)
silo-llnl 4.10.2.real-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 26,472 kB
  • sloc: ansic: 86,101; sh: 18,875; cpp: 6,690; pascal: 1,261; makefile: 1,146; fortran: 882; python: 313; xml: 109; csh: 74; f90: 69; perl: 54
file content (444 lines) | stat: -rw-r--r-- 14,503 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
// LLNL-CODE-425250.
// All rights reserved.
// 
// This file is part of Silo. For details, see silo.llnl.gov.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the disclaimer below.
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the disclaimer (as noted
//      below) in the documentation and/or other materials provided with
//      the distribution.
//    * Neither the name of the LLNS/LLNL nor the names of its
//      contributors may be used to endorse or promote products derived
//      from this software without specific prior written permission.
// 
// THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
// "AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
// LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
// LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
// CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
// PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
// NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// This work was produced at Lawrence Livermore National Laboratory under
// Contract  No.   DE-AC52-07NA27344 with  the  DOE.  Neither the  United
// States Government  nor Lawrence  Livermore National Security,  LLC nor
// any of  their employees,  makes any warranty,  express or  implied, or
// assumes   any   liability   or   responsibility  for   the   accuracy,
// completeness, or usefulness of any information, apparatus, product, or
// process  disclosed, or  represents  that its  use  would not  infringe
// privately-owned   rights.  Any  reference   herein  to   any  specific
// commercial products,  process, or  services by trade  name, trademark,
// manufacturer or otherwise does not necessarily constitute or imply its
// endorsement,  recommendation,   or  favoring  by   the  United  States
// Government or Lawrence Livermore National Security, LLC. The views and
// opinions  of authors  expressed  herein do  not  necessarily state  or
// reflect those  of the United  States Government or  Lawrence Livermore
// National  Security, LLC,  and shall  not  be used  for advertising  or
// product endorsement purposes.

// #if PY_MAJOR_VERSION >= 3
#define PyString_FromString(x) PyUnicode_FromString(x)
// #endif

#include "pydbtoc.h"

// ****************************************************************************
//  Method:  DBtoc_dealloc
//
//  Purpose:
//    Convert the DBtocObject to a string representation.
//
//  Arguments:
//    s          the target string, with space already allocated
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
static void DBtoc_dealloc(PyObject *self)
{
    PyObject_Del(self);
}

#define PRINT_OBJS(nm)                                 \
{                                                      \
    if (toc->n ## nm)                                  \
    {                                                  \
        sprintf(tmp, "n%s = %d\n", #nm, toc->n ## nm); \
        len += strlen(tmp);                            \
        if (s) strcat(s, tmp);                         \
                                                       \
        sprintf(tmp, "%s_names = (", #nm);             \
        len += strlen(tmp);                            \
        if (s) strcat(s, tmp);                         \
        for (int i=0; i<toc->n ## nm; i++)             \
        {                                              \
            len += strlen(toc->nm ## _names[i]);       \
            if (s) strcat(s, toc->nm ## _names[i]);    \
            if (i < toc->n ## nm -1)                   \
            {                                          \
                len += strlen(sep);                    \
                if (s) strcat(s, sep);                 \
            }                                          \
        }                                              \
        len += strlen(term);                           \
        if (s) strcat(s, term);                        \
    }                                                  \
}

// ****************************************************************************
//  Method:  DBtoc_as_string
//
//  Purpose:
//    Convert the DBtocObject to a string representation.
//
//  Arguments:
//    s          the target string, with space already allocated
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
//  Modifications
//    Mark C. Miller, Tue Nov 13 14:36:36 PST 2012
//    Added all the members of the toc.
// ****************************************************************************
static int DBtoc_as_string(PyObject *self, char *s)
{
    DBtocObject *obj = (DBtocObject*)self;
    DBtoc *toc = obj->toc;
    const char *sep = ", ";
    const char *term = ")\n";
    char tmp[1000];
    int len = 0;

    if (s) strcpy(s, "");

    PRINT_OBJS(var);
    PRINT_OBJS(dir);
    PRINT_OBJS(curve);
    PRINT_OBJS(multimesh);
    PRINT_OBJS(multivar);
    PRINT_OBJS(multimat);
    PRINT_OBJS(multimatspecies);
    PRINT_OBJS(csgmesh);
    PRINT_OBJS(csgvar);
    PRINT_OBJS(defvars);
    PRINT_OBJS(qmesh);
    PRINT_OBJS(qvar);
    PRINT_OBJS(ucdmesh);
    PRINT_OBJS(ucdvar);
    PRINT_OBJS(ptmesh);
    PRINT_OBJS(ptvar);
    PRINT_OBJS(mat);
    PRINT_OBJS(matspecies);
    PRINT_OBJS(array);
    PRINT_OBJS(mrgtree);
    PRINT_OBJS(mrgvar);
    PRINT_OBJS(groupelmap);

    return len;
}

// ****************************************************************************
//  Method:  DBtoc_str
//
//  Purpose:
//    Convert the DBtocObject to a PyString
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
static PyObject *DBtoc_str(PyObject *self)
{
    PyObject *retval;
    int len = DBtoc_as_string(self, 0);
    char *str = new char[len]; 
    DBtoc_as_string(self, str);
    retval = PyString_FromString(str);
    delete [] str;
    return retval; 
}

// ****************************************************************************
//  Method:  DBtoc_print
//
//  Purpose:
//    Print the DBtocObject into a file as text
//
//  Arguments:
//    fp         the file pointer
//    flags      (unused)
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
static int DBtoc_print(PyObject *self, FILE *fp, int flags)
{
    DBtocObject *obj = (DBtocObject*)self;
    int len = DBtoc_as_string(self, 0);
    char *str = new char[len]; 
    DBtoc_as_string(self, str);
    fprintf(fp, "%s", str);
    delete [] str;
    return 0;
}

#define GET_FUNC_DEFS(nm)                                                  \
static PyObject *DBtoc_GetN ## nm(PyObject *self, PyObject *args)          \
{                                                                          \
    DBtoc *toc = ((DBtocObject*)self)->toc;                                \
    PyObject *retval = PyLong_FromLong(toc->n ## nm);                       \
    return retval;                                                         \
}                                                                          \
static PyObject *DBtoc_Get ## nm ## names(PyObject *self, PyObject *args)  \
{                                                                          \
    DBtoc *toc = ((DBtocObject*)self)->toc;                                \
    PyObject *retval = PyTuple_New(toc->n ## nm);                          \
    for (int i=0; i<toc->n ## nm; i++)                                     \
    {                                                                      \
        PyTuple_SET_ITEM(retval, i, PyString_FromString(toc->nm ## _names[i])); \
    }                                                                      \
    return retval;                                                         \
}

GET_FUNC_DEFS(var)
GET_FUNC_DEFS(dir)
GET_FUNC_DEFS(curve);
GET_FUNC_DEFS(multimesh);
GET_FUNC_DEFS(multivar);
GET_FUNC_DEFS(multimat);
GET_FUNC_DEFS(multimatspecies);
GET_FUNC_DEFS(csgmesh);
GET_FUNC_DEFS(csgvar);
GET_FUNC_DEFS(defvars);
GET_FUNC_DEFS(qmesh);
GET_FUNC_DEFS(qvar);
GET_FUNC_DEFS(ucdmesh);
GET_FUNC_DEFS(ucdvar);
GET_FUNC_DEFS(ptmesh);
GET_FUNC_DEFS(ptvar);
GET_FUNC_DEFS(mat);
GET_FUNC_DEFS(matspecies);
GET_FUNC_DEFS(array);
GET_FUNC_DEFS(mrgtree);
GET_FUNC_DEFS(mrgvar);
GET_FUNC_DEFS(groupelmap);

#define GET_FUNC_N(nm) if (!strcmp(name, "n" #nm)) return DBtoc_GetN ## nm(self, NULL);
#define GET_FUNC_NAMES(nm) if (!strcmp(name, #nm "_names")) return DBtoc_Get ## nm ## names(self, NULL);

// ****************************************************************************
//  Method: DBtoc_getattr 
//
//  Purpose:
//    Return an attribute by name.
//
//  Arguments:
//    name       the name of the attribute to return
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
//  Modifications
//    Mark C. Miller, Tue Nov 13 14:36:36 PST 2012
//    Added all the members of the toc.
// ****************************************************************************
static PyObject *DBtoc_getattr(PyObject *self, char *name)
{
    GET_FUNC_N(var);
    GET_FUNC_N(dir);
    GET_FUNC_N(curve);
    GET_FUNC_N(multimesh);
    GET_FUNC_N(multivar);
    GET_FUNC_N(multimat);
    GET_FUNC_N(multimatspecies);
    GET_FUNC_N(csgmesh);
    GET_FUNC_N(csgvar);
    GET_FUNC_N(defvars);
    GET_FUNC_N(qmesh);
    GET_FUNC_N(qvar);
    GET_FUNC_N(ucdmesh);
    GET_FUNC_N(ucdvar);
    GET_FUNC_N(ptmesh);
    GET_FUNC_N(ptvar);
    GET_FUNC_N(mat);
    GET_FUNC_N(matspecies);
    GET_FUNC_N(array);
    GET_FUNC_N(mrgtree);
    GET_FUNC_N(mrgvar);
    GET_FUNC_N(groupelmap);

    GET_FUNC_NAMES(var);
    GET_FUNC_NAMES(dir);
    GET_FUNC_NAMES(curve);
    GET_FUNC_NAMES(multimesh);
    GET_FUNC_NAMES(multivar);
    GET_FUNC_NAMES(multimat);
    GET_FUNC_NAMES(multimatspecies);
    GET_FUNC_NAMES(csgmesh);
    GET_FUNC_NAMES(csgvar);
    GET_FUNC_NAMES(defvars);
    GET_FUNC_NAMES(qmesh);
    GET_FUNC_NAMES(qvar);
    GET_FUNC_NAMES(ucdmesh);
    GET_FUNC_NAMES(ucdvar);
    GET_FUNC_NAMES(ptmesh);
    GET_FUNC_NAMES(ptvar);
    GET_FUNC_NAMES(mat);
    GET_FUNC_NAMES(matspecies);
    GET_FUNC_NAMES(array);
    GET_FUNC_NAMES(mrgtree);
    GET_FUNC_NAMES(mrgvar);
    GET_FUNC_NAMES(groupelmap);

    return 0;
}

// ****************************************************************************
//  Method:  DBtoc_compare
//
//  Purpose:
//    Compare two DBtocObjects.
//
//  Arguments:
//    u, v       the objects to compare
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
static int DBtoc_compare(PyObject *v, PyObject *w)
{
    DBtoc *a = ((DBtocObject *)v)->toc;
    DBtoc *b = ((DBtocObject *)w)->toc;
    return (a<b) ? -1 : ((a==b) ? 0 : +1);
}


// ****************************************************************************
//  DBtoc Python Type Object
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
PyTypeObject DBtocType =
{
    //
    // Type header
    //
    PyObject_HEAD_INIT(&PyType_Type)
#if PY_MAJOR_VERSION < 3
    ob_size           : 0,
#endif
    tp_name           : "DBtoc",
    tp_basicsize      : sizeof(DBtocObject),
    tp_itemsize       : 0,
    //
    // Standard methods
    //
    tp_dealloc        : (destructor)DBtoc_dealloc,
    tp_print          : (printfunc)DBtoc_print,
#if PY_MAJOR_VERSION >= 3
    tp_getattr        : 0,
#else
    tp_getattr        : (getattrfunc)DBtoc_getattr,
#endif    
    tp_setattr        : 0, // object is read-only
#if PY_MAJOR_VERSION >= 3
    tp_as_async       : (PyAsyncMethods*) NULL,
#else    
    tp_compare        : (cmpfunc)DBtoc_compare,
#endif
    tp_repr           : (reprfunc)0,                 
    //
    // Type categories
    //
    tp_as_number      : 0,
    tp_as_sequence    : 0,
    tp_as_mapping     : 0,
    //
    // More methods
    //

    tp_hash           : 0, 
    tp_call           :  0,
    tp_str            : (reprfunc)DBtoc_str,
    tp_getattro       : 0,
    tp_setattro       : 0,
    tp_as_buffer      : 0,
#if PY_MAJOR_VERSION < 3    
    tp_flags          : Py_TPFLAGS_CHECKTYPES,
#else
    tp_flags          : Py_TPFLAGS_DEFAULT, 
#endif
    tp_doc            : "This class wraps a Silo DBtoc object.",
    tp_traverse       : 0, 
    tp_clear          : 0,
    tp_richcompare    : 0,
    tp_weaklistoffset : 0,                 
    // PYTHON 2.2 FROM HERE
    0,
    0,
    0,
    0,
};

// ****************************************************************************
//  Method:  DBtoc_NEW
//
//  Purpose:
//    Allocate and initialize a DBtocObject.
//
//  Arguments:
//    init       the initial value
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
PyObject *DBtoc_NEW(DBtoc *init)
{
    DBtocObject *obj = PyObject_NEW(DBtocObject, &DBtocType);
    if (obj)
    {
        obj->toc = init;
    }
    return (PyObject*)obj;
}


// ****************************************************************************
//  Method:  DBtoc_NEW
//
//  Purpose:
//    Allocate and initialize a DBtocObject with default values.
//
//  Python Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
PyObject *DBtoc_new(PyObject *self, PyObject *args)
{
    return DBtoc_NEW(NULL);
}