source: trunk/src/fortran/RpLibraryFInterface.cc @ 232

Last change on this file since 232 was 165, checked in by dkearney, 18 years ago

1) removing matlab and octave tests for put*id functions
2) adding old fortran examples
3) minor changes to matlab and octave to fix rpUnitsGetBasis
4) rpUnitsMakeMetric does not work in matlab
5) changes to fortran bindings to return proper error codes.
6) added debugging to matlab compiling options and changed outdir location in Makefile

File size: 22.9 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  INTERFACE: Fortran Rappture Library Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2004-2005  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include "RpLibraryFInterface.h"
15#include "RpBindingsDict.h"
16#include "RpLibraryFStubs.c"
17
18/**********************************************************************/
19// FUNCTION: rp_lib(const char* filePath, int filePath_len)
20/// Open the file at 'filePath' and return Rappture Library Object.
21/**
22 */
23
24int rp_lib(const char* filePath, int filePath_len) {
25
26    RpLibrary* lib = NULL;
27    int handle = -1;
28    std::string inFilePath = "";
29
30    inFilePath = null_terminate_str(filePath, filePath_len);
31
32    // create a RapptureIO object and store in dictionary
33    lib = new RpLibrary(inFilePath);
34
35    if (lib) {
36        handle = storeObject_Lib(lib);
37    }
38
39    return handle;
40}
41
42/**********************************************************************/
43// FUNCTION: rp_lib_element_obj()
44/// Return the node located at 'path'.
45/**
46 */
47int rp_lib_element_obj(int* handle,     /* integer handle of library */
48                          char* path,     /* null terminated path */
49                          int path_len
50                        )
51{
52    int newObjHandle = -1;
53
54    std::string inPath = "";
55
56    RpLibrary* lib = NULL;
57    RpLibrary* retObj = NULL;
58
59    inPath = null_terminate_str(path,path_len);
60
61    if ((handle) && (*handle != 0)) {
62        lib = getObject_Lib(*handle);
63        if (lib) {
64            retObj = lib->element(inPath);
65
66            if (retObj) {
67                newObjHandle = storeObject_Lib(retObj);
68            }
69        }
70    }
71
72    return newObjHandle;
73}
74
75/**********************************************************************/
76// FUNCTION: rp_lib_element_comp()
77/// Return the component name of the node located at 'path'.
78/**
79 */
80void rp_lib_element_comp( int* handle, /* integer handle of library */
81                            char* path,      /* null terminated path */
82                            char* retText,   /* return buffer for fortran*/
83                            int path_len,
84                            int retText_len /* length of return text buffer */
85                          )
86{
87    std::string inPath = "";
88    RpLibrary* lib = NULL;
89    std::string retStr = "";
90
91    inPath = null_terminate_str(path,path_len);
92
93    lib = getObject_Lib(*handle);
94
95    if (lib) {
96        retStr = lib->element(inPath)->nodeComp();
97        if (!retStr.empty()) {
98            fortranify(retStr.c_str(),retText,retText_len);
99        }
100    }
101
102}
103
104/**********************************************************************/
105// FUNCTION: rp_lib_element_id()
106/// Return the 'id' of node located at 'path'.
107/**
108 */
109void rp_lib_element_id(   int* handle,       /* integer handle of library    */
110                            char* path,      /* path of element within xml   */
111                            char* retText,   /* return buffer for fortran    */
112                            int path_len,
113                            int retText_len  /* length of return text buffer */
114                        )
115{
116    std::string inPath = "";
117    RpLibrary* lib = NULL;
118    std::string retStr = "";
119
120    inPath = null_terminate_str(path,path_len);
121
122    lib = getObject_Lib(*handle);
123
124    if (lib) {
125        retStr = lib->element(inPath)->nodeId();
126        if (!retStr.empty()) {
127            fortranify(retStr.c_str(),retText,retText_len);
128        }
129    }
130}
131
132/**********************************************************************/
133// FUNCTION: rp_lib_element_type()
134/// Return the type of the node located at 'path'.
135/**
136 */
137void rp_lib_element_type( int* handle,       /* integer handle of library */
138                            char* path,      /* search path inside xml */
139                            char* retText,   /* return buffer for fortran*/
140                            int path_len,
141                            int retText_len  /* length of return text buffer */
142                          )
143{
144    std::string inPath = "";
145    RpLibrary* lib = NULL;
146    std::string retStr = "";
147
148    inPath = null_terminate_str(path,path_len);
149
150    lib = getObject_Lib(*handle);
151
152    if (lib) {
153        retStr = lib->element(inPath)->nodeType();
154        fortranify(retStr.c_str(),retText,retText_len);
155    }
156}
157
158/**********************************************************************/
159// FUNCTION: rp_lib_children(const char* filePath, int filePath_len)
160/// Retrieve the next child of 'path' from the Rappture Library Object.
161/**
162 */
163int rp_lib_children (   int* handle, /* integer handle of library */
164                        char* path, /* search path of the xml */
165                        int* childHandle, /*integer hanlde of last returned child*/
166                        int path_len  /* length of the search path buffer */
167                    ) {
168
169    std::string inPath = "";
170    RpLibrary* lib = NULL;
171    RpLibrary* childNode = NULL;
172    int newObjHandle = -1;
173
174    inPath = null_terminate_str(path,path_len);
175
176    if (handle && (*handle >= 0) ) {
177        lib = getObject_Lib(*handle);
178        if (lib) {
179
180            if (*childHandle > 0) {
181                // check to see if there were any previously returned children
182                childNode = getObject_Lib(*childHandle);
183            }
184
185            // call the children base method
186            childNode = lib->children(inPath,childNode);
187
188            // store the childNode in the dictionary.
189            //
190            // because the base method is using static memory to get store the
191            // children we should be able to check and see if the childHandle
192            // was valid.
193            // if so, then we can just return the childHandle back to the user
194            // if not, store the object in the dictionary and return the new
195            // handle.
196
197            if (childNode) {
198                if (*childHandle < 1) {
199                    newObjHandle = storeObject_Lib(childNode);
200                }
201                else {
202                    newObjHandle = storeObject_Lib(childNode,*childHandle);
203                }
204            }
205        }
206    }
207
208    return newObjHandle;
209
210}
211
212
213//int rp_lib_child_comp (   int* handle,    /* integer handle of library */
214//                            char* path,     /* DOM path of requested object */
215//                            char* type,     /* specific name of element */
216//                            int* childNum,  /* child number for iteration */
217//                            char* retText,  /* buffer to store return text */
218//                            int path_len,   /* length of path */
219//                            int type_len,   /* length of type */
220//                            int retText_len /* length of return text buffer */
221//                       )
222/*
223{
224    int retVal = 0;
225
226    char* inPath = NULL;
227    char* inType = NULL;
228
229    inPath = null_terminate(path,path_len);
230    inType = null_terminate(type,type_len);
231
232    if (inPath) {
233        path_len = strlen(inPath) + 1;
234    }
235    else {
236        path_len = 0;
237    }
238
239    if (inType) {
240        type_len = strlen(inType) + 1;
241    }
242    else {
243        type_len = 0;
244    }
245
246    retVal = rp_lib_child_str( handle,
247                                 inPath,
248                                 "component",
249                                 inType,
250                                 childNum,
251                                 retText,
252                                 path_len,
253                                 10,
254                                 type_len,
255                                 retText_len
256                               );
257
258    if (inPath) {
259        free(inPath);
260        inPath = NULL;
261    }
262
263    if (inType) {
264        free(inType);
265        inType = NULL;
266    }
267
268    return retVal;
269}
270*/
271
272//int rp_lib_child_id(   int* handle,    /* integer handle of library */
273//                          char* path,     /* DOM path of requested object */
274//                          char* type,     /* specific name of element */
275//                          int* childNum,  /* child number for iteration */
276//                          char* retText,  /* buffer to store return text */
277//                          int path_len,   /* length of path */
278//                          int type_len,   /* length of type */
279//                          int retText_len /* length of return text buffer */
280//                       )
281/*
282{
283    int retVal = 0;
284    char* inPath = NULL;
285    char* inType = NULL;
286
287    inPath = null_terminate(path,path_len);
288    inType = null_terminate(type,type_len);
289
290    if (inPath) {
291        path_len = strlen(inPath) + 1;
292    }
293    else {
294        path_len = 0;
295    }
296
297    if (inType) {
298        type_len = strlen(inType) + 1;
299    }
300    else {
301        type_len = 0;
302    }
303
304    retVal = rp_lib_child_str( handle,
305                                 inPath,
306                                 "id",
307                                 inType,
308                                 childNum,
309                                 retText,
310                                 path_len,
311                                 3,
312                                 type_len,
313                                 retText_len
314                               );
315
316    if (inPath) {
317        free(inPath);
318        inPath = NULL;
319    }
320
321    if (inType) {
322        free(inType);
323        inType = NULL;
324    }
325
326    return retVal;
327}
328*/
329
330//int rp_lib_child_type (   int* handle,    /* integer handle of library */
331//                            char* path,     /* DOM path of requested object */
332//                            char* type,     /* specific name of element */
333//                            int* childNum,  /* child number for iteration */
334//                            char* retText,  /* buffer to store return text */
335//                            int path_len,   /* length of path */
336//                            int type_len,   /* length of type */
337//                            int retText_len /* length of return text buffer */
338//                       )
339/*
340{
341    int retVal = 0;
342    char* inPath = NULL;
343    char* inType = NULL;
344
345    inPath = null_terminate(path,path_len);
346    inType = null_terminate(type,type_len);
347
348    if (inPath) {
349        path_len = strlen(inPath) + 1;
350    }
351    else {
352        path_len = 0;
353    }
354
355    if (inType) {
356        type_len = strlen(inType) + 1;
357    }
358    else {
359        type_len = 0;
360    }
361
362    retVal = rp_lib_child_str( handle,
363                                 inPath,
364                                 "type",
365                                 inType,
366                                 childNum,
367                                 retText,
368                                 path_len,
369                                 5,
370                                 type_len,
371                                 retText_len
372                               );
373
374    if (inPath) {
375        free(inPath);
376        inPath = NULL;
377    }
378
379    if (inType) {
380        free(inType);
381        inType = NULL;
382    }
383
384    return retVal;
385}
386*/
387
388/*
389int rp_lib_child_obj ( int* handle,
390                            char* path,
391                            char* type,
392                            int path_len,
393                            int type_len
394                          )
395{
396    int newObjHandle = -1;
397
398    PyObject* lib = NULL;
399    PyObject* list = NULL;
400
401    char* inPath = NULL;
402    char* inType = NULL;
403
404    inPath = null_terminate(path,path_len);
405    inType = null_terminate(type,type_len);
406
407    if (rapptureStarted) {
408        if ((handle) && (*handle != 0)) {
409            lib = getObject_Lib(*handle);
410
411            if (lib) {
412                list = rpChildren_f(lib, inPath, "object");
413                if (list) {
414                    // store the whole list,
415                    // need to make a way to read the nodes
416                    newObjHandle = storeObject_Lib(list);
417                    // Py_DECREF(list);
418                }
419                else {
420
421                }
422            }
423        }
424    }
425
426    if (inPath) {
427        free(inPath);
428        inPath = NULL;
429    }
430
431    if (inType) {
432        free(inType);
433        inType = NULL;
434    }
435
436    return newObjHandle;
437
438}
439*/
440
441/**********************************************************************/
442// FUNCTION: rp_lib_get()
443/// Get data located at 'path' and return it as a string value.
444/**
445 */
446void rp_lib_get( int* handle, /* integer handle of library */
447                   char* path,      /* null terminated path */
448                   char* retText,   /* return text buffer for fortran*/
449                   int path_len,
450                   int retText_len /* length of return text buffer */
451                 )
452{
453    std::string xmlText = "";
454
455    RpLibrary* lib = NULL;
456
457    std::string inPath = "";
458
459    inPath = null_terminate_str(path,path_len);
460
461    if ((handle) && (*handle != 0)) {
462        lib = getObject_Lib(*handle);
463
464        if (lib) {
465            xmlText = lib->get(inPath);
466            if (!xmlText.empty()) {
467                fortranify(xmlText.c_str(),retText,retText_len);
468            }
469
470        }
471    }
472}
473
474/**********************************************************************/
475// FUNCTION: rp_lib_get_str()
476/// Get data located at 'path' and return it as a string value.
477/**
478 */
479void rp_lib_get_str( int* handle, /* integer handle of library */
480                   char* path,      /* null terminated path */
481                   char* retText,   /* return text buffer for fortran*/
482                   int path_len,
483                   int retText_len /* length of return text buffer */
484                 )
485{
486    std::string xmlText = "";
487
488    RpLibrary* lib = NULL;
489
490    std::string inPath = "";
491
492    inPath = null_terminate_str(path,path_len);
493
494    if ((handle) && (*handle != 0)) {
495        lib = getObject_Lib(*handle);
496
497        if (lib) {
498            xmlText = lib->getString(inPath);
499            if (!xmlText.empty()) {
500                fortranify(xmlText.c_str(),retText,retText_len);
501            }
502
503        }
504    }
505}
506
507
508/**********************************************************************/
509// FUNCTION: rp_lib_get_double()
510/// Get data located at 'path' and return it as a double precision value.
511/**
512 */
513double rp_lib_get_double( int* handle,   /* integer handle of library */
514                            char* path,    /* null terminated path */
515                            int path_len
516                          )
517{
518    double retVal = 0.0;
519
520    RpLibrary* lib = NULL;
521
522    std::string inPath = "";
523
524    inPath = null_terminate_str(path,path_len);
525
526    if ((handle) && (*handle != 0)) {
527        lib = getObject_Lib(*handle);
528
529        if (lib) {
530            retVal = lib->getDouble(inPath);
531        }
532    }
533
534    return retVal;
535}
536
537
538/**********************************************************************/
539// FUNCTION: rp_lib_put_str()
540/// Put string into Rappture Library Object at location 'path'.
541/**
542 */
543void rp_lib_put_str( int* handle,
544                        char* path,
545                        char* value,
546                        int* append,
547                        int path_len,
548                        int value_len
549                      )
550{
551    std::string inPath = "";
552    std::string inValue = "";
553    RpLibrary* lib = NULL;
554
555    inPath = null_terminate_str(path,path_len);
556    inValue = null_terminate_str(value,value_len);
557
558    if ((handle) && (*handle != 0)) {
559        lib = getObject_Lib(*handle);
560
561        if (lib) {
562            lib->put(inPath,inValue,"",*append);
563        }
564    }
565
566    return;
567
568}
569
570
571/**********************************************************************/
572// FUNCTION: rp_lib_put_id_str()
573/// Put string into Rappture Library Object at location 'path' with id = 'id'
574/**
575 */
576void rp_lib_put_id_str ( int* handle,
577                        char* path,
578                        char* value,
579                        char* id,
580                        int* append,
581                        int path_len,
582                        int value_len,
583                        int id_len
584                      )
585{
586    RpLibrary* lib = NULL;
587
588    std::string inPath = "";
589    std::string inValue = "";
590    std::string inId = "";
591
592    inPath = null_terminate_str(path,path_len);
593    inValue = null_terminate_str(value,value_len);
594    inId = null_terminate_str(id,id_len);
595
596    if ((handle) && (*handle != 0)) {
597        lib = getObject_Lib(*handle);
598
599        if (lib) {
600            lib->put(inPath,inValue,inId,*append);
601        }
602    }
603
604}
605
606//void rp_lib_put_obj( int* handle,
607//                        char* path,
608//                        int* valHandle,
609//                        int* append,
610//                        int path_len
611//                      )
612//{
613//    std::string inPath = "";
614//    RpLibrary* lib = NULL;
615//
616//    // inPath = null_terminate(path,path_len);
617//    inPath = std::string(path,path_len);
618//
619//    /*
620//    if (inPath) {
621//        path_len = strlen(inPath) + 1;
622//    }
623//    */
624//
625//    if ((handle) && (*handle != 0)) {
626//        lib = getObject_Lib(*handle);
627//       
628//        if (lib) {
629//            // rpPut(lib, inPath, inValue, inId, *append);
630//            lib->put(inPath,inValue,"",*append);
631//        }
632//    }
633//
634//    /*
635//    rp_lib_put_id_obj(handle,inPath,valHandle,NULL,append,path_len,0);
636//
637//    if (inPath) {
638//        free(inPath);
639//        inPath = NULL;
640//    }
641//    */
642//
643//}
644
645/*
646void rp_lib_put_id_obj ( int* handle,
647                        char* path,
648                        int* valHandle,
649                        char* id,
650                        int* append,
651                        int path_len,
652                        int id_len
653                      )
654{
655    PyObject* lib = NULL;
656    PyObject* value = NULL;
657
658    char* inPath = NULL;
659    char* inId = NULL;
660
661    inPath = null_terminate(path,path_len);
662    inId = null_terminate(id,id_len);
663
664    if (rapptureStarted) {
665        if ((handle) && (*handle != 0)) {
666            lib = getObject_Lib(*handle);
667            value = getObject_Lib(*valHandle);
668
669            if (lib && value) {
670                // retObj is a borrowed object
671                // whose contents must not be modified
672                rpPutObj(lib, inPath, value, inId, *append);
673            }
674        }
675    }
676
677    if (inPath) {
678        free(inPath);
679        inPath = NULL;
680    }
681
682    if (inId) {
683        free(inId);
684        inId = NULL;
685    }
686
687}
688*/
689
690/*
691int rp_lib_remove (int* handle, char* path, int path_len)
692{
693    int newObjHandle = -1;
694
695    PyObject* lib = NULL;
696    PyObject* removedObj = NULL;
697
698    char* inPath = NULL;
699
700    inPath = null_terminate(path,path_len);
701
702    if (rapptureStarted) {
703        if ((handle) && (*handle != 0)) {
704            lib = getObject_Lib(*handle);
705
706            if (lib) {
707                removedObj = rpRemove(lib, inPath);
708
709                if (removedObj) {
710                    newObjHandle = storeObject_Lib(removedObj);
711                    // Py_DECREF(removedObj);
712                }
713
714            }
715        }
716    }
717
718    if (inPath) {
719        free(inPath);
720        inPath = NULL;
721    }
722
723    return newObjHandle;
724}
725*/
726
727/*
728int rp_lib_xml_len (int* handle)
729{
730    int length = -1;
731    char* xmlText = NULL;
732
733    PyObject* lib = NULL;
734
735    if (rapptureStarted) {
736        if ((handle) && (*handle != 0)) {
737            lib = getObject_Lib(*handle);
738
739            if (lib) {
740                // dont modify xmlText, borrowed pointer
741                xmlText = rpXml(lib);
742
743                if (xmlText) {
744                    length = strlen(xmlText) + 1;
745                    free(xmlText);
746                    // printf("len = :%d:\n",length);
747                }
748            }
749        }
750    }
751    return length;
752}
753*/
754
755/**********************************************************************/
756// FUNCTION: rp_lib_write_xml()
757/// Write the xml text into a file.
758/**
759 * \sa {rp_result}
760 */
761int rp_lib_write_xml(int* handle, char* outFile, int outFile_len)
762{
763    int retVal = -1;
764    RpLibrary* lib = NULL;
765    std::string inOutFile = "";
766    std::string xmlText = "";
767    std::fstream file;
768
769    inOutFile = null_terminate_str(outFile, outFile_len);
770
771    if (!inOutFile.empty() ) {
772        file.open(inOutFile.c_str(),std::ios::out);
773    }
774
775    if ( file.is_open() ) {
776        if ((handle) && (*handle != 0)) {
777            lib = getObject_Lib(*handle);
778
779            if (lib) {
780                xmlText = lib->xml();
781                if (!xmlText.empty()) {
782                    file << xmlText;
783                    retVal = 0;
784                }
785            }
786        }
787
788        file.close();
789    }
790
791    return retVal;
792}
793
794/**********************************************************************/
795// FUNCTION: rp_lib_xml()
796/// Return this node's xml text
797/**
798 */
799void  rp_lib_xml(int* handle, char* retText, int retText_len)
800{
801    std::string xmlText = "";
802
803    RpLibrary* lib = NULL;
804
805    if ((handle) && (*handle != 0)) {
806        lib = getObject_Lib(*handle);
807
808        if (lib) {
809            xmlText = lib->xml();
810            if (!xmlText.empty()) {
811                fortranify(xmlText.c_str(), retText, retText_len);
812            }
813        }
814    }
815}
816
817/**********************************************************************/
818// FUNCTION: rp_lib_node_comp()
819/// Return this node's component name
820/**
821 */
822void rp_lib_node_comp ( int* handle, char* retText, int retText_len ) {
823
824    std::string retStr = "";
825    RpLibrary* node = NULL;
826
827    if ((handle) && (*handle != 0)) {
828        node = getObject_Lib(*handle);
829
830        if (node) {
831            retStr = node->nodeComp();
832            if (!retStr.empty()) {
833                fortranify(retStr.c_str(), retText, retText_len);
834            }
835        }
836    }
837}
838
839/**********************************************************************/
840// FUNCTION: rp_lib_node_type()
841/// Return this node's type
842/**
843 */
844void rp_lib_node_type ( int* handle, char* retText, int retText_len ) {
845
846    std::string retStr = "";
847    RpLibrary* node = NULL;
848
849    if ((handle) && (*handle != 0)) {
850        node = getObject_Lib(*handle);
851
852        if (node) {
853            retStr = node->nodeType();
854            if (!retStr.empty()) {
855                fortranify(retStr.c_str(), retText, retText_len);
856            }
857        }
858    }
859}
860
861/**********************************************************************/
862// FUNCTION: rp_lib_node_id()
863/// Return this node's id.
864/**
865 */
866void rp_lib_node_id ( int* handle, char* retText, int retText_len ) {
867
868    std::string retStr = "";
869    RpLibrary* node = NULL;
870
871    if ((handle) && (*handle != 0)) {
872        node = getObject_Lib(*handle);
873
874        if (node) {
875            retStr = node->nodeId();
876            if (!retStr.empty()) {
877                fortranify(retStr.c_str(), retText, retText_len);
878            }
879        }
880    }
881}
882
883/**********************************************************************/
884// FUNCTION: rp_result()
885/// Write xml text to a run.xml file and signal the program has completed
886/**
887 */
888void rp_result(int* handle) {
889    RpLibrary* lib = NULL;
890
891    if (handle && *handle != 0) {
892        lib = getObject_Lib(*handle);
893        if (lib) {
894            lib->result();
895        }
896    }
897
898    // do no delete this, still working on testing this
899    cleanLibDict();
900}
901
Note: See TracBrowser for help on using the repository browser.