source: trunk/src/fortran/rappture_fortran.c @ 47

Last change on this file since 47 was 47, checked in by dkearney, 19 years ago

fixed python interface memory problem with py_decref() where i was decreffing a python object before i was finished using it.
this is a fix for shaikh's dgfet code not printing large amounts of data to the run.xml file.

File size: 38.5 KB
Line 
1#include "rappture_interface.h"
2#include "RpDict.h"
3#include <string.h>
4
5/*
6// rappture-fortran api
7*/
8
9#ifdef COMPNAME_ADD1UNDERSCORE
10#   define rp_init                 rp_init_
11#   define rp_lib                  rp_lib_
12#   define rp_lib_element_comp     rp_lib_element_comp_
13#   define rp_lib_element_id       rp_lib_element_id_
14#   define rp_lib_element_type     rp_lib_element_type_
15#   define rp_lib_element_str      rp_lib_element_str_
16#   define rp_lib_element_obj      rp_lib_element_obj_
17#   define rp_lib_child_num        rp_lib_child_num_
18#   define rp_lib_child_comp       rp_lib_child_comp_
19#   define rp_lib_child_id         rp_lib_child_id_
20#   define rp_lib_child_type       rp_lib_child_type_
21#   define rp_lib_child_str        rp_lib_child_str_
22#   define rp_lib_child_obj        rp_lib_child_obj_
23#   define rp_lib_get              rp_lib_get_
24#   define rp_lib_get_double       rp_lib_get_double_
25#   define rp_lib_put_str          rp_lib_put_str_
26#   define rp_lib_put_id_str       rp_lib_put_id_str_
27#   define rp_lib_put_obj          rp_lib_put_obj_
28#   define rp_lib_put_id_obj       rp_lib_put_id_obj_
29#   define rp_lib_remove           rp_lib_remove_
30#   define rp_lib_xml_len          rp_lib_xml_len_
31#   define rp_lib_write_xml        rp_lib_write_xml_
32#   define rp_lib_xml              rp_lib_xml_
33#   define rp_quit                 rp_quit_
34#elif defined(COMPNAME_ADD2UNDERSCORE)
35#   define rp_init                 rp_init__
36#   define rp_lib                  rp_lib__
37#   define rp_lib_element_comp     rp_lib_element_comp__
38#   define rp_lib_element_id       rp_lib_element_id__
39#   define rp_lib_element_type     rp_lib_element_type__
40#   define rp_lib_element_str      rp_lib_element_str__
41#   define rp_lib_element_obj      rp_lib_element_obj__
42#   define rp_lib_child_num        rp_lib_child_num__
43#   define rp_lib_child_comp       rp_lib_child_comp__
44#   define rp_lib_child_id         rp_lib_child_id__
45#   define rp_lib_child_type       rp_lib_child_type__
46#   define rp_lib_child_str        rp_lib_child_str__
47#   define rp_lib_child_obj        rp_lib_child_obj__
48#   define rp_lib_get              rp_lib_get__
49#   define rp_lib_get_double       rp_lib_get_double__
50#   define rp_lib_put_str          rp_lib_put_str__
51#   define rp_lib_put_id_str       rp_lib_put_id_str__
52#   define rp_lib_put_obj          rp_lib_put_obj__
53#   define rp_lib_put_id_obj       rp_lib_put_id_obj__
54#   define rp_lib_remove           rp_lib_remove__
55#   define rp_lib_xml_len          rp_lib_xml_len__
56#   define rp_lib_write_xml        rp_lib_write_xml__
57#   define rp_lib_xml              rp_lib_xml__
58#   define rp_quit                 rp_quit__
59#elif defined(COMPNAME_NOCHANGE)
60#   define rp_init                 rp_init
61#   define rp_lib                  rp_lib
62#   define rp_lib_element_comp     rp_lib_element_comp
63#   define rp_lib_element_id       rp_lib_element_id
64#   define rp_lib_element_type     rp_lib_element_type
65#   define rp_lib_element_str      rp_lib_element_str
66#   define rp_lib_element_obj      rp_lib_element_obj
67#   define rp_lib_child_num        rp_lib_child_num
68#   define rp_lib_child_comp       rp_lib_child_comp
69#   define rp_lib_child_id         rp_lib_child_id
70#   define rp_lib_child_type       rp_lib_child_type
71#   define rp_lib_child_str        rp_lib_child_str
72#   define rp_lib_child_obj        rp_lib_child_obj
73#   define rp_lib_get              rp_lib_get
74#   define rp_lib_get_double       rp_lib_get_double
75#   define rp_lib_put_str          rp_lib_put_str
76#   define rp_lib_put_id_str       rp_lib_put_id_str
77#   define rp_lib_put_obj          rp_lib_put_obj
78#   define rp_lib_put_id_obj       rp_lib_put_id_obj
79#   define rp_lib_remove           rp_lib_remove
80#   define rp_lib_xml_len          rp_lib_xml_len
81#   define rp_lib_write_xml        rp_lib_write_xml
82#   define rp_lib_xml              rp_lib_xml
83#   define rp_quit                 rp_quit
84#elif defined(COMPNAME_UPPERCASE)
85#   define rp_init                 RP_INIT
86#   define rp_lib                  RP_LIB
87#   define rp_lib_element_comp     RP_LIB_ELEMENT_COMP
88#   define rp_lib_element_id       RP_LIB_ELEMENT_ID
89#   define rp_lib_element_type     RP_LIB_ELEMENT_TYPE
90#   define rp_lib_element_str      RP_LIB_ELEMENT_STR
91#   define rp_lib_element_obj      RP_LIB_ELEMENT_OBJ
92#   define rp_lib_child_num        RP_LIB_CHILD_NUM
93#   define rp_lib_child_comp       RP_LIB_CHILD_COMP
94#   define rp_lib_child_id         RP_LIB_CHILD_ID
95#   define rp_lib_child_type       RP_LIB_CHILD_TYPE
96#   define rp_lib_child_str        RP_LIB_CHILD_STR
97#   define rp_lib_child_obj        RP_LIB_CHILD_OBJ
98#   define rp_lib_get              RP_LIB_GET
99#   define rp_lib_get_double       RP_LIB_GET_DOUBLE
100#   define rp_lib_put_str          RP_LIB_PUT_STR
101#   define rp_lib_put_id_str       RP_LIB_PUT_ID_STR
102#   define rp_lib_put_obj          RP_LIB_PUT_OBJ
103#   define rp_lib_put_id_obj       RP_LIB_PUT_ID_OBJ
104#   define rp_lib_remove           RP_LIB_REMOVE
105#   define rp_lib_xml_len          RP_LIB_XML_LEN
106#   define rp_lib_write_xml        RP_LIB_WRITE_XML
107#   define rp_lib_xml              RP_LIB_XML
108#   define rp_quit                 RP_QUIT
109#endif
110
111
112extern "C"
113void rp_init();
114
115extern "C"
116int rp_lib(const char* filePath, int filePath_len);
117
118extern "C"
119void rp_lib_element_comp( int* handle,
120                            char* path,
121                            char* retText,
122                            int path_len,
123                            int retText_len );
124
125extern "C"
126void rp_lib_element_id(   int* handle,
127                            char* path,
128                            char* retText,
129                            int path_len,
130                            int retText_len );
131
132extern "C"
133void rp_lib_element_type( int* handle,
134                            char* path,
135                            char* retText,
136                            int path_len,
137                            int retText_len );
138
139extern "C"
140void rp_lib_element_str(  int* handle,
141                            char* path,
142                            char* flavor,
143                            char* retText,
144                            int path_len,
145                            int flavor_len,
146                            int retText_len );
147
148extern "C"
149int rp_lib_element_obj(   int* handle,
150                            char* path,
151                            int path_len );
152
153extern "C"
154int rp_lib_child_num(    int* handle,
155                            char* path,
156                            int path_len);
157
158extern "C"
159int rp_lib_child_comp(   int* handle,    /* integer handle of library */
160                            char* path,     /* DOM path of requested object */
161                            char* type,     /* specific name of element */
162                            int* childNum,  /* child number for iteration */
163                            char* retText,  /* buffer to store return text */
164                            int path_len,   /* length of path */
165                            int type_len,   /* length of type */
166                            int retText_len /* length of return text buffer */
167                       );
168
169extern "C"
170int rp_lib_child_id(     int* handle,    /* integer handle of library */
171                            char* path,     /* DOM path of requested object */
172                            char* type,     /* specific name of element */
173                            int* childNum,  /* child number for iteration */
174                            char* retText,  /* buffer to store return text */
175                            int path_len,   /* length of path */
176                            int type_len,   /* length of type */
177                            int retText_len /* length of return text buffer */
178                       );
179
180extern "C"
181int rp_lib_child_type(   int* handle,    /* integer handle of library */
182                            char* path,     /* DOM path of requested object */
183                            char* type,     /* specific name of element */
184                            int* childNum,  /* child number for iteration */
185                            char* retText,  /* buffer to store return text */
186                            int path_len,   /* length of path */
187                            int type_len,   /* length of type */
188                            int retText_len /* length of return text buffer */
189                       );
190
191extern "C"
192int rp_lib_child_str(    int* handle,    /* integer handle of library */
193                            char* path,     /* DOM path of requested object */
194                            char* flavor,   /* information you want returned*/
195                            char* type,     /* specific name of element */
196                            int* childNum,  /* child number for iteration */
197                            char* retText,  /* buffer to store return text */
198                            int path_len,   /* length of path */
199                            int flavor_len, /* length of flavor */
200                            int type_len,   /* length of type */
201                            int retText_len /* length of return text buffer */
202                       );
203
204extern "C"
205int rp_lib_child_obj(    int* handle,
206                            char* path,
207                            char* type,
208                            int path_len,
209                            int type_len
210                          );
211
212extern "C"
213void rp_lib_get(          int* handle,
214                            char* path,
215                            char* retText,
216                            int path_len,
217                            int retText_len );
218
219extern "C"
220double rp_lib_get_double( int* handle,
221                            char* path,
222                            int path_len);
223
224extern "C"
225void rp_lib_put_str(     int* handle,
226                            char* path,
227                            char* value,
228                            int* append,
229                            int path_len,
230                            int value_len );
231
232extern "C"
233void rp_lib_put_id_str(  int* handle,
234                            char* path,
235                            char* value,
236                            char* id,
237                            int* append,
238                            int path_len,
239                            int value_len,
240                            int id_len );
241
242extern "C"
243void rp_lib_put_obj(     int* handle,
244                            char* path,
245                            int* valHandle,
246                            int* append,
247                            int path_len );
248
249extern "C"
250void rp_lib_put_id_obj(  int* handle,
251                            char* path,
252                            int* valHandle,
253                            char* id,
254                            int* append,
255                            int path_len,
256                            int id_len );
257
258extern "C"
259int rp_lib_remove(       int* handle,
260                            char* path,
261                            int path_len);
262
263extern "C"
264int rp_lib_xml_len(      int* handle);
265
266extern "C"
267void rp_lib_xml(         int* handle,
268                            char* retText,
269                            int retText_len);
270
271extern "C"
272int rp_lib_write_xml(     int* handle,
273                            char* outFile,
274                            int outFile_len);
275extern "C"
276void rp_quit();
277
278
279/**********************************************************/
280
281/**********************************************************/
282
283// private member functions
284int storeNode(PyObject* node);
285int objType( char* flavor);
286
287int storeObject(PyObject* objectName);
288PyObject* getObject(int objKey);
289char* null_terminate(char* inStr, int len);
290
291// global vars
292// dictionary to hold the python objects that
293// cannot be sent to fortran
294
295#define DICT_TEMPLATE <int,PyObject*>
296RpDict DICT_TEMPLATE fortObjDict;
297int rapptureStarted = 0;
298
299void rp_init() {
300    //
301    PyObject* rpClass = NULL;
302    int retVal = 0;
303   
304    // initialize the interpreter
305    Py_Initialize();
306    rpClass = importRappture();
307
308    if (rpClass) {
309        // store the handle in the dictionary
310        retVal = storeObject(rpClass);
311        rapptureStarted = 1;
312    }
313}
314
315int rp_lib(const char* filePath, int filePath_len)
316{
317    PyObject* lib = NULL;
318    PyObject* rpClass = NULL;
319    int handle = -1;
320    char* inFilePath = NULL;
321
322    if (!rapptureStarted) {
323        rp_init();
324    }
325
326    inFilePath = null_terminate((char*)filePath, filePath_len);
327   
328    // error checking to make sure inText is valid???
329   
330    // grab the rappture class from the dictionary
331    rpClass = getObject(0);
332
333    if (rpClass) {
334
335
336        // create a RapptureIO object and store in dictionary
337        lib = createRapptureObj(rpClass, inFilePath);
338
339        if (lib) {
340            handle = storeObject(lib);
341        }
342    }
343
344    if (inFilePath) {
345        free(inFilePath);
346        inFilePath = NULL;
347    }
348
349    return handle;
350}
351
352int rp_lib_element_obj(int* handle,     /* integer handle of library */
353                          char* path,     /* null terminated path */
354                          int path_len
355                        )
356{
357    int newObjHandle = -1;
358
359    const char* flavor = "object";
360    char* inPath = NULL;
361
362    PyObject* lib = NULL;
363    PyObject* retObj = NULL;
364
365    inPath = null_terminate(path,path_len);
366
367    // error checking to make sure inText is valid???
368
369    if (fortObjDict.size() > 0) {
370        if ((handle) && (*handle != 0)) {
371            lib = getObject(*handle);
372            if (lib) {
373                retObj = (PyObject*) rpElement(lib, inPath, flavor);
374               
375                if (retObj) {
376
377                    newObjHandle = storeObject(retObj);
378                    // Py_DECREF(retObj);
379                }
380            }
381        }
382    }
383
384    if (inPath) {
385        free(inPath);
386        inPath = NULL;
387    }
388
389    return newObjHandle;
390}
391
392void rp_lib_element_comp( int* handle, /* integer handle of library */
393                            char* path,      /* null terminated path */
394                            char* retText,   /* return buffer for fortran*/
395                            int path_len,
396                            int retText_len /* length of return text buffer */
397                          )
398{
399    char* inPath = NULL;
400
401    inPath = null_terminate(path,path_len);
402    if (path_len) {
403        path_len = strlen(inPath) + 1;
404    }
405    else {
406        path_len = 0;
407    }
408
409    // error checking to make sure inText is valid???
410
411    rp_lib_element_str(handle,inPath,"component",retText,path_len,10,retText_len);
412   
413    if (inPath) {
414        free(inPath);
415        inPath = NULL;
416    }
417
418}
419
420void rp_lib_element_id(   int* handle, /* integer handle of library */
421                            char* path,      /* null terminated path */
422                            char* retText,   /* return buffer for fortran*/
423                            int path_len,
424                            int retText_len /* length of return text buffer */
425                        )
426{
427    char* inPath = NULL;
428    inPath = null_terminate(path,path_len);
429    if (inPath) {
430        path_len = strlen(inPath) + 1;
431    }
432    else {
433        path_len = 0;
434    }
435
436    rp_lib_element_str(handle,inPath,"id",retText,path_len,3,retText_len);
437    if (inPath) {
438        free(inPath);
439        inPath = NULL;
440    }
441
442
443}
444
445void rp_lib_element_type( int* handle, /* integer handle of library */
446                            char* path,      /* null terminated path */
447                            char* retText,   /* return buffer for fortran*/
448                            int path_len,
449                            int retText_len /* length of return text buffer */
450                          )
451{
452    char* inPath = NULL;
453    inPath = null_terminate(path,path_len);
454    if (inPath) {
455        path_len = strlen(inPath) + 1;
456    }
457    else {
458        path_len = 0;
459    }
460    rp_lib_element_str(handle,path,"type",retText,path_len,5,retText_len);
461    if (inPath) {
462        free(inPath);
463        inPath = NULL;
464    }
465
466
467}
468
469void rp_lib_element_str(  int* handle, /* integer handle of library */
470                            char* path,      /* null terminated path */
471                            char* flavor,    /* null terminated flavor */
472                            char* retText,   /* return buffer for fortran*/
473                            int path_len,
474                            int flavor_len,
475                            int retText_len /* length of return text buffer */
476                        )
477{
478    int length_in = 0;
479    int length_out = 0;
480    int i = 0;
481    const char* xmlText = NULL;
482
483    PyObject* lib = NULL;
484   
485    char* retObj = NULL;
486   
487    char* inPath = NULL;
488    char* inFlavor = NULL;
489
490    inPath = null_terminate(path,path_len);
491    inFlavor = null_terminate(flavor,flavor_len);
492
493    if (rapptureStarted) {
494        if ((handle) && (*handle != 0)) {
495            lib = getObject(*handle);
496
497            if (lib) {
498                if(objType(inFlavor) == 1) {
499
500                    retObj = (char*) rpElement(lib, inPath, inFlavor);
501                    if (retObj) {
502                        xmlText = retObj;
503                   
504                        length_in = strlen(xmlText);
505                        length_out = retText_len;
506
507                        strncpy(retText, xmlText, length_out);
508
509                        // fortran-ify the string
510                        if (length_in < length_out) {
511                              for (i = length_in; i < length_out; i++) {
512                                  retText[i] = ' ';
513                              }
514                        }
515                        *(retText+length_out-1) = ' ';
516
517                    }
518                    else {
519                       
520                    }
521                }
522
523            }
524        }
525    }
526   
527    if (inPath) {
528        free(inPath);
529        inPath = NULL;
530    }
531
532    if (inFlavor) {
533        free(inFlavor);
534        inFlavor = NULL;
535    }
536
537}
538
539int rp_lib_child_num( int* handle,
540                         char* path,
541                         int path_len
542                       )
543{
544    int numChildren = 0;
545
546    PyObject* lib = NULL;
547    PyObject* list = NULL;
548    char* inPath = NULL;
549
550    inPath = null_terminate(path,path_len);
551   
552    if (rapptureStarted) {
553        if ((handle) && (*handle != 0)) {
554            lib = getObject(*handle);
555
556            if (lib) {
557                list = rpChildren_f(lib, inPath, "type");
558                if (list) {
559                    numChildren = PyList_Size(list);
560                    Py_DECREF(list);
561                }
562                else {
563                   
564                }
565            }
566        }
567    }
568
569    if (inPath) {
570        free(inPath);
571        inPath = NULL;
572    }
573
574    return numChildren;
575
576}
577
578
579int rp_lib_child_comp (   int* handle,    /* integer handle of library */
580                            char* path,     /* DOM path of requested object */
581                            char* type,     /* specific name of element */
582                            int* childNum,  /* child number for iteration */
583                            char* retText,  /* buffer to store return text */
584                            int path_len,   /* length of path */
585                            int type_len,   /* length of type */
586                            int retText_len /* length of return text buffer */
587                       )
588{
589    int retVal = 0;
590
591    char* inPath = NULL;
592    char* inType = NULL;
593
594    inPath = null_terminate(path,path_len);
595    inType = null_terminate(type,type_len);
596
597    if (inPath) {
598        path_len = strlen(inPath) + 1;
599    }
600    else {
601        path_len = 0;
602    }
603
604    if (inType) {
605        type_len = strlen(inType) + 1;
606    }
607    else {
608        type_len = 0;
609    }
610
611    retVal = rp_lib_child_str( handle,
612                                 inPath,
613                                 "component",
614                                 inType,
615                                 childNum,
616                                 retText,
617                                 path_len,
618                                 10,
619                                 type_len,
620                                 retText_len
621                               );
622
623    if (inPath) {
624        free(inPath);
625        inPath = NULL;
626    }
627
628    if (inType) {
629        free(inType);
630        inType = NULL;
631    }
632
633    return retVal;
634}
635
636int rp_lib_child_id(   int* handle,    /* integer handle of library */
637                          char* path,     /* DOM path of requested object */
638                          char* type,     /* specific name of element */
639                          int* childNum,  /* child number for iteration */
640                          char* retText,  /* buffer to store return text */
641                          int path_len,   /* length of path */
642                          int type_len,   /* length of type */
643                          int retText_len /* length of return text buffer */
644                       )
645{
646    int retVal = 0;
647    char* inPath = NULL;
648    char* inType = NULL;
649
650    inPath = null_terminate(path,path_len);
651    inType = null_terminate(type,type_len);
652
653    if (inPath) {
654        path_len = strlen(inPath) + 1;
655    }
656    else {
657        path_len = 0;
658    }
659
660    if (inType) {
661        type_len = strlen(inType) + 1;
662    }
663    else {
664        type_len = 0;
665    }
666
667    retVal = rp_lib_child_str( handle,
668                                 inPath,
669                                 "id",
670                                 inType,
671                                 childNum,
672                                 retText,
673                                 path_len,
674                                 3,
675                                 type_len,
676                                 retText_len
677                               );
678
679    if (inPath) {
680        free(inPath);
681        inPath = NULL;
682    }
683
684    if (inType) {
685        free(inType);
686        inType = NULL;
687    }
688
689    return retVal;
690}
691
692int rp_lib_child_type (   int* handle,    /* integer handle of library */
693                            char* path,     /* DOM path of requested object */
694                            char* type,     /* specific name of element */
695                            int* childNum,  /* child number for iteration */
696                            char* retText,  /* buffer to store return text */
697                            int path_len,   /* length of path */
698                            int type_len,   /* length of type */
699                            int retText_len /* length of return text buffer */
700                       )
701{
702    int retVal = 0;
703    char* inPath = NULL;
704    char* inType = NULL;
705
706    inPath = null_terminate(path,path_len);
707    inType = null_terminate(type,type_len);
708
709    if (inPath) {
710        path_len = strlen(inPath) + 1;
711    }
712    else {
713        path_len = 0;
714    }
715
716    if (inType) {
717        type_len = strlen(inType) + 1;
718    }
719    else {
720        type_len = 0;
721    }
722
723    retVal = rp_lib_child_str( handle,
724                                 inPath,
725                                 "type",
726                                 inType,
727                                 childNum,
728                                 retText,
729                                 path_len,
730                                 5,
731                                 type_len,
732                                 retText_len
733                               );
734
735    if (inPath) {
736        free(inPath);
737        inPath = NULL;
738    }
739
740    if (inType) {
741        free(inType);
742        inType = NULL;
743    }
744
745    return retVal;
746}
747
748int rp_lib_child_str (    int* handle,    /* integer handle of library */
749                            char* path,     /* DOM path of requested object */
750                            char* flavor,   /* information you want returned*/
751                            char* type,     /* specific name of element */
752                            int* childNum,  /* child number for iteration */
753                            char* retText,  /* buffer to store return text */
754                            int path_len,   /* length of path */
755                            int flavor_len, /* length of flavor */
756                            int type_len,   /* length of type */
757                            int retText_len /* length of return text buffer */
758                       )
759{
760    int retVal = 0;
761    int i = 0;
762    int length_in = 0;
763
764    PyObject* lib = NULL;
765    PyObject* list = NULL;
766    PyObject* list_item = NULL;
767   
768    char* xmlChild = NULL;
769    char* inPath = NULL;
770    char* inType = NULL;
771    char* inFlavor = NULL;
772   
773    inPath = null_terminate(path,path_len);
774    inFlavor = null_terminate(flavor,flavor_len);
775    inType = null_terminate(type,type_len);
776
777    if (rapptureStarted) {
778        if ((handle) && (*handle != 0)) {
779            lib = getObject(*handle);
780            if (lib) {
781                if(objType(inFlavor) == 1) {
782
783                    list = rpChildren_f(lib, inPath, inFlavor);
784                    if (list) {
785                        retVal = PyList_Size(list);
786                        // check to make sure our node index is within bounds
787                        if (((*childNum)-1) < retVal) {
788                            // get the requested node
789                            list_item = PyList_GetItem(list,(*childNum)-1);
790                            // make node string, borrowed object
791                            xmlChild = PyString_AsString(list_item);
792                            Py_DECREF(list);
793                            // printf("xmlChild = :%s:\n",xmlChild);
794                            if (xmlChild) {
795                                strncpy(retText, xmlChild, retText_len);
796                                length_in = strlen(xmlChild);
797
798                                // fortran-ify the string
799                                if (length_in < retText_len) {
800                                      for (i = length_in; i < retText_len; i++) {
801                                          retText[i] = ' ';
802                                      }
803                                }
804                                // *(retText+retText_len-1) = ' ';
805                            }
806                        }
807                    }
808                    else {
809                       
810                    }
811                }
812            }
813        }
814    }
815
816    if (inPath) {
817        free(inPath);
818        inPath = NULL;
819    }
820
821    if (inFlavor) {
822        free(inFlavor);
823        inFlavor = NULL;
824    }
825
826    if (inType) {
827        free(inType);
828        inType = NULL;
829    }
830
831
832    return retVal;
833}
834
835 int rp_lib_child_obj ( int* handle,
836                            char* path,
837                            char* type,
838                            int path_len,
839                            int type_len
840                          )
841{
842    int newObjHandle = -1;
843
844    PyObject* lib = NULL;
845    PyObject* list = NULL;
846   
847    char* inPath = NULL;
848    char* inType = NULL;
849
850    inPath = null_terminate(path,path_len);
851    inType = null_terminate(type,type_len);
852
853    if (rapptureStarted) {
854        if ((handle) && (*handle != 0)) {
855            lib = getObject(*handle);
856           
857            if (lib) {
858                list = rpChildren_f(lib, inPath, "object");
859                if (list) {
860                    // store the whole list,
861                    // need to make a way to read the nodes
862                    newObjHandle = storeObject(list);
863                    // Py_DECREF(list);
864                }
865                else {
866                   
867                }
868            }
869        }
870    }
871
872    if (inPath) {
873        free(inPath);
874        inPath = NULL;
875    }
876
877    if (inType) {
878        free(inType);
879        inType = NULL;
880    }
881
882    return newObjHandle;
883
884}
885
886
887void rp_lib_get( int* handle, /* integer handle of library */
888                   char* path,      /* null terminated path */
889                   char* retText,   /* return text buffer for fortran*/
890                   int path_len,
891                   int retText_len /* length of return text buffer */
892                 )
893{
894    int length_in = 0;
895    int length_out = 0;
896    int i = 0;
897    const char* xmlText = NULL;
898
899    PyObject* lib = NULL;
900   
901    char* inPath = NULL;
902
903    inPath = null_terminate(path,path_len);
904
905    if (rapptureStarted) {
906        if ((handle) && (*handle != 0)) {
907            lib = getObject(*handle);
908           
909            if (lib) {
910                // retObj is a borrowed object
911                // whose contents must not be modified
912                xmlText = rpGet(lib, inPath);
913               
914                if (xmlText) {
915
916                    length_in = strlen(xmlText);
917                    length_out = retText_len;
918
919                    strncpy(retText, xmlText, length_out);
920
921                    // fortran-ify the string
922                    if (length_in < length_out) {
923                          for (i = length_in; i < length_out; i++) {
924                              retText[i] = ' ';
925                          }
926                    }
927                    *(retText+length_out-1) = ' ';
928                }
929
930            }
931        }
932    }
933
934    if (inPath) {
935        free(inPath);
936        inPath = NULL;
937    }
938
939}
940
941double rp_lib_get_double( int* handle,   /* integer handle of library */
942                            char* path,    /* null terminated path */
943                            int path_len
944                          )
945{
946    double retVal = 0.0;
947    const char* xmlText = NULL;
948
949    PyObject* lib = NULL;
950   
951    char* inPath = NULL;
952
953    inPath = null_terminate(path,path_len);
954
955    path_len = strlen(inPath) + 1;
956
957    if (rapptureStarted) {
958        if ((handle) && (*handle != 0)) {
959            lib = getObject(*handle);
960           
961            if (lib) {
962                // retObj is a borrowed object
963                // whose contents must not be modified
964                xmlText = rpGet(lib, inPath);
965               
966                if (xmlText) {
967                    retVal = atof(xmlText);
968                }
969
970            }
971        }
972    }
973
974    if (inPath) {
975        free(inPath);
976        inPath = NULL;
977    }
978
979    return retVal;
980}
981
982
983void rp_lib_put_str( int* handle,
984                        char* path,
985                        char* value,
986                        int* append,
987                        int path_len,
988                        int value_len
989                      )
990{
991    char* inPath = NULL;
992    char* inValue = NULL;
993
994    inPath = null_terminate(path,path_len);
995    inValue = null_terminate(value,value_len);
996
997
998    if (inPath) {
999        path_len = strlen(inPath) + 1;
1000    }
1001    else {
1002        path_len = 0;
1003    }
1004
1005    if (inValue){
1006        value_len = strlen(inValue) + 1;
1007    }
1008    else {
1009        value_len = 0;
1010    }
1011
1012    rp_lib_put_id_str(handle,inPath,inValue,NULL,append,path_len,value_len,0);
1013
1014    if (inPath) {
1015        free(inPath);
1016        inPath = NULL;
1017    }
1018
1019    if (inValue) {
1020        free(inValue);
1021        inValue = NULL;
1022    }
1023}
1024
1025
1026void rp_lib_put_id_str ( int* handle,
1027                        char* path,
1028                        char* value,
1029                        char* id,
1030                        int* append,
1031                        int path_len,
1032                        int value_len,
1033                        int id_len
1034                      )
1035{
1036    PyObject* lib = NULL;
1037   
1038    char* inPath = NULL;
1039    char* inValue = NULL;
1040    char* inId = NULL;
1041
1042    inPath = null_terminate(path,path_len);
1043    inValue = null_terminate(value,value_len);
1044    inId = null_terminate(id,id_len);
1045
1046    if (rapptureStarted) {
1047        if ((handle) && (*handle != 0)) {
1048            lib = getObject(*handle);
1049           
1050            if (lib) {
1051                rpPut(lib, inPath, inValue, inId, *append);
1052            }
1053        }
1054    }
1055
1056    if (inPath) {
1057        free(inPath);
1058        inPath = NULL;
1059    }
1060
1061    if (inValue) {
1062        free(inValue);
1063        inValue = NULL;
1064    }
1065
1066    if (inId) {
1067        free(inId);
1068        inId = NULL;
1069    }
1070
1071}
1072
1073void rp_lib_put_obj( int* handle,
1074                        char* path,
1075                        int* valHandle,
1076                        int* append,
1077                        int path_len
1078                      )
1079{
1080    char* inPath = NULL;
1081
1082    inPath = null_terminate(path,path_len);
1083
1084    if (inPath) {
1085        path_len = strlen(inPath) + 1;
1086    }
1087
1088    rp_lib_put_id_obj(handle,inPath,valHandle,NULL,append,path_len,0);
1089
1090    if (inPath) {
1091        free(inPath);
1092        inPath = NULL;
1093    }
1094
1095}
1096
1097void rp_lib_put_id_obj ( int* handle,
1098                        char* path,
1099                        int* valHandle,
1100                        char* id,
1101                        int* append,
1102                        int path_len,
1103                        int id_len
1104                      )
1105{
1106    PyObject* lib = NULL;
1107    PyObject* value = NULL;
1108   
1109    char* inPath = NULL;
1110    char* inId = NULL;
1111
1112    inPath = null_terminate(path,path_len);
1113    inId = null_terminate(id,id_len);
1114
1115    if (rapptureStarted) {
1116        if ((handle) && (*handle != 0)) {
1117            lib = getObject(*handle);
1118            value = getObject(*valHandle);
1119           
1120            if (lib && value) {
1121                // retObj is a borrowed object
1122                // whose contents must not be modified
1123                rpPutObj(lib, inPath, value, inId, *append);
1124            }
1125        }
1126    }
1127
1128    if (inPath) {
1129        free(inPath);
1130        inPath = NULL;
1131    }
1132
1133    if (inId) {
1134        free(inId);
1135        inId = NULL;
1136    }
1137
1138}
1139
1140int rp_lib_remove (int* handle, char* path, int path_len)
1141{
1142    int newObjHandle = -1;
1143
1144    PyObject* lib = NULL;
1145    PyObject* removedObj = NULL;
1146   
1147    char* inPath = NULL;
1148
1149    inPath = null_terminate(path,path_len);
1150
1151    if (rapptureStarted) {
1152        if ((handle) && (*handle != 0)) {
1153            lib = getObject(*handle);
1154
1155            if (lib) {
1156                removedObj = rpRemove(lib, inPath);
1157               
1158                if (removedObj) {
1159                    newObjHandle = storeObject(removedObj);
1160                    // Py_DECREF(removedObj);
1161                }
1162
1163            }
1164        }
1165    }
1166   
1167    if (inPath) {
1168        free(inPath);
1169        inPath = NULL;
1170    }
1171
1172    return newObjHandle;
1173}
1174
1175int rp_lib_xml_len (int* handle)
1176{
1177    int length = -1;
1178    char* xmlText = NULL;
1179
1180    PyObject* lib = NULL;
1181
1182    if (rapptureStarted) {
1183        if ((handle) && (*handle != 0)) {
1184            lib = getObject(*handle);
1185
1186            if (lib) {
1187                // dont modify xmlText, borrowed pointer
1188                xmlText = rpXml(lib);
1189
1190                if (xmlText) {
1191                    length = strlen(xmlText) + 1;
1192                    free(xmlText);
1193                    // printf("len = :%d:\n",length);
1194                }
1195            }
1196        }
1197    }
1198    return length;
1199}
1200
1201void  rp_lib_xml(int* handle, char* retText, int retText_len)
1202{
1203    int length_in = 0;
1204    int length_out = 0;
1205    int i = 0;
1206    char* xmlText = NULL;
1207
1208    PyObject* lib = NULL;
1209   
1210    if (rapptureStarted) {
1211        if ((handle) && (*handle != 0)) {
1212            lib = getObject(*handle);
1213
1214            if (lib) {
1215                xmlText = rpXml(lib);
1216               
1217                if (xmlText) {
1218                    length_in = strlen(xmlText);
1219                    length_out = retText_len;
1220
1221                    strncpy(retText, xmlText, length_out);
1222
1223                    // fortran-ify the string
1224                    if (length_in < length_out) {
1225                          for (i = length_in; i < length_out; i++) {
1226                              retText[i] = ' ';
1227                          }
1228                    }
1229                    *(retText+length_out-1) = ' ';
1230
1231                    free(xmlText);
1232                }
1233               
1234            }
1235        }
1236    }
1237}
1238
1239int rp_lib_write_xml(int* handle, char* outFile, int outFile_len)
1240{
1241    int retVal = -1;
1242    PyObject* lib = NULL;
1243    FILE* outFH = NULL;
1244    char* inOutFile = NULL;
1245    char* xmlText = NULL;
1246
1247    inOutFile = null_terminate(outFile, outFile_len);
1248
1249    if (inOutFile && (inOutFile != "") ) {
1250        outFH = fopen(inOutFile, "w");
1251    }
1252
1253    if (outFH && rapptureStarted) {
1254        if ((handle) && (*handle != 0)) {
1255            lib = getObject(*handle);
1256
1257            if (lib) {
1258                xmlText = rpXml(lib);
1259                if (xmlText) {
1260                    retVal = fputs(xmlText,outFH);
1261                    free(xmlText);
1262                }
1263            }
1264        }
1265    }
1266
1267    if (outFH) {
1268        fclose(outFH);
1269        outFH = NULL;
1270    }
1271
1272    if (inOutFile) {
1273        free(inOutFile);
1274        inOutFile = NULL;
1275    }
1276
1277    return retVal;
1278}
1279
1280void rp_quit()
1281{
1282
1283    // clean up python's memory
1284    // shut down the python interpreter
1285    // clean up the dictionary
1286
1287    RpDictEntry DICT_TEMPLATE *hPtr;
1288    // RpDictIterator DICT_TEMPLATE iter((RpDict&)*this);                     
1289    RpDictIterator DICT_TEMPLATE iter(fortObjDict);
1290   
1291    hPtr = iter.first();                                                     
1292   
1293    while (hPtr) {
1294        Py_DECREF(*(hPtr->getValue()));
1295        hPtr->erase();
1296        hPtr = iter.next();                                                   
1297    }
1298
1299    if (fortObjDict.size()) {
1300        // probably want to change the warning sometime
1301        // printf("\nWARNING: internal dictionary is not empty..deleting\n");
1302    }
1303
1304    rapptureStarted = 0;
1305    // Py_Finalize();
1306}
1307
1308int objType( char* flavor)
1309{
1310    if (flavor == NULL) {
1311        // return a PyObject*
1312        return 0;
1313    }
1314    else if((*flavor == 'o')&&(strncmp(flavor,"object", 6)==0)){
1315        // return a PyObject*
1316        return 0;
1317    }
1318    else if (
1319      ((*flavor == 't')&&(strncmp(flavor,"type", 4) == 0))
1320    ||((*flavor == 'i')&&(strncmp(flavor,"id", 2) == 0))
1321    ||((*flavor == 'c')&&(strncmp(flavor,"component", 9) == 0)))
1322    {
1323        // return a char*
1324        // convert the result to c style strings
1325        return 1;
1326    }
1327    else {
1328        // unrecognized format
1329        return -1;
1330    }
1331}
1332
1333int storeObject(PyObject* objectName) {
1334
1335    int retVal = -1;
1336    int dictKey = fortObjDict.size();
1337    int newEntry = 0;
1338
1339    if (objectName) {
1340        // dictionary returns a reference to the inserted value
1341        // no error checking to make sure it was successful in entering
1342        // the new entry.
1343        fortObjDict.set(dictKey,objectName, &newEntry);
1344    }
1345   
1346    retVal = dictKey;
1347    return retVal;
1348}
1349
1350PyObject* getObject(int objKey) {
1351
1352    PyObject* retVal = *(fortObjDict.find(objKey).getValue());
1353
1354    if (retVal == *(fortObjDict.getNullEntry().getValue())) {
1355        retVal = NULL;
1356    }
1357
1358   return retVal;
1359
1360}
1361
1362/* fix buffer overflow possibility*/
1363char* null_terminate(char* inStr, int len)
1364{
1365    int retVal = 0;
1366    char* newStr = NULL;
1367    char* current = NULL;
1368
1369    if (inStr) {
1370
1371        current = inStr+len-1;
1372
1373        while ((len > 0) && (isspace(*(current)))) {
1374            // dont strip off newlines
1375
1376            if ( (*(current) == '\f')
1377              || (*(current) == '\n')
1378              || (*(current) == '\r')
1379              || (*(current) == '\t')
1380              || (*(current) == '\v') )
1381            {
1382                break;
1383            }
1384
1385            if (--len) {
1386                current--;
1387            }
1388        }
1389
1390        newStr = (char*) calloc(len+1,(sizeof(char)));
1391        strncpy(newStr,inStr,len);
1392        *(newStr+len) = '\0';
1393
1394        retVal++;
1395    }
1396
1397    // return retVal;
1398
1399    return newStr;
1400}
Note: See TracBrowser for help on using the repository browser.