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

Last change on this file since 73 was 66, checked in by mmc, 19 years ago

Cleaned up the app-fermi example so that it works the same way
across all of the various languages.

Added rpResult() function for reporting results. All programs
should all this to save out the XML before they exit. Also added
the fortran version rp_result().

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