source: trunk/test/src/rappture_example.c @ 104

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

initial add for test directory which will hold test cases for using rappture

File size: 5.6 KB
Line 
1#include "../include/rappture_interface.h"
2
3/* fix buffer overflow possibility*/
4char* null_terminate(char* inStr, int len)
5{
6    int retVal = 0;
7    char* newStr = NULL;
8    char* current = NULL;
9
10    if (inStr) {
11
12        current = inStr+len-1;
13
14        while ((len > 0) && (isspace(*(current)))) {
15            // dont strip off newlines
16
17            if ( (*(current) == '\f')
18              || (*(current) == '\n')
19              || (*(current) == '\r')
20              || (*(current) == '\t')
21              || (*(current) == '\v') )
22            {
23                break;
24            }
25
26            if (--len) {
27                current--;
28            }
29        }
30
31        newStr = (char*) calloc(len,(sizeof(char)));
32        strncpy(newStr,inStr,len);
33        *(newStr+len) = '\0';
34
35        retVal++;
36    }
37
38    // return retVal;
39
40    return newStr;
41}
42
43
44int main(int argc, char * argv[])
45{
46
47    PyObject* rpClass = NULL;
48    PyObject* lib = NULL;
49
50    // char* filePath = "/home/derrick/playground/rappture_fortran/xml_files/driver_big.xml";
51    // char* filePath = NULL; // "/home/derrick/playground/rappture_fortran/xml_files/driver_big.xml";
52    const char* xmlText = NULL;
53
54    // filePath = (char*) calloc(67,sizeof(char));
55
56    char* filePath2 = "/home/derrick/playground/rappture_fortran/xml_files/driver_big.xml     ";
57    char* filePath = null_terminate(filePath2,70);
58//    char* filePath = "/home/derrick/playground/rappture_fortran/xml_files/driver_small.xml";
59
60    printf("filePath = :%s:\n",filePath);
61
62    //scanf("%66c", filePath );
63    // scanf("%70c", filePath );
64   
65    // initialize the interpreter
66    Py_Initialize();
67
68    rpClass = importRappture();
69    if (rpClass) {
70        printf("created rappture object\n");
71        lib = createRapptureObj(rpClass, filePath);
72        Py_DECREF(rpClass);
73
74        if (lib) {
75
76            // test lib.xml()
77            printf("test lib.xml()\n");
78           
79            xmlText = rpXml(lib);
80            if (xmlText) {
81                printf("\nXML FILE\n\n%s\n\n",xmlText);
82            }
83
84            printf("\n");
85
86            // test lib.get("...")
87            xmlText = rpGet(lib, "input.(ambient).(temperature).current");
88            if (xmlText) {
89                printf("xmlText = %s\n", xmlText);
90            }
91
92
93            Py_DECREF(lib);
94        }
95    }
96   
97    // shut down the interpreter
98    Py_Finalize();
99    return 0;
100}
101
102/*
103 *
104python notes
105
106>>> print lib.xml()
107<?xml version="1.0" ?>
108<nanohub xmlns="http://www.nanohub.org" xmlns:class="http://www.nanohub.org">
109  <purdue>
110    <class class:number="101" class:school="ECE">
111      <name>Nanotechnology 101</name>
112      <desc>Introduction to Nanotechnology</desc>
113    </class>
114  <class id="NANO512"><name>Nanotechnology 512</name><desc>Not so intro to Nanotech</desc></class></purdue>
115</nanohub>
116>>> lib.children('purdue','component')
117[u'class', 'class(NANO512)']
118>>> lib.children('purdue','type')
119[u'class', 'class']
120>>> lib.children('purdue','id')
121[u'class', 'NANO512']
122>>> lib.children('purdue','object')
123[<Rappture.library.library instance at 0xb7d2b32c>, <Rappture.library.library instance at 0xb7d2b42c>]
124>>> lib.children('class', 'component')
125[u'name', u'desc']
126>>> lib.children('name', 'component')
127[]
128>>> lib.children('desc', 'component')
129[]
130>>>
131>>>
132>>> lib.element('purdue','component')
133u'purdue'
134>>> lib.element('class','component')
135u'class'
136>>> lib.element('class(NANO512)','component')
137'class(NANO512)'
138>>>
139>>> lib.element('purdue','type')
140u'purdue'
141>>> lib.element('class','type')
142u'class'
143>>> lib.element('class(NANO512)','type')
144'class'
145>>>
146>>> lib.element('purdue','id')
147u'purdue'
148>>> lib.element('class','id')
149u'class'
150>>> lib.element('class(NANO512)','id')
151'NANO512'
152>>> lib.get('purdue.class(NANO512).name')
153'Nanotechnology 512'
154>>> lib.get('purdue.class.name')
155u'Nanotechnology 101'
156>>> lib.put('uiuc.class(NANO500).name','Nanotechnology 500')
157>>> lib.put('uiuc.class(NANO500).desc','Carbon NanoTubes')
158>>> print lib.xml()
159<?xml version="1.0" ?>
160<nanohub xmlns="http://www.nanohub.org" xmlns:class="http://www.nanohub.org">
161  <purdue>
162    <class class:number="101" class:school="ECE">
163      <name>Nanotechnology 101</name>
164      <desc>Introduction to Nanotechnology</desc>
165    </class>
166  <class id="NANO512"><name>Nanotechnology 512</name><desc>Not so intro to Nanotech</desc></class></purdue>
167<uiuc><class id="NANO500"><name>Nanotechnology 500</name><desc>Carbon NanoTubes</desc></class></uiuc></nanohub>
168>>> nano500 = lib.element('class(NANO500)')
169>>> nano500.get('name')
170'Nanotechnology 500'
171>>> lib.put('sdsc',nano500)
172>>> print lib.xml()
173<?xml version="1.0" ?>
174<nanohub xmlns="http://www.nanohub.org" xmlns:class="http://www.nanohub.org">
175  <purdue>
176    <class class:number="101" class:school="ECE">
177      <name>Nanotechnology 101</name>
178      <desc>Introduction to Nanotechnology</desc>
179    </class>
180    <class id="NANO512">
181      <name>Nanotechnology 512</name>
182      <desc>Not so intro to Nanotech</desc>
183    </class>
184  </purdue>
185  <uiuc/>
186  <sdsc>
187    <class id="NANO500">
188      <name>Nanotechnology 500</name>
189      <desc>hi</desc>
190    </class>
191  </sdsc>
192</nanohub>
193>>>
194>>> lib.put('uw',nano500)
195>>> print lib.xml()
196<?xml version="1.0" ?>
197<nanohub xmlns="http://www.nanohub.org" xmlns:class="http://www.nanohub.org">
198  <purdue>
199    <class class:number="101" class:school="ECE">
200      <name>Nanotechnology 101</name>
201      <desc>Introduction to Nanotechnology</desc>
202    </class>
203    <class id="NANO512">
204      <name>Nanotechnology 512</name>
205      <desc>Not so intro to Nanotech</desc>
206    </class>
207  </purdue>
208  <uiuc/>
209  <sdsc/>
210  <uw>
211    <class id="NANO500">
212      <name>Nanotechnology 500</name>
213      <desc>hi</desc>
214    </class>
215  </uw>
216</nanohub>
217>>>
218
219
220*/
221
222
Note: See TracBrowser for help on using the repository browser.