source: trunk/oldtest/src/rappture_example.c @ 5673

Last change on this file since 5673 was 5673, checked in by ldelgass, 9 years ago

Fix line endings, set eol-style to native on all C/C++ sources.

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