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

Last change on this file since 829 was 115, checked in by mmc, 18 years ago

Updated all copyright notices.

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