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

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

made a temporary fix to the fortran api which now sends the contents of the
xml file to the python rappture parser instead of the file name. before
with sending the filename, we were getting weird segfaults inside of python.
this new approach seems to solve the problem. maybe this change should be
moved to the c api and not the fortran api, because we require those using
the c api to send the contents of the file still, where the fortran api can
just send the file name.

dsk - 20050719

File size: 38.7 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    long int fileLength = 0;
323    char* fileContents = NULL;
324    FILE* fp = NULL;
325
326    if (!rapptureStarted) {
327        rp_init();
328    }
329
330
331    inFilePath = null_terminate((char*)filePath, filePath_len);
332   
333    // error checking to make sure inText is valid???
334   
335    // grab the rappture class from the dictionary
336    rpClass = getObject(0);
337
338    if (rpClass) {
339
340        fp = fopen(inFilePath,"rb");
341        if (fp) {
342            fseek(fp, 0, SEEK_END);
343            fileLength = ftell(fp);
344            rewind(fp);
345        }
346        fileContents = (char*) calloc(fileLength,sizeof(char));
347        fread((void*)fileContents,sizeof(char),fileLength,fp);
348
349        // create a RapptureIO object and store in dictionary
350        // lib = createRapptureObj(rpClass, inFilePath);
351        lib = createRapptureObj(rpClass, fileContents);
352
353        if (lib) {
354            handle = storeObject(lib);
355        }
356    }
357
358    if (inFilePath) {
359        free(inFilePath);
360        inFilePath = NULL;
361    }
362
363    return handle;
364}
365
366int rp_lib_element_obj(int* handle,     /* integer handle of library */
367                          char* path,     /* null terminated path */
368                          int path_len
369                        )
370{
371    int newObjHandle = -1;
372
373    const char* flavor = "object";
374    char* inPath = NULL;
375
376    PyObject* lib = NULL;
377    PyObject* retObj = NULL;
378
379    inPath = null_terminate(path,path_len);
380
381    // error checking to make sure inText is valid???
382
383    if (fortObjDict.size() > 0) {
384        if ((handle) && (*handle != 0)) {
385            lib = getObject(*handle);
386            if (lib) {
387                retObj = (PyObject*) rpElement(lib, inPath, flavor);
388               
389                if (retObj) {
390
391                    newObjHandle = storeObject(retObj);
392                    // Py_DECREF(retObj);
393                }
394            }
395        }
396    }
397
398    if (inPath) {
399        free(inPath);
400        inPath = NULL;
401    }
402
403    return newObjHandle;
404}
405
406void rp_lib_element_comp( int* handle, /* integer handle of library */
407                            char* path,      /* null terminated path */
408                            char* retText,   /* return buffer for fortran*/
409                            int path_len,
410                            int retText_len /* length of return text buffer */
411                          )
412{
413    char* inPath = NULL;
414
415    inPath = null_terminate(path,path_len);
416    if (path_len) {
417        path_len = strlen(inPath) + 1;
418    }
419    else {
420        path_len = 0;
421    }
422
423    // error checking to make sure inText is valid???
424
425    rp_lib_element_str(handle,inPath,"component",retText,path_len,10,retText_len);
426   
427    if (inPath) {
428        free(inPath);
429        inPath = NULL;
430    }
431
432}
433
434void rp_lib_element_id(   int* handle, /* integer handle of library */
435                            char* path,      /* null terminated path */
436                            char* retText,   /* return buffer for fortran*/
437                            int path_len,
438                            int retText_len /* length of return text buffer */
439                        )
440{
441    char* inPath = NULL;
442    inPath = null_terminate(path,path_len);
443    if (inPath) {
444        path_len = strlen(inPath) + 1;
445    }
446    else {
447        path_len = 0;
448    }
449
450    rp_lib_element_str(handle,inPath,"id",retText,path_len,3,retText_len);
451    if (inPath) {
452        free(inPath);
453        inPath = NULL;
454    }
455
456
457}
458
459void rp_lib_element_type( int* handle, /* integer handle of library */
460                            char* path,      /* null terminated path */
461                            char* retText,   /* return buffer for fortran*/
462                            int path_len,
463                            int retText_len /* length of return text buffer */
464                          )
465{
466    char* inPath = NULL;
467    inPath = null_terminate(path,path_len);
468    if (inPath) {
469        path_len = strlen(inPath) + 1;
470    }
471    else {
472        path_len = 0;
473    }
474    rp_lib_element_str(handle,path,"type",retText,path_len,5,retText_len);
475    if (inPath) {
476        free(inPath);
477        inPath = NULL;
478    }
479
480
481}
482
483void rp_lib_element_str(  int* handle, /* integer handle of library */
484                            char* path,      /* null terminated path */
485                            char* flavor,    /* null terminated flavor */
486                            char* retText,   /* return buffer for fortran*/
487                            int path_len,
488                            int flavor_len,
489                            int retText_len /* length of return text buffer */
490                        )
491{
492    int length_in = 0;
493    int length_out = 0;
494    int i = 0;
495    const char* xmlText = NULL;
496
497    PyObject* lib = NULL;
498   
499    char* retObj = NULL;
500   
501    char* inPath = NULL;
502    char* inFlavor = NULL;
503
504    inPath = null_terminate(path,path_len);
505    inFlavor = null_terminate(flavor,flavor_len);
506
507    if (rapptureStarted) {
508        if ((handle) && (*handle != 0)) {
509            lib = getObject(*handle);
510
511            if (lib) {
512                if(objType(inFlavor) == 1) {
513
514                    retObj = (char*) rpElement(lib, inPath, inFlavor);
515                    if (retObj) {
516                        xmlText = retObj;
517                   
518                        length_in = strlen(xmlText);
519                        length_out = retText_len;
520
521                        strncpy(retText, xmlText, length_out);
522
523                        // fortran-ify the string
524                        if (length_in < length_out) {
525                              for (i = length_in; i < length_out; i++) {
526                                  retText[i] = ' ';
527                              }
528                        }
529                        *(retText+length_out-1) = ' ';
530
531                    }
532                    else {
533                       
534                    }
535                }
536
537            }
538        }
539    }
540   
541    if (inPath) {
542        free(inPath);
543        inPath = NULL;
544    }
545
546    if (inFlavor) {
547        free(inFlavor);
548        inFlavor = NULL;
549    }
550
551}
552
553int rp_lib_child_num( int* handle,
554                         char* path,
555                         int path_len
556                       )
557{
558    int numChildren = 0;
559
560    PyObject* lib = NULL;
561    PyObject* list = NULL;
562    char* inPath = NULL;
563
564    inPath = null_terminate(path,path_len);
565   
566    if (rapptureStarted) {
567        if ((handle) && (*handle != 0)) {
568            lib = getObject(*handle);
569
570            if (lib) {
571                list = rpChildren_f(lib, inPath, "type");
572                if (list) {
573                    numChildren = PyList_Size(list);
574                    Py_DECREF(list);
575                }
576                else {
577                   
578                }
579            }
580        }
581    }
582
583    if (inPath) {
584        free(inPath);
585        inPath = NULL;
586    }
587
588    return numChildren;
589
590}
591
592
593int rp_lib_child_comp (   int* handle,    /* integer handle of library */
594                            char* path,     /* DOM path of requested object */
595                            char* type,     /* specific name of element */
596                            int* childNum,  /* child number for iteration */
597                            char* retText,  /* buffer to store return text */
598                            int path_len,   /* length of path */
599                            int type_len,   /* length of type */
600                            int retText_len /* length of return text buffer */
601                       )
602{
603    int retVal = 0;
604
605    char* inPath = NULL;
606    char* inType = NULL;
607
608    inPath = null_terminate(path,path_len);
609    inType = null_terminate(type,type_len);
610
611    if (inPath) {
612        path_len = strlen(inPath) + 1;
613    }
614    else {
615        path_len = 0;
616    }
617
618    if (inType) {
619        type_len = strlen(inType) + 1;
620    }
621    else {
622        type_len = 0;
623    }
624
625    retVal = rp_lib_child_str( handle,
626                                 inPath,
627                                 "component",
628                                 inType,
629                                 childNum,
630                                 retText,
631                                 path_len,
632                                 10,
633                                 type_len,
634                                 retText_len
635                               );
636
637    if (inPath) {
638        free(inPath);
639        inPath = NULL;
640    }
641
642    if (inType) {
643        free(inType);
644        inType = NULL;
645    }
646
647    return retVal;
648}
649
650int rp_lib_child_id(   int* handle,    /* integer handle of library */
651                          char* path,     /* DOM path of requested object */
652                          char* type,     /* specific name of element */
653                          int* childNum,  /* child number for iteration */
654                          char* retText,  /* buffer to store return text */
655                          int path_len,   /* length of path */
656                          int type_len,   /* length of type */
657                          int retText_len /* length of return text buffer */
658                       )
659{
660    int retVal = 0;
661    char* inPath = NULL;
662    char* inType = NULL;
663
664    inPath = null_terminate(path,path_len);
665    inType = null_terminate(type,type_len);
666
667    if (inPath) {
668        path_len = strlen(inPath) + 1;
669    }
670    else {
671        path_len = 0;
672    }
673
674    if (inType) {
675        type_len = strlen(inType) + 1;
676    }
677    else {
678        type_len = 0;
679    }
680
681    retVal = rp_lib_child_str( handle,
682                                 inPath,
683                                 "id",
684                                 inType,
685                                 childNum,
686                                 retText,
687                                 path_len,
688                                 3,
689                                 type_len,
690                                 retText_len
691                               );
692
693    if (inPath) {
694        free(inPath);
695        inPath = NULL;
696    }
697
698    if (inType) {
699        free(inType);
700        inType = NULL;
701    }
702
703    return retVal;
704}
705
706int rp_lib_child_type (   int* handle,    /* integer handle of library */
707                            char* path,     /* DOM path of requested object */
708                            char* type,     /* specific name of element */
709                            int* childNum,  /* child number for iteration */
710                            char* retText,  /* buffer to store return text */
711                            int path_len,   /* length of path */
712                            int type_len,   /* length of type */
713                            int retText_len /* length of return text buffer */
714                       )
715{
716    int retVal = 0;
717    char* inPath = NULL;
718    char* inType = NULL;
719
720    inPath = null_terminate(path,path_len);
721    inType = null_terminate(type,type_len);
722
723    if (inPath) {
724        path_len = strlen(inPath) + 1;
725    }
726    else {
727        path_len = 0;
728    }
729
730    if (inType) {
731        type_len = strlen(inType) + 1;
732    }
733    else {
734        type_len = 0;
735    }
736
737    retVal = rp_lib_child_str( handle,
738                                 inPath,
739                                 "type",
740                                 inType,
741                                 childNum,
742                                 retText,
743                                 path_len,
744                                 5,
745                                 type_len,
746                                 retText_len
747                               );
748
749    if (inPath) {
750        free(inPath);
751        inPath = NULL;
752    }
753
754    if (inType) {
755        free(inType);
756        inType = NULL;
757    }
758
759    return retVal;
760}
761
762int rp_lib_child_str (    int* handle,    /* integer handle of library */
763                            char* path,     /* DOM path of requested object */
764                            char* flavor,   /* information you want returned*/
765                            char* type,     /* specific name of element */
766                            int* childNum,  /* child number for iteration */
767                            char* retText,  /* buffer to store return text */
768                            int path_len,   /* length of path */
769                            int flavor_len, /* length of flavor */
770                            int type_len,   /* length of type */
771                            int retText_len /* length of return text buffer */
772                       )
773{
774    int retVal = 0;
775    int i = 0;
776    int length_in = 0;
777
778    PyObject* lib = NULL;
779    PyObject* list = NULL;
780    PyObject* list_item = NULL;
781   
782    char* xmlChild = NULL;
783    char* inPath = NULL;
784    char* inType = NULL;
785    char* inFlavor = NULL;
786   
787    inPath = null_terminate(path,path_len);
788    inFlavor = null_terminate(flavor,flavor_len);
789    inType = null_terminate(type,type_len);
790
791    if (rapptureStarted) {
792        if ((handle) && (*handle != 0)) {
793            lib = getObject(*handle);
794            if (lib) {
795                if(objType(inFlavor) == 1) {
796
797                    list = rpChildren_f(lib, inPath, inFlavor);
798                    if (list) {
799                        retVal = PyList_Size(list);
800                        // check to make sure our node index is within bounds
801                        if (((*childNum)-1) < retVal) {
802                            // get the requested node
803                            list_item = PyList_GetItem(list,(*childNum)-1);
804                            // make node string, borrowed object
805                            xmlChild = PyString_AsString(list_item);
806                            Py_DECREF(list);
807                            // printf("xmlChild = :%s:\n",xmlChild);
808                            if (xmlChild) {
809                                strncpy(retText, xmlChild, retText_len);
810                                length_in = strlen(xmlChild);
811
812                                // fortran-ify the string
813                                if (length_in < retText_len) {
814                                      for (i = length_in; i < retText_len; i++) {
815                                          retText[i] = ' ';
816                                      }
817                                }
818                                // *(retText+retText_len-1) = ' ';
819                            }
820                        }
821                    }
822                    else {
823                       
824                    }
825                }
826            }
827        }
828    }
829
830    if (inPath) {
831        free(inPath);
832        inPath = NULL;
833    }
834
835    if (inFlavor) {
836        free(inFlavor);
837        inFlavor = NULL;
838    }
839
840    if (inType) {
841        free(inType);
842        inType = NULL;
843    }
844
845
846    return retVal;
847}
848
849 int rp_lib_child_obj ( int* handle,
850                            char* path,
851                            char* type,
852                            int path_len,
853                            int type_len
854                          )
855{
856    int newObjHandle = -1;
857
858    PyObject* lib = NULL;
859    PyObject* list = NULL;
860   
861    char* inPath = NULL;
862    char* inType = NULL;
863
864    inPath = null_terminate(path,path_len);
865    inType = null_terminate(type,type_len);
866
867    if (rapptureStarted) {
868        if ((handle) && (*handle != 0)) {
869            lib = getObject(*handle);
870           
871            if (lib) {
872                list = rpChildren_f(lib, inPath, "object");
873                if (list) {
874                    // store the whole list,
875                    // need to make a way to read the nodes
876                    newObjHandle = storeObject(list);
877                    // Py_DECREF(list);
878                }
879                else {
880                   
881                }
882            }
883        }
884    }
885
886    if (inPath) {
887        free(inPath);
888        inPath = NULL;
889    }
890
891    if (inType) {
892        free(inType);
893        inType = NULL;
894    }
895
896    return newObjHandle;
897
898}
899
900
901void rp_lib_get( int* handle, /* integer handle of library */
902                   char* path,      /* null terminated path */
903                   char* retText,   /* return text buffer for fortran*/
904                   int path_len,
905                   int retText_len /* length of return text buffer */
906                 )
907{
908    int length_in = 0;
909    int length_out = 0;
910    int i = 0;
911    const char* xmlText = NULL;
912
913    PyObject* lib = NULL;
914   
915    char* inPath = NULL;
916
917    inPath = null_terminate(path,path_len);
918
919    if (rapptureStarted) {
920        if ((handle) && (*handle != 0)) {
921            lib = getObject(*handle);
922           
923            if (lib) {
924                // retObj is a borrowed object
925                // whose contents must not be modified
926                xmlText = rpGet(lib, inPath);
927               
928                if (xmlText) {
929
930                    length_in = strlen(xmlText);
931                    length_out = retText_len;
932
933                    strncpy(retText, xmlText, length_out);
934
935                    // fortran-ify the string
936                    if (length_in < length_out) {
937                          for (i = length_in; i < length_out; i++) {
938                              retText[i] = ' ';
939                          }
940                    }
941                    *(retText+length_out-1) = ' ';
942                }
943
944            }
945        }
946    }
947
948    if (inPath) {
949        free(inPath);
950        inPath = NULL;
951    }
952
953}
954
955double rp_lib_get_double( int* handle,   /* integer handle of library */
956                            char* path,    /* null terminated path */
957                            int path_len
958                          )
959{
960    double retVal = 0.0;
961    const char* xmlText = NULL;
962
963    PyObject* lib = NULL;
964   
965    char* inPath = NULL;
966
967    inPath = null_terminate(path,path_len);
968
969    path_len = strlen(inPath) + 1;
970
971    if (rapptureStarted) {
972        if ((handle) && (*handle != 0)) {
973            lib = getObject(*handle);
974           
975            if (lib) {
976                // retObj is a borrowed object
977                // whose contents must not be modified
978                xmlText = rpGet(lib, inPath);
979               
980                if (xmlText) {
981                    retVal = atof(xmlText);
982                }
983
984            }
985        }
986    }
987
988    if (inPath) {
989        free(inPath);
990        inPath = NULL;
991    }
992
993    return retVal;
994}
995
996
997void rp_lib_put_str( int* handle,
998                        char* path,
999                        char* value,
1000                        int* append,
1001                        int path_len,
1002                        int value_len
1003                      )
1004{
1005    char* inPath = NULL;
1006    char* inValue = NULL;
1007
1008    inPath = null_terminate(path,path_len);
1009    inValue = null_terminate(value,value_len);
1010
1011
1012    if (inPath) {
1013        path_len = strlen(inPath) + 1;
1014    }
1015    else {
1016        path_len = 0;
1017    }
1018
1019    if (inValue){
1020        value_len = strlen(inValue) + 1;
1021    }
1022    else {
1023        value_len = 0;
1024    }
1025
1026    rp_lib_put_id_str(handle,inPath,inValue,NULL,append,path_len,value_len,0);
1027
1028    if (inPath) {
1029        free(inPath);
1030        inPath = NULL;
1031    }
1032
1033    if (inValue) {
1034        free(inValue);
1035        inValue = NULL;
1036    }
1037}
1038
1039
1040void rp_lib_put_id_str ( int* handle,
1041                        char* path,
1042                        char* value,
1043                        char* id,
1044                        int* append,
1045                        int path_len,
1046                        int value_len,
1047                        int id_len
1048                      )
1049{
1050    PyObject* lib = NULL;
1051   
1052    char* inPath = NULL;
1053    char* inValue = NULL;
1054    char* inId = NULL;
1055
1056    inPath = null_terminate(path,path_len);
1057    inValue = null_terminate(value,value_len);
1058    inId = null_terminate(id,id_len);
1059
1060    if (rapptureStarted) {
1061        if ((handle) && (*handle != 0)) {
1062            lib = getObject(*handle);
1063           
1064            if (lib) {
1065                rpPut(lib, inPath, inValue, inId, *append);
1066            }
1067        }
1068    }
1069
1070    if (inPath) {
1071        free(inPath);
1072        inPath = NULL;
1073    }
1074
1075    if (inValue) {
1076        free(inValue);
1077        inValue = NULL;
1078    }
1079
1080    if (inId) {
1081        free(inId);
1082        inId = NULL;
1083    }
1084
1085}
1086
1087void rp_lib_put_obj( int* handle,
1088                        char* path,
1089                        int* valHandle,
1090                        int* append,
1091                        int path_len
1092                      )
1093{
1094    char* inPath = NULL;
1095
1096    inPath = null_terminate(path,path_len);
1097
1098    if (inPath) {
1099        path_len = strlen(inPath) + 1;
1100    }
1101
1102    rp_lib_put_id_obj(handle,inPath,valHandle,NULL,append,path_len,0);
1103
1104    if (inPath) {
1105        free(inPath);
1106        inPath = NULL;
1107    }
1108
1109}
1110
1111void rp_lib_put_id_obj ( int* handle,
1112                        char* path,
1113                        int* valHandle,
1114                        char* id,
1115                        int* append,
1116                        int path_len,
1117                        int id_len
1118                      )
1119{
1120    PyObject* lib = NULL;
1121    PyObject* value = NULL;
1122   
1123    char* inPath = NULL;
1124    char* inId = NULL;
1125
1126    inPath = null_terminate(path,path_len);
1127    inId = null_terminate(id,id_len);
1128
1129    if (rapptureStarted) {
1130        if ((handle) && (*handle != 0)) {
1131            lib = getObject(*handle);
1132            value = getObject(*valHandle);
1133           
1134            if (lib && value) {
1135                // retObj is a borrowed object
1136                // whose contents must not be modified
1137                rpPutObj(lib, inPath, value, inId, *append);
1138            }
1139        }
1140    }
1141
1142    if (inPath) {
1143        free(inPath);
1144        inPath = NULL;
1145    }
1146
1147    if (inId) {
1148        free(inId);
1149        inId = NULL;
1150    }
1151
1152}
1153
1154int rp_lib_remove (int* handle, char* path, int path_len)
1155{
1156    int newObjHandle = -1;
1157
1158    PyObject* lib = NULL;
1159    PyObject* removedObj = NULL;
1160   
1161    char* inPath = NULL;
1162
1163    inPath = null_terminate(path,path_len);
1164
1165    if (rapptureStarted) {
1166        if ((handle) && (*handle != 0)) {
1167            lib = getObject(*handle);
1168
1169            if (lib) {
1170                removedObj = rpRemove(lib, inPath);
1171               
1172                if (removedObj) {
1173                    newObjHandle = storeObject(removedObj);
1174                    // Py_DECREF(removedObj);
1175                }
1176
1177            }
1178        }
1179    }
1180   
1181    if (inPath) {
1182        free(inPath);
1183        inPath = NULL;
1184    }
1185
1186    return newObjHandle;
1187}
1188
1189int rp_lib_xml_len (int* handle)
1190{
1191    int length = -1;
1192    const char* xmlText = NULL;
1193
1194    PyObject* lib = NULL;
1195
1196    if (rapptureStarted) {
1197        if ((handle) && (*handle != 0)) {
1198            lib = getObject(*handle);
1199
1200            if (lib) {
1201                // dont modify xmlText, borrowed pointer
1202                xmlText = rpXml(lib);
1203
1204                if (xmlText) {
1205                    length = strlen(xmlText) + 1;
1206                    // printf("len = :%d:\n",length);
1207                }
1208            }
1209        }
1210    }
1211    return length;
1212}
1213
1214void  rp_lib_xml(int* handle, char* retText, int retText_len)
1215{
1216    int length_in = 0;
1217    int length_out = 0;
1218    int i = 0;
1219    const char* xmlText = NULL;
1220
1221    PyObject* lib = NULL;
1222   
1223    if (rapptureStarted) {
1224        if ((handle) && (*handle != 0)) {
1225            lib = getObject(*handle);
1226
1227            if (lib) {
1228                xmlText = rpXml(lib);
1229               
1230                length_in = strlen(xmlText);
1231                length_out = retText_len;
1232
1233                strncpy(retText, xmlText, length_out);
1234
1235                // fortran-ify the string
1236                if (length_in < length_out) {
1237                      for (i = length_in; i < length_out; i++) {
1238                          retText[i] = ' ';
1239                      }
1240                }
1241                *(retText+length_out-1) = ' ';
1242            }
1243        }
1244    }
1245}
1246
1247int rp_lib_write_xml(int* handle, char* outFile, int outFile_len)
1248{
1249    int retVal = 0;
1250    PyObject* lib = NULL;
1251    FILE* outFH = NULL;
1252    char* inOutFile = NULL;
1253    const char* xmlText = NULL;
1254
1255    inOutFile = null_terminate(outFile, outFile_len);
1256
1257    if (inOutFile && (inOutFile != "") ) {
1258        outFH = fopen(inOutFile, "w");
1259    }
1260
1261    if (outFH && rapptureStarted) {
1262        if ((handle) && (*handle != 0)) {
1263            lib = getObject(*handle);
1264
1265            if (lib) {
1266                xmlText = rpXml(lib);
1267                retVal = fputs(xmlText,outFH);
1268            }
1269        }
1270    }
1271
1272    if (outFH) {
1273        fclose(outFH);
1274        outFH = NULL;
1275    }
1276
1277    if (inOutFile) {
1278        free(inOutFile);
1279        inOutFile = NULL;
1280    }
1281
1282    return retVal;
1283}
1284
1285void rp_quit()
1286{
1287
1288    // clean up python's memory
1289    // shut down the python interpreter
1290    // clean up the dictionary
1291
1292    RpDictEntry DICT_TEMPLATE *hPtr;
1293    // RpDictIterator DICT_TEMPLATE iter((RpDict&)*this);                     
1294    RpDictIterator DICT_TEMPLATE iter(fortObjDict);
1295   
1296    hPtr = iter.first();                                                     
1297   
1298    while (hPtr) {
1299        Py_DECREF(*(hPtr->getValue()));
1300        hPtr->erase();
1301        hPtr = iter.next();                                                   
1302    }
1303
1304    if (fortObjDict.size()) {
1305        // probably want to change the warning sometime
1306        // printf("\nWARNING: internal dictionary is not empty..deleting\n");
1307    }
1308
1309    rapptureStarted = 0;
1310    // Py_Finalize();
1311}
1312
1313int objType( char* flavor)
1314{
1315    if (flavor == NULL) {
1316        // return a PyObject*
1317        return 0;
1318    }
1319    else if((*flavor == 'o')&&(strncmp(flavor,"object", 6)==0)){
1320        // return a PyObject*
1321        return 0;
1322    }
1323    else if (
1324      ((*flavor == 't')&&(strncmp(flavor,"type", 4) == 0))
1325    ||((*flavor == 'i')&&(strncmp(flavor,"id", 2) == 0))
1326    ||((*flavor == 'c')&&(strncmp(flavor,"component", 9) == 0)))
1327    {
1328        // return a char*
1329        // convert the result to c style strings
1330        return 1;
1331    }
1332    else {
1333        // unrecognized format
1334        return -1;
1335    }
1336}
1337
1338int storeObject(PyObject* objectName) {
1339
1340    int retVal = -1;
1341    int dictKey = fortObjDict.size();
1342    int newEntry = 0;
1343
1344    if (objectName) {
1345        // dictionary returns a reference to the inserted value
1346        // no error checking to make sure it was successful in entering
1347        // the new entry.
1348        fortObjDict.set(dictKey,objectName, &newEntry);
1349    }
1350   
1351    retVal = dictKey;
1352    return retVal;
1353}
1354
1355PyObject* getObject(int objKey) {
1356
1357    PyObject* retVal = *(fortObjDict.find(objKey).getValue());
1358
1359    if (retVal == *(fortObjDict.getNullEntry().getValue())) {
1360        retVal = NULL;
1361    }
1362
1363   return retVal;
1364
1365}
1366
1367/* fix buffer overflow possibility*/
1368char* null_terminate(char* inStr, int len)
1369{
1370    int retVal = 0;
1371    char* newStr = NULL;
1372    char* current = NULL;
1373
1374    if (inStr) {
1375
1376        current = inStr+len-1;
1377
1378        while ((len > 0) && (isspace(*(current)))) {
1379            // dont strip off newlines
1380
1381            if ( (*(current) == '\f')
1382              || (*(current) == '\n')
1383              || (*(current) == '\r')
1384              || (*(current) == '\t')
1385              || (*(current) == '\v') )
1386            {
1387                break;
1388            }
1389
1390            if (--len) {
1391                current--;
1392            }
1393        }
1394
1395        newStr = (char*) calloc(len+1,(sizeof(char)));
1396        strncpy(newStr,inStr,len);
1397        *(newStr+len) = '\0';
1398
1399        retVal++;
1400    }
1401
1402    // return retVal;
1403
1404    return newStr;
1405}
Note: See TracBrowser for help on using the repository browser.