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

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

initial submission of the Rappture Core components and language bindings

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