1 | /*
|
---|
2 | * ======================================================================
|
---|
3 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
4 | * See the file "license.terms" for information on usage and
|
---|
5 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
---|
6 | * ======================================================================
|
---|
7 | */
|
---|
8 | #include "rappture.h"
|
---|
9 |
|
---|
10 | #include <stdlib.h>
|
---|
11 | #include <string.h>
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <math.h>
|
---|
14 |
|
---|
15 | #ifdef DEBUG
|
---|
16 | static int debug = 1;
|
---|
17 | #else
|
---|
18 | static int debug = 0;
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | int
|
---|
22 | main(int argc, char **argv)
|
---|
23 | {
|
---|
24 |
|
---|
25 | RpLibrary* lib = NULL;
|
---|
26 |
|
---|
27 | const char* filePath;
|
---|
28 | const char* xmltext = NULL;
|
---|
29 | double fmin, fmax;
|
---|
30 | char strFormula[100];
|
---|
31 | int i;
|
---|
32 | int err = 0;
|
---|
33 |
|
---|
34 | double fx, fy;
|
---|
35 | int npts = 100;
|
---|
36 | char str[50];
|
---|
37 |
|
---|
38 | memset(strFormula, '\0', 100);
|
---|
39 |
|
---|
40 | if (argc < 2) {
|
---|
41 | printf("usage: %s driver.xml\n", argv[0]);
|
---|
42 | }
|
---|
43 |
|
---|
44 | filePath = argv[1];
|
---|
45 |
|
---|
46 | if (debug)
|
---|
47 | printf("filePath: %s:\n", filePath);
|
---|
48 |
|
---|
49 | // create a rappture library from the file filePath
|
---|
50 | lib = rpLibrary(argv[1]);
|
---|
51 |
|
---|
52 | if (lib) {
|
---|
53 | if(debug) {
|
---|
54 | printf("created Rappture Library successfully\n");
|
---|
55 | }
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | // cannot open file or out of memory
|
---|
59 | printf("FAILED creating Rappture Library\n");
|
---|
60 | return(1);
|
---|
61 | }
|
---|
62 |
|
---|
63 | // get the xml that is stored in the rappture library lib
|
---|
64 | err = rpXml(lib,&xmltext);
|
---|
65 | if( !err ) {
|
---|
66 | if(debug) {
|
---|
67 | //printf("XML file content:\n");
|
---|
68 | //printf("%s\n", xmltext);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | printf("xml(lib) failed\n");
|
---|
73 | return(1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | // get the min
|
---|
77 | rpGetString (lib, "input.number(min).current",&xmltext);
|
---|
78 |
|
---|
79 | if (! (xmltext) ) {
|
---|
80 | printf("getString(lib,input.number(xmin).current) returns null\n");
|
---|
81 | return(1);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // if you want to keep the string around, you will need to malloc
|
---|
85 | // space for it and strncpy() it to your own space.
|
---|
86 | // it will live in rappture's memory until the next call to getString()
|
---|
87 |
|
---|
88 | if(debug) {
|
---|
89 | printf("xml min: %s: len=%d\n", xmltext, (int)strlen(xmltext));
|
---|
90 | }
|
---|
91 |
|
---|
92 | fmin = atof(xmltext);
|
---|
93 | // fmin = getDouble(lib,"input.number(min).current");
|
---|
94 | if(debug) {
|
---|
95 | printf("min: %f\n", fmin);
|
---|
96 | }
|
---|
97 |
|
---|
98 | // get the max
|
---|
99 | rpGetDouble(lib,"input.(max).current",&fmax);
|
---|
100 | if(debug) {
|
---|
101 | printf("max: %f\n", fmax);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // label the graph with a title
|
---|
105 | rpPutString(lib,"output.curve(result).about.label",
|
---|
106 | "Formula: Y Vs X",RPLIB_OVERWRITE);
|
---|
107 |
|
---|
108 | // evaluate formula and generate results
|
---|
109 | for (i = 0; i<npts; i++) {
|
---|
110 | fx = i* (fmax - fmin)/npts + fmin;
|
---|
111 | fy = sin(fx);
|
---|
112 | sprintf(str, "%f %f\n", fx, fy);
|
---|
113 | rpPutString(lib,"output.curve(result).component.xy",str,RPLIB_APPEND);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | // write output to run file and signal
|
---|
118 | rpResult(lib);
|
---|
119 |
|
---|
120 | // free the rappture library
|
---|
121 | rpFreeLibrary(&lib);
|
---|
122 |
|
---|
123 | // exit program
|
---|
124 | return 0;
|
---|
125 | }
|
---|