source: trunk/examples/c-example/plot.cc @ 872

Last change on this file since 872 was 872, checked in by dkearney, 16 years ago

mainly code cleanups that i've made and stored in my repository over time.

File size: 2.9 KB
Line 
1// ======================================================================
2//  Copyright (c) 2004-2005  Purdue Research Foundation
3//  See the file "license.terms" for information on usage and
4//  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5// ======================================================================
6
7#include "rappture.h"
8
9#include <stdlib.h>
10#include <math.h>
11#include <iostream>
12#include <sstream>
13
14int main(int argc, char * argv[])
15{
16
17    RpLibrary* lib = NULL;
18
19    std::string filePath = "";
20    std::string xmltext = "";
21    double fmin, fmax;
22    std::string strFormula = "";
23    int i;
24
25    if (argc < 2) {
26        std::cout << "usage: " << argv[0] << " driver.xml" << std::endl;
27    }
28
29    filePath = std::string(argv[1]);
30
31    if (DEBUG) {
32        std::cout << "filePath: " << filePath << std::endl;
33    }
34
35    // create a rappture library from the file filePath
36    lib = new RpLibrary(filePath);
37
38    if (lib) {
39        if(DEBUG) {
40            std::cout << "created Rappture Library successfully" << std::endl;
41        }
42    }
43    else {
44        // cannot open file or out of memory
45        std::cout << "FAILED creating Rappture Library" << std::endl;
46        exit(1);
47    }
48
49    // get the xml that is stored in the rappture library lib
50    xmltext = lib->xml();
51
52    if(! (xmltext.empty()) ) {
53        if(DEBUG) {
54        //printf("XML file content:\n");
55        //printf("%s\n", xmltext);
56    }
57    }
58    else {
59        printf("lib->xml() failed\n");
60        delete lib;
61        exit(1);
62    }
63
64    // get the min
65    xmltext = lib->getString("input.number(min).current");
66
67    if ( xmltext.empty() ) {
68        std::cout << "lib->getString(input.number(xmin).current) returns null" << std::endl;
69        delete lib;
70        exit(1);
71    }
72
73    if(DEBUG) {
74        std::cout << "xml min :" << xmltext << ": len=" << xmltext.size() << std::endl;
75    }
76
77    // grab a double value from the xml
78    fmin = lib->getDouble("input.number(min).current");
79
80    if(DEBUG) {
81        std::cout << "min: " << fmin << std::endl;
82    }
83
84    // get the max
85    fmax = lib->getDouble("input.(max).current");
86    if(DEBUG) {
87        std::cout << "max: " << fmax << std::endl;
88    }
89
90    // label the graph with a title
91    lib->put("output.curve(result).about.label",
92        "Formula: Y Vs X","",RPLIB_OVERWRITE);
93
94    // evaluate formula and generate results
95    // science begains here
96    double fx, fy;
97    int npts = 100;
98    std::stringstream myStr;
99
100    for (i = 0; i<npts; i++) {
101        fx = i* (fmax - fmin)/npts + fmin;
102        fy = sin(fx);
103        myStr << fx << " " << fy << std::endl;
104        lib->put("output.curve(result).component.xy",
105            myStr.str(),"",RPLIB_APPEND);
106        myStr.str("");
107    }
108
109
110    // write output to run file and signal
111    lib->result();
112
113    delete lib;
114
115    return 0;
116}
Note: See TracBrowser for help on using the repository browser.