source: branches/1.7/src/core/rappture_fortran.c @ 6716

Last change on this file since 6716 was 5850, checked in by gah, 9 years ago

merge accumulative changes from 1.3 branch into uq branch

  • Property svn:eol-style set to native
File size: 37.1 KB
Line 
1/*
2 * ======================================================================
3 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
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
224extern "C"
225void rp_lib_get(int* handle,
226                char* path,
227                char* retText,
228                int path_len,
229                int retText_len);
230
231extern "C"
232double rp_lib_get_double(int* handle,
233                         char* path,
234                         int path_len);
235
236extern "C"
237void rp_lib_put_str(int* handle,
238                    char* path,
239                    char* value,
240                    int* append,
241                    int path_len,
242                    int value_len);
243
244extern "C"
245void rp_lib_put_id_str(int* handle,
246                       char* path,
247                       char* value,
248                       char* id,
249                       int* append,
250                       int path_len,
251                       int value_len,
252                       int id_len);
253
254extern "C"
255void rp_lib_put_obj(int* handle,
256                    char* path,
257                    int* valHandle,
258                    int* append,
259                    int path_len);
260
261extern "C"
262void rp_lib_put_id_obj(int* handle,
263                       char* path,
264                       int* valHandle,
265                       char* id,
266                       int* append,
267                       int path_len,
268                       int id_len);
269
270extern "C"
271int rp_lib_remove(int* handle,
272                  char* path,
273                  int path_len);
274
275extern "C"
276int rp_lib_xml_len(int* handle);
277
278extern "C"
279void rp_lib_xml(int* handle,
280                char* retText,
281                int retText_len);
282
283extern "C"
284int rp_lib_write_xml(int* handle,
285                     char* outFile,
286                     int outFile_len);
287
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    //
318    PyObject* rpClass = NULL;
319    int retVal = 0;
320
321    // initialize the interpreter
322    Py_Initialize();
323    rpClass = importRappture();
324
325    if (rpClass) {
326        // store the handle in the dictionary
327        retVal = storeObject(rpClass);
328        rapptureStarted = 1;
329    }
330}
331
332int rp_lib(const char* filePath, int filePath_len)
333{
334    PyObject* lib = NULL;
335    PyObject* rpClass = NULL;
336    int handle = -1;
337    char* inFilePath = NULL;
338
339    if (!rapptureStarted) {
340        rp_init();
341    }
342
343    inFilePath = null_terminate((char*)filePath, filePath_len);
344
345    // error checking to make sure inText is valid???
346
347    // grab the rappture class from the dictionary
348    rpClass = getObject(0);
349
350    if (rpClass) {
351
352
353        // create a RapptureIO object and store in dictionary
354        lib = createRapptureObj(rpClass, inFilePath);
355
356        if (lib) {
357            handle = storeObject(lib);
358        }
359    }
360
361    if (inFilePath) {
362        free(inFilePath);
363        inFilePath = NULL;
364    }
365
366    return handle;
367}
368
369int rp_lib_element_obj(int* handle,     /* integer handle of library */
370                       char* path,     /* null terminated path */
371                       int path_len)
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    int numChildren = 0;
566
567    PyObject* lib = NULL;
568    PyObject* list = NULL;
569    char* inPath = NULL;
570
571    inPath = null_terminate(path,path_len);
572
573    if (rapptureStarted) {
574        if ((handle) && (*handle != 0)) {
575            lib = getObject(*handle);
576
577            if (lib) {
578                list = rpChildren_f(lib, inPath, "type");
579                if (list) {
580                    numChildren = PyList_Size(list);
581                    Py_DECREF(list);
582                }
583                else {
584
585                }
586            }
587        }
588    }
589
590    if (inPath) {
591        free(inPath);
592        inPath = NULL;
593    }
594
595    return numChildren;
596
597}
598
599
600int rp_lib_child_comp(int* handle,    /* integer handle of library */
601                      char* path,     /* DOM path of requested object */
602                      char* type,     /* specific name of element */
603                      int* childNum,  /* child number for iteration */
604                      char* retText,  /* buffer to store return text */
605                      int path_len,   /* length of path */
606                      int type_len,   /* length of type */
607                      int retText_len /* length of return text buffer */
608                      )
609{
610    int retVal = 0;
611
612    char* inPath = NULL;
613    char* inType = NULL;
614
615    inPath = null_terminate(path,path_len);
616    inType = null_terminate(type,type_len);
617
618    if (inPath) {
619        path_len = strlen(inPath) + 1;
620    }
621    else {
622        path_len = 0;
623    }
624
625    if (inType) {
626        type_len = strlen(inType) + 1;
627    }
628    else {
629        type_len = 0;
630    }
631
632    retVal = rp_lib_child_str(handle,
633                              inPath,
634                              "component",
635                              inType,
636                              childNum,
637                              retText,
638                              path_len,
639                              10,
640                              type_len,
641                              retText_len);
642
643    if (inPath) {
644        free(inPath);
645        inPath = NULL;
646    }
647
648    if (inType) {
649        free(inType);
650        inType = NULL;
651    }
652
653    return retVal;
654}
655
656int rp_lib_child_id(int* handle,    /* integer handle of library */
657                    char* path,     /* DOM path of requested object */
658                    char* type,     /* specific name of element */
659                    int* childNum,  /* child number for iteration */
660                    char* retText,  /* buffer to store return text */
661                    int path_len,   /* length of path */
662                    int type_len,   /* length of type */
663                    int retText_len /* length of return text buffer */
664                    )
665{
666    int retVal = 0;
667    char* inPath = NULL;
668    char* inType = NULL;
669
670    inPath = null_terminate(path,path_len);
671    inType = null_terminate(type,type_len);
672
673    if (inPath) {
674        path_len = strlen(inPath) + 1;
675    }
676    else {
677        path_len = 0;
678    }
679
680    if (inType) {
681        type_len = strlen(inType) + 1;
682    }
683    else {
684        type_len = 0;
685    }
686
687    retVal = rp_lib_child_str(handle,
688                              inPath,
689                              "id",
690                              inType,
691                              childNum,
692                              retText,
693                              path_len,
694                              3,
695                              type_len,
696                              retText_len);
697
698    if (inPath) {
699        free(inPath);
700        inPath = NULL;
701    }
702
703    if (inType) {
704        free(inType);
705        inType = NULL;
706    }
707
708    return retVal;
709}
710
711int rp_lib_child_type(int* handle,    /* integer handle of library */
712                      char* path,     /* DOM path of requested object */
713                      char* type,     /* specific name of element */
714                      int* childNum,  /* child number for iteration */
715                      char* retText,  /* buffer to store return text */
716                      int path_len,   /* length of path */
717                      int type_len,   /* length of type */
718                      int retText_len /* length of return text buffer */
719                      )
720{
721    int retVal = 0;
722    char* inPath = NULL;
723    char* inType = NULL;
724
725    inPath = null_terminate(path,path_len);
726    inType = null_terminate(type,type_len);
727
728    if (inPath) {
729        path_len = strlen(inPath) + 1;
730    }
731    else {
732        path_len = 0;
733    }
734
735    if (inType) {
736        type_len = strlen(inType) + 1;
737    }
738    else {
739        type_len = 0;
740    }
741
742    retVal = rp_lib_child_str(handle,
743                              inPath,
744                              "type",
745                              inType,
746                              childNum,
747                              retText,
748                              path_len,
749                              5,
750                              type_len,
751                              retText_len);
752
753    if (inPath) {
754        free(inPath);
755        inPath = NULL;
756    }
757
758    if (inType) {
759        free(inType);
760        inType = NULL;
761    }
762
763    return retVal;
764}
765
766int rp_lib_child_str(int* handle,    /* integer handle of library */
767                     char* path,     /* DOM path of requested object */
768                     char* flavor,   /* information you want returned*/
769                     char* type,     /* specific name of element */
770                     int* childNum,  /* child number for iteration */
771                     char* retText,  /* buffer to store return text */
772                     int path_len,   /* length of path */
773                     int flavor_len, /* length of flavor */
774                     int type_len,   /* length of type */
775                     int retText_len /* length of return text buffer */
776                     )
777{
778    int retVal = 0;
779    // int i = 0;
780    // int length_in = 0;
781
782    PyObject* lib = NULL;
783    PyObject* list = NULL;
784    PyObject* list_item = NULL;
785
786    char* xmlChild = NULL;
787    char* inPath = NULL;
788    char* inType = NULL;
789    char* inFlavor = NULL;
790
791    inPath = null_terminate(path,path_len);
792    inFlavor = null_terminate(flavor,flavor_len);
793    inType = null_terminate(type,type_len);
794
795    if (rapptureStarted) {
796        if ((handle) && (*handle != 0)) {
797            lib = getObject(*handle);
798            if (lib) {
799                if (objType(inFlavor) == 1) {
800
801                    list = rpChildren_f(lib, inPath, inFlavor);
802                    if (list) {
803                        retVal = PyList_Size(list);
804                        // check to make sure our node index is within bounds
805                        if (((*childNum)-1) < retVal) {
806                            // get the requested node
807                            list_item = PyList_GetItem(list,(*childNum)-1);
808                            // make node string, borrowed object
809                            xmlChild = PyString_AsString(list_item);
810                            Py_DECREF(list);
811                            // printf("xmlChild = :%s:\n",xmlChild);
812                            if (xmlChild) {
813                                fortranify(xmlChild, retText, retText_len);
814
815                                /*
816                                strncpy(retText, xmlChild, retText_len);
817                                length_in = strlen(xmlChild);
818
819                                // fortran-ify the string
820                                if (length_in < retText_len) {
821                                      for (i = length_in; i < retText_len; i++) {
822                                          retText[i] = ' ';
823                                      }
824                                }
825                                // *(retText+retText_len-1) = ' ';
826                                */
827                            }
828                        }
829                    }
830                }
831            }
832        }
833    }
834
835    if (inPath) {
836        free(inPath);
837        inPath = NULL;
838    }
839
840    if (inFlavor) {
841        free(inFlavor);
842        inFlavor = NULL;
843    }
844
845    if (inType) {
846        free(inType);
847        inType = NULL;
848    }
849
850    return retVal;
851}
852
853int rp_lib_child_obj(int* handle,
854                     char* path,
855                     char* type,
856                     int path_len,
857                     int type_len)
858{
859    int newObjHandle = -1;
860
861    PyObject* lib = NULL;
862    PyObject* list = NULL;
863
864    char* inPath = NULL;
865    char* inType = NULL;
866
867    inPath = null_terminate(path,path_len);
868    inType = null_terminate(type,type_len);
869
870    if (rapptureStarted) {
871        if ((handle) && (*handle != 0)) {
872            lib = getObject(*handle);
873
874            if (lib) {
875                list = rpChildren_f(lib, inPath, "object");
876                if (list) {
877                    // store the whole list,
878                    // need to make a way to read the nodes
879                    newObjHandle = storeObject(list);
880                    // Py_DECREF(list);
881                }
882            }
883        }
884    }
885
886    if (inPath) {
887        free(inPath);
888        inPath = NULL;
889    }
890
891    if (inType) {
892        free(inType);
893        inType = NULL;
894    }
895
896    return newObjHandle;
897}
898
899
900void rp_lib_get(int* handle, /* integer handle of library */
901                char* path,      /* null terminated path */
902                char* retText,   /* return text buffer for fortran*/
903                int path_len,
904                int retText_len /* length of return text buffer */
905                )
906{
907    // int length_in = 0;
908    // int length_out = 0;
909    // int i = 0;
910    const char* xmlText = NULL;
911
912    PyObject* lib = NULL;
913
914    char* inPath = NULL;
915
916    inPath = null_terminate(path,path_len);
917
918    if (rapptureStarted) {
919        if ((handle) && (*handle != 0)) {
920            lib = getObject(*handle);
921
922            if (lib) {
923                // retObj is a borrowed object
924                // whose contents must not be modified
925                xmlText = rpGet(lib, inPath);
926
927                if (xmlText) {
928
929                    fortranify(xmlText, retText, retText_len);
930
931                    /*
932                    length_in = strlen(xmlText);
933                    length_out = retText_len;
934
935                    strncpy(retText, xmlText, length_out);
936
937                    // fortran-ify the string
938                    if (length_in < length_out) {
939                          for (i = length_in; i < length_out; i++) {
940                              retText[i] = ' ';
941                          }
942                    }
943                    *(retText+length_out-1) = ' ';
944                    */
945                }
946            }
947        }
948    }
949
950    if (inPath) {
951        free(inPath);
952        inPath = NULL;
953    }
954}
955
956double rp_lib_get_double(int* handle,   /* integer handle of library */
957                         char* path,    /* null terminated path */
958                         int path_len)
959{
960    double retVal = 0.0;
961    const char* xmlText = NULL;
962
963    PyObject* lib = NULL;
964
965    char* inPath = NULL;
966
967    inPath = null_terminate(path,path_len);
968
969    path_len = strlen(inPath) + 1;
970
971    if (rapptureStarted) {
972        if ((handle) && (*handle != 0)) {
973            lib = getObject(*handle);
974
975            if (lib) {
976                // retObj is a borrowed object
977                // whose contents must not be modified
978                xmlText = rpGet(lib, inPath);
979
980                if (xmlText) {
981                    retVal = atof(xmlText);
982                }
983            }
984        }
985    }
986
987    if (inPath) {
988        free(inPath);
989        inPath = NULL;
990    }
991
992    return retVal;
993}
994
995
996void rp_lib_put_str(int* handle,
997                    char* path,
998                    char* value,
999                    int* append,
1000                    int path_len,
1001                    int value_len)
1002{
1003    char* inPath = NULL;
1004    char* inValue = NULL;
1005
1006    inPath = null_terminate(path,path_len);
1007    inValue = null_terminate(value,value_len);
1008
1009    if (inPath) {
1010        path_len = strlen(inPath) + 1;
1011    }
1012    else {
1013        path_len = 0;
1014    }
1015
1016    if (inValue){
1017        value_len = strlen(inValue) + 1;
1018    }
1019    else {
1020        value_len = 0;
1021    }
1022
1023    rp_lib_put_id_str(handle,inPath,inValue,NULL,append,path_len,value_len,0);
1024
1025    if (inPath) {
1026        free(inPath);
1027        inPath = NULL;
1028    }
1029
1030    if (inValue) {
1031        free(inValue);
1032        inValue = NULL;
1033    }
1034}
1035
1036
1037void rp_lib_put_id_str(int* handle,
1038                       char* path,
1039                       char* value,
1040                       char* id,
1041                       int* append,
1042                       int path_len,
1043                       int value_len,
1044                       int id_len)
1045{
1046    PyObject* lib = NULL;
1047
1048    char* inPath = NULL;
1049    char* inValue = NULL;
1050    char* inId = NULL;
1051
1052    inPath = null_terminate(path,path_len);
1053    inValue = null_terminate(value,value_len);
1054    inId = null_terminate(id,id_len);
1055
1056    if (rapptureStarted) {
1057        if ((handle) && (*handle != 0)) {
1058            lib = getObject(*handle);
1059
1060            if (lib) {
1061                rpPut(lib, inPath, inValue, inId, *append);
1062            }
1063        }
1064    }
1065
1066    if (inPath) {
1067        free(inPath);
1068        inPath = NULL;
1069    }
1070
1071    if (inValue) {
1072        free(inValue);
1073        inValue = NULL;
1074    }
1075
1076    if (inId) {
1077        free(inId);
1078        inId = NULL;
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    char* inPath = NULL;
1089
1090    inPath = null_terminate(path,path_len);
1091
1092    if (inPath) {
1093        path_len = strlen(inPath) + 1;
1094    }
1095
1096    rp_lib_put_id_obj(handle,inPath,valHandle,NULL,append,path_len,0);
1097
1098    if (inPath) {
1099        free(inPath);
1100        inPath = NULL;
1101    }
1102
1103}
1104
1105void rp_lib_put_id_obj(int* handle,
1106                       char* path,
1107                       int* valHandle,
1108                       char* id,
1109                       int* append,
1110                       int path_len,
1111                       int id_len)
1112{
1113    PyObject* lib = NULL;
1114    PyObject* value = NULL;
1115
1116    char* inPath = NULL;
1117    char* inId = NULL;
1118
1119    inPath = null_terminate(path,path_len);
1120    inId = null_terminate(id,id_len);
1121
1122    if (rapptureStarted) {
1123        if ((handle) && (*handle != 0)) {
1124            lib = getObject(*handle);
1125            value = getObject(*valHandle);
1126
1127            if (lib && value) {
1128                // retObj is a borrowed object
1129                // whose contents must not be modified
1130                rpPutObj(lib, inPath, value, inId, *append);
1131            }
1132        }
1133    }
1134
1135    if (inPath) {
1136        free(inPath);
1137        inPath = NULL;
1138    }
1139
1140    if (inId) {
1141        free(inId);
1142        inId = NULL;
1143    }
1144}
1145
1146int rp_lib_remove(int* handle, char* path, int path_len)
1147{
1148    int newObjHandle = -1;
1149
1150    PyObject* lib = NULL;
1151    PyObject* removedObj = NULL;
1152
1153    char* inPath = NULL;
1154
1155    inPath = null_terminate(path,path_len);
1156
1157    if (rapptureStarted) {
1158        if ((handle) && (*handle != 0)) {
1159            lib = getObject(*handle);
1160
1161            if (lib) {
1162                removedObj = rpRemove(lib, inPath);
1163
1164                if (removedObj) {
1165                    newObjHandle = storeObject(removedObj);
1166                    // Py_DECREF(removedObj);
1167                }
1168            }
1169        }
1170    }
1171
1172    if (inPath) {
1173        free(inPath);
1174        inPath = NULL;
1175    }
1176
1177    return newObjHandle;
1178}
1179
1180int rp_lib_xml_len(int* handle)
1181{
1182    int length = -1;
1183    char* xmlText = NULL;
1184
1185    PyObject* lib = NULL;
1186
1187    if (rapptureStarted) {
1188        if ((handle) && (*handle != 0)) {
1189            lib = getObject(*handle);
1190
1191            if (lib) {
1192                // dont modify xmlText, borrowed pointer
1193                xmlText = rpXml(lib);
1194
1195                if (xmlText) {
1196                    length = strlen(xmlText) + 1;
1197                    free(xmlText);
1198                    // printf("len = :%d:\n",length);
1199                }
1200            }
1201        }
1202    }
1203    return length;
1204}
1205
1206void rp_lib_xml(int* handle, char* retText, int retText_len)
1207{
1208    // int length_in = 0;
1209    // int length_out = 0;
1210    // int i = 0;
1211    char* xmlText = NULL;
1212
1213    PyObject* lib = NULL;
1214
1215    if (rapptureStarted) {
1216        if ((handle) && (*handle != 0)) {
1217            lib = getObject(*handle);
1218
1219            if (lib) {
1220                xmlText = rpXml(lib);
1221
1222                if (xmlText) {
1223
1224                    fortranify(xmlText, retText, retText_len);
1225
1226                    /*
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
1241                    free(xmlText);
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{
1319    PyObject* lib = NULL;
1320
1321    if (rapptureStarted && handle && *handle != 0) {
1322        lib = getObject(*handle);
1323        if (lib) {
1324            rpResult(lib);
1325        }
1326    }
1327    rp_quit();
1328}
1329
1330int objType( char* flavor)
1331{
1332    if (flavor == NULL) {
1333        // return a PyObject*
1334        return 0;
1335    }
1336    else if ((*flavor == 'o') && (strncmp(flavor,"object", 6) == 0)) {
1337        // return a PyObject*
1338        return 0;
1339    }
1340    else if (((*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}
Note: See TracBrowser for help on using the repository browser.