Ignore:
Timestamp:
Aug 28, 2009, 5:54:24 AM (15 years ago)
Author:
dkearney
Message:

updates to the object system, fixed up tree and xml parser objects, added some basic tests for them and adopted number object to accept xml text and configure itself from the parsed xml. added fermi3.cc example program which contains suggested interface from apps meeting.

Location:
trunk/examples/objects
Files:
7 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/objects/Makefile.in

    r1528 r1560  
    1818                axis \
    1919                curve \
     20                dxReader \
    2021                dxWriter \
    2122                floatBuffer \
  • trunk/examples/objects/app-fermi/fermi1.cc

    r1542 r1560  
    7474
    7575    // do it the easy way,
    76     // create a plot to add to the library
     76    // create a plot and add to the library
    7777    // p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
    7878
    79     Rappture::Plot *p1 = new Rappture::Plot();
     79    Rappture::Plot *p1 = new Rappture::Plot(lib);
    8080    p1->add(nPts,fArr,EArr,"",curveLabel,curveDesc);
    8181    p1.propstr("xlabel","Fermi-Dirac Factor");
    8282    p1.propstr("ylabel","Energy");
    8383    p1.propstr("yunits","eV");
    84 
    85     // add the plot to the library
    86     lib.put(p1);
    8784
    8885
     
    9188    // c1->axis("xaxis","xlabel","xdesc","units","scale",dataArr,nPts);
    9289
    93     Rappture::Curve *c1 = new Rappture::Curve("output.curve(FDF)");
     90    Rappture::Curve *c1 = new Rappture::Curve(lib,"FDF");
    9491    c1->label(curveLabel);
    9592    c1->desc(curveDesc);
  • trunk/examples/objects/app-fermi/fermi2.cc

    r1542 r1560  
    2525    Rappture::Number *T  = NULL;
    2626    Rappture::Number *Ef = NULL;
    27     double E            = 0.0;
    2827    double dE           = 0.0;
    2928    double kT           = 0.0;
    3029    double Emin         = 0.0;
    3130    double Emax         = 0.0;
    32     double f            = 0.0;
     31    size_t nPts         = 200;
     32    double E[nPts];
     33    double f[nPts];
    3334
    3435    // create a rappture library from the file filePath
     
    5556    Emax = Ef->value("eV") + 10*kT;
    5657
    57     dE = 0.005*(Emax-Emin);
     58    dE = (1.0/nPts)*(Emax-Emin);
    5859
    59     Rappture::SimpleDoubleBuffer fBuf;
    60     Rappture::SimpleDoubleBuffer EBuf;
    61 
    62     for (E = Emin; E < Emax; E = E + dE) {
     60    E = Emin;
     61    for (size_t idx = 0; idx < nPts; idx++) {
     62        E = E + dE;
    6363        f = 1.0/(1.0 + exp((E - Ef)/kT));
    64         fBuf.append(f);
    65         EBuf.append(E);
     64        fArr[idx] = f;
     65        EArr[idx] = E;
    6666        rpUtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
    6767    }
    68     const double *fArr = fBuf.data();
    69     const double *EArr = EBuf.data();
    70     size_t nPts = fBuf.nmemb();
    7168
    7269    const char *curveLabel = "Fermi-Dirac Curve"
     
    7572    // do it the easy way,
    7673    // create a plot to add to the library
     74    // plot is registered with lib upon object creation
    7775    // p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
    7876
    79     Rappture::Plot *p1 = new Rappture::Plot();
     77    Rappture::Plot *p1 = new Rappture::Plot(lib);
    8078    p1->add(nPts,fArr,EArr,"",curveLabel,curveDesc);
    8179    p1.propstr("xlabel","Fermi-Dirac Factor");
     
    8381    p1.propstr("yunits","eV");
    8482
    85     // add the plot to the library
    86     lib.put(p1);
    87 
    88 
    89     // or do it the harder way,
    90     // create a curve to add to the library
    91     // c1->axis("xaxis","xlabel","xdesc","units","scale",dataArr,nPts);
    92 
    93     Rappture::Curve *c1 = new Rappture::Curve("output.curve(FDF)");
    94     c1->label(curveLabel);
    95     c1->desc(curveDesc);
    96     c1->axis("xaxis","Fermi-Dirac Factor","","","",fArr,nPts);
    97     c1->axis("yaxis","Energy","","eV","",EArr,nPts);
    98 
    99     // add the curve to the library
    100     lib.put(c1);
    101 
    10283    lib.result();
    10384
    10485    // clean up memory
    10586    delete lib;
     87    delete T;
     88    delete Ef;
    10689    delete p1;
    107     delete c1;
    10890
    10991    return 0;
  • trunk/examples/objects/curve/curve.cc

    r1528 r1560  
    11#include <iostream>
    22#include "RpCurve.h"
     3
     4size_t indent = 0;
     5size_t tabstop = 4;
    36
    47int test(
     
    3437    }
    3538
    36     std::cout << "xml: " << n->xml() << std::endl;
     39    std::cout << "xml: " << n->xml(indent,tabstop) << std::endl;
    3740    return 0;
    3841}
  • trunk/examples/objects/histogram/histogram.cc

    r1528 r1560  
    3636    h3->yaxis("ylabel3","ydesc3","yunits3");
    3737
     38    size_t indent = 0;
     39    size_t tabstop = 4;
    3840
    39     std::printf("xml: %s\n",h1->xml());
    40     std::printf("xml: %s\n",h2->xml());
    41     std::printf("xml: %s\n",h3->xml());
     41    std::printf("xml: %s\n",h1->xml(indent,tabstop));
     42    std::printf("xml: %s\n",h2->xml(indent,tabstop));
     43    std::printf("xml: %s\n",h3->xml(indent,tabstop));
    4244
    4345    return 0;
  • trunk/examples/objects/number/Makefile.in

    r1386 r1560  
    3434FILES   = \
    3535                $(srcdir)/number.cc \
     36                $(srcdir)/number_1_0_in.xml \
    3637                Makefile
    3738
  • trunk/examples/objects/number/number.cc

    r1528 r1560  
    11#include <iostream>
     2#include <errno.h>
     3#include <fstream>
     4#include <sys/stat.h>
    25#include "RpNumber.h"
     6
     7size_t indent = 0;
     8size_t tabstop = 4;
    39
    410int
    511printNumber(Rappture::Number *n)
    612{
     13    std::cout << "name: " << n->name() << std::endl;
    714    std::cout << "path: " << n->path() << std::endl;
    815    std::cout << "label: " << n->label() << std::endl;
     
    1320    std::cout << "max: " << n->max() << std::endl;
    1421    std::cout << "units: " << n->units() << std::endl;
    15     std::cout << "xml: " << n->xml() << std::endl;
    16     return 0;
     22    std::cout << "xml:\n" << n->xml(indent,tabstop) << std::endl;
     23    return 0;
     24}
     25
     26int testStringVal(
     27    const char *testname,
     28    const char *desc,
     29    const char *expected,
     30    const char *received)
     31{
     32    if ((!expected && received) ||
     33        (expected && !received) ||
     34        (expected && received && strcmp(expected,received) != 0)) {
     35        printf("Error: %s\n", testname);
     36        printf("\t%s\n", desc);
     37        printf("\texpected \"%s\"\n",expected);
     38        printf("\treceived \"%s\"\n",received);
     39        return 1;
     40    }
     41    return 0;
     42}
     43
     44int testDoubleVal(
     45    const char *testname,
     46    const char *desc,
     47    double expected,
     48    double received)
     49{
     50    if (expected != received) {
     51        printf("Error: %s\n", testname);
     52        printf("\t%s\n", desc);
     53        printf("\texpected \"%g\"\n",expected);
     54        printf("\treceived \"%g\"\n",received);
     55        return 1;
     56    }
     57    return 0;
     58}
     59
     60size_t
     61readFile (
     62    const char *filePath,
     63    const char **buf)
     64{
     65    if (buf == NULL) {
     66        fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath);
     67        return 0;
     68    }
     69
     70    FILE *f;
     71    f = fopen(filePath, "rb");
     72    if (f == NULL) {
     73        fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno));
     74        return 0;
     75    }
     76    struct stat stat;
     77    if (fstat(fileno(f), &stat) < 0) {
     78        fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno));
     79        return 0;
     80    }
     81    off_t size;
     82    size = stat.st_size;
     83    char* memblock;
     84    memblock = new char [size+1];
     85    if (memblock == NULL) {
     86        fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s",
     87            size, filePath, strerror(errno));
     88        fclose(f);
     89        return 0;
     90    }
     91
     92    size_t nRead;
     93    nRead = fread(memblock, sizeof(char), size, f);
     94    fclose(f);
     95
     96    if (nRead != (size_t)size) {
     97        fprintf(stderr,"can't read %zu bytes from \"%s\": %s", size, filePath,
     98            strerror(errno));
     99        return 0;
     100    }
     101
     102    memblock[size] = '\0';
     103    *buf = memblock;
     104    return nRead;
     105}
     106int number_0_0 ()
     107{
     108    const char *testdesc = "test number full constructor";
     109    const char *testname = "number_0_0";
     110    int retVal = 0;
     111
     112    const char *name = "temp";
     113    const char *units = "K";
     114    double def = 300;
     115    double min = 0;
     116    double max = 1000;
     117    const char *label = "mylabel";
     118    const char *desc = "mydesc";
     119
     120    Rappture::Number *n = NULL;
     121
     122    n = new Rappture::Number(name,units,def,min,max,label,desc);
     123
     124    if (n == NULL) {
     125        printf("Error: %s\n", testname);
     126        printf("\t%s\n", testdesc);
     127        printf("\terror creating number object\n");
     128        retVal = 1;
     129    }
     130
     131    retVal |= testStringVal(testname,testdesc,name,n->name());
     132    retVal |= testStringVal(testname,testdesc,units,n->units());
     133    retVal |= testDoubleVal(testname,testdesc,def,n->def());
     134    retVal |= testDoubleVal(testname,testdesc,min,n->min());
     135    retVal |= testDoubleVal(testname,testdesc,max,n->max());
     136    retVal |= testStringVal(testname,testdesc,label,n->label());
     137    retVal |= testStringVal(testname,testdesc,desc,n->desc());
     138
     139    if (n != NULL) {
     140        delete n;
     141    }
     142
     143    return retVal;
     144}
     145
     146int number_0_1 ()
     147{
     148    const char *testdesc = "test number constuctor";
     149    const char *testname = "number_0_0";
     150    int retVal = 0;
     151
     152    const char *name = "eV";
     153    const char *units = "eV";
     154    double def = 1.4;
     155
     156    Rappture::Number *n = NULL;
     157
     158    n = new Rappture::Number(name,units,def);
     159
     160    if (n == NULL) {
     161        printf("Error: %s\n", testname);
     162        printf("\t%s\n", testdesc);
     163        printf("\terror creating number object\n");
     164        retVal = 1;
     165    }
     166
     167    retVal |= testStringVal(testname,testdesc,name,n->name());
     168    retVal |= testStringVal(testname,testdesc,units,n->units());
     169    retVal |= testDoubleVal(testname,testdesc,def,n->def());
     170
     171    if (n != NULL) {
     172        delete n;
     173    }
     174
     175    return retVal;
     176}
     177
     178int number_1_0 ()
     179{
     180    const char *testdesc = "test constructing number from xml";
     181    const char *testname = "number_1_0";
     182    const char *filePath = "number_1_0_in.xml";
     183    int retVal = 0;
     184
     185    const char *buf = NULL;
     186    readFile(filePath,&buf);
     187
     188    const char *name = "Ef";
     189    const char *units = "eV";
     190    double def = 0;
     191    double cur = 5;
     192    double min = -10;
     193    double max = 10;
     194    const char *label = "Fermi Level";
     195    const char *desc = "Energy at center of distribution.";
     196
     197    Rappture::Number n;
     198
     199    n.xml(buf);
     200
     201    retVal |= testStringVal(testname,testdesc,name,n.name());
     202    retVal |= testStringVal(testname,testdesc,units,n.units());
     203    retVal |= testDoubleVal(testname,testdesc,def,n.def());
     204    retVal |= testDoubleVal(testname,testdesc,cur,n.cur());
     205    retVal |= testDoubleVal(testname,testdesc,min,n.min());
     206    retVal |= testDoubleVal(testname,testdesc,max,n.max());
     207    retVal |= testStringVal(testname,testdesc,label,n.label());
     208    retVal |= testStringVal(testname,testdesc,desc,n.desc());
     209
     210    return retVal;
    17211}
    18212
    19213int main()
    20214{
    21     Rappture::Number *n = NULL;
    22 
    23     n = new Rappture::Number("input.number(temperature)","K",300,
    24                              0,1000,"mylabel","mydesc");
    25 
    26     printNumber(n);
    27 
    28     Rappture::Number n1("input.number(eV)","eV",1.4);
    29     printNumber(&n1);
    30 
    31     delete n;
    32 
    33     return 0;
    34 }
     215    number_0_0();
     216    number_0_1();
     217    number_1_0();
     218
     219    return 0;
     220}
  • trunk/examples/objects/path/path.cc

    r1528 r1560  
    88    const char *received)
    99{
    10     if (strcmp(expected,received) != 0) {
     10    if ((!expected && received) ||
     11        (expected && !received) ||
     12        (expected && received && strcmp(expected,received) != 0)) {
    1113        printf("Error: %s\n", testname);
    1214        printf("\t%s\n", desc);
     
    114116
    115117    const char *path = "input.group(tabs).number(mynum).current";
    116     const char *expected = "";
     118    const char *expected = NULL;
    117119    const char *received = NULL;
    118120
     
    773775// FIXME: add test for parent get/set functions
    774776// FIXME: add test for path get/set functions
     777// FIXME: add test for changing the curr location
     778//        using first,last,next,prev and checking
     779//        comp, type, parent,id fxns
     780// FIXME: add test for numbered paths
     781//        input.number2
     782//        input.number2(eee)
     783//        input.number(eee)2
    775784
    776785int main()
  • trunk/examples/objects/plot/plot.cc

    r1528 r1560  
    4141    std::printf("curveCnt = %zu\n",curveCnt);
    4242
    43     std::printf("xml: %s\n",p1->xml());
     43    size_t indent = 0;
     44    size_t tabstop = 4;
     45
     46    std::printf("xml: %s\n",p1->xml(indent,tabstop));
    4447
    4548/*
  • trunk/examples/objects/scatter/scatter.cc

    r1528 r1560  
    4141    std::printf("curveCnt = %zu\n",curveCnt);
    4242
    43     std::printf("xml: %s\n",p1->xml());
     43    size_t indent = 0;
     44    size_t tabstop = 4;
     45    std::printf("xml: %s\n",p1->xml(indent,tabstop));
    4446
    4547/*
  • trunk/examples/objects/tree/tree.c

    r1528 r1560  
    1414    Rp_Tree t;
    1515
    16     Rp_TreeCreate(treeName,t);
     16    Rp_TreeCreate(treeName,&t);
    1717    received = Rp_TreeName(t);
    1818
     
    2828}
    2929
     30int tree_1_0 ()
     31{
     32    const char *desc = "test creation of tree node";
     33    const char *testname = "tree_1_0";
     34
     35    const char *treeName = "mytree";
     36    const char *nodeName = "mynode";
     37    const char *expected = "mynode";
     38    const char *received = NULL;
     39
     40    Rp_Tree t;
     41    Rp_TreeNode n = NULL;
     42
     43    Rp_TreeCreate(treeName,&t);
     44    n = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), nodeName, 0);
     45    received = Rp_TreeNodeLabel(n);
     46
     47    if (strcmp(expected,received) != 0) {
     48        printf("Error: %s\n", testname);
     49        printf("\t%s\n", desc);
     50        printf("\texpected \"%s\"\n",expected);
     51        printf("\treceived \"%s\"\n",received);
     52        return 1;
     53    }
     54
     55    return 0;
     56}
     57
     58int tree_2_0 ()
     59{
     60    const char *desc = "test creation of tree node with single child";
     61    const char *testname = "tree_2_0";
     62
     63    const char *treeName = "mytree";
     64    const char *parentNodeName = "myparentnode";
     65    const char *childNodeName = "mychildnode";
     66    const char *expected = "mychildnode";
     67    const char *received = NULL;
     68
     69    Rp_Tree t;
     70    Rp_TreeNode parent = NULL;
     71    Rp_TreeNode child = NULL;
     72
     73    Rp_TreeCreate(treeName,&t);
     74    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
     75    child = Rp_TreeCreateNode(t, parent, childNodeName, 0);
     76    received = Rp_TreeNodeLabel(child);
     77
     78    if (strcmp(expected,received) != 0) {
     79        printf("Error: %s\n", testname);
     80        printf("\t%s\n", desc);
     81        printf("\texpected \"%s\"\n",expected);
     82        printf("\treceived \"%s\"\n",received);
     83        return 1;
     84    }
     85
     86    return 0;
     87}
     88
     89int tree_2_1 ()
     90{
     91    const char *desc = "test creation of tree node with multiple children";
     92    const char *testname = "tree_2_1";
     93
     94    const char *treeName = "mytree";
     95    const char *parentNodeName = "myparentnode";
     96    size_t expected = 5;
     97    size_t received = 0;
     98
     99    Rp_Tree t;
     100    Rp_TreeNode parent = NULL;
     101
     102    Rp_TreeCreate(treeName,&t);
     103    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
     104
     105    Rp_TreeCreateNode(t, parent, "child1", 0);
     106    Rp_TreeCreateNode(t, parent, "child2", 0);
     107    Rp_TreeCreateNode(t, parent, "child3", 0);
     108    Rp_TreeCreateNode(t, parent, "child4", 0);
     109    Rp_TreeCreateNode(t, parent, "child5", 0);
     110
     111    received = Rp_TreeNodeDegree(parent);
     112
     113    if (expected != received) {
     114        printf("Error: %s\n", testname);
     115        printf("\t%s\n", desc);
     116        printf("\texpected \"%zu\"\n",expected);
     117        printf("\treceived \"%zu\"\n",received);
     118        return 1;
     119    }
     120
     121    return 0;
     122}
     123
     124int tree_2_2 ()
     125{
     126    const char *desc = "test search for child node";
     127    const char *testname = "tree_2_2";
     128
     129    const char *treeName = "mytree";
     130    const char *parentNodeName = "myparentnode";
     131    const char *childNodeName = "mychildnode";
     132    const char *expected = "mychildnode";
     133    const char *received = NULL;
     134
     135    Rp_Tree t;
     136    Rp_TreeNode parent = NULL;
     137    Rp_TreeNode foundChild = NULL;
     138
     139    Rp_TreeCreate(treeName,&t);
     140    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
     141
     142    Rp_TreeCreateNode(t, parent, "child1", 0);
     143    Rp_TreeCreateNode(t, parent, "child2", 0);
     144    Rp_TreeCreateNode(t, parent, "child3", 0);
     145    Rp_TreeCreateNode(t, parent, childNodeName, 0);
     146    Rp_TreeCreateNode(t, parent, "child4", 0);
     147    Rp_TreeCreateNode(t, parent, "child5", 0);
     148
     149    foundChild = Rp_TreeFindChild(parent,childNodeName);
     150    received = Rp_TreeNodeLabel(foundChild);
     151
     152    if (strcmp(expected,received) != 0) {
     153        printf("Error: %s\n", testname);
     154        printf("\t%s\n", desc);
     155        printf("\texpected \"%s\"\n",expected);
     156        printf("\treceived \"%s\"\n",received);
     157        return 1;
     158    }
     159
     160    return 0;
     161}
     162
     163int tree_3_0 ()
     164{
     165    const char *desc = "test adding node with string value";
     166    const char *testname = "tree_3_0";
     167
     168    const char *treeName = "mytree";
     169    const char *parentNodeName = "myparentnode";
     170    const char *childNodeName = "mychildnode";
     171    const char *childNodeValue = "quick brown fox";
     172    const char *expected = "quick brown fox1";
     173    const char *received = NULL;
     174
     175    Rp_Tree t;
     176    Rp_TreeNode parent = NULL;
     177    Rp_TreeNode child = NULL;
     178
     179    Rp_TreeCreate(treeName,&t);
     180    parent = Rp_TreeCreateNode(t, Rp_TreeRootNode(t), parentNodeName, 0);
     181
     182    child = Rp_TreeCreateNode(t, parent, childNodeName, 0);
     183    Rp_TreeSetValue(t,child,"value",(void *)childNodeValue);
     184
     185    Rp_TreeGetValue(t,child,"value",(void **)&received);
     186
     187    if (strcmp(expected,received) != 0) {
     188        printf("Error: %s\n", testname);
     189        printf("\t%s\n", desc);
     190        printf("\texpected \"%s\"\n",expected);
     191        printf("\treceived \"%s\"\n",received);
     192        return 1;
     193    }
     194
     195    return 0;
     196}
     197
    30198int main()
    31199{
    32200    tree_0_0();
    33 
    34     return 0;
    35 }
     201    tree_1_0();
     202    tree_2_0();
     203    tree_2_1();
     204    tree_2_2();
     205    tree_3_0();
     206
     207    // add test case where two children have the same name but different ids
     208
     209    return 0;
     210}
  • trunk/examples/objects/xmlparser/Makefile.in

    r1528 r1560  
    3434FILES   = \
    3535                $(srcdir)/xmlparser.cc \
     36                $(srcdir)/xmlparser_1_0_in.xml \
     37                $(srcdir)/xmlparser_1_0_out.xml \
     38                $(srcdir)/xmlparser_2_0_in.xml \
    3639                $(srcdir)/tool.xml \
    3740                Makefile
  • trunk/examples/objects/xmlparser/xmlparser.cc

    r1528 r1560  
    1 #include <stdio.h>
    2 #include <expat.h>
    3 #include <string.h>
    4 #include "RpInt.h"
    5 #include "RpChain.h"
    6 #include "RpPath.h"
    7 #include "RpNumber.h"
    8 
    9 #define BUFFSIZE        8192
    10 
    11 char Buff[BUFFSIZE];
    12 
    13 typedef struct {
    14     int skip;
    15     int depth;
    16     Rappture::Path *path;
    17     Rp_Chain *c;
    18     void *lastObj;
    19     int lastObjType;
    20     Rappture::Object *v;
    21 } Parseinfo;
    22 
    23 void
    24 init_info(Parseinfo *info) {
    25     info->skip = 0;
    26     info->depth = 1;
    27     info->path = new Rappture::Path();
    28     info->c = Rp_ChainCreate();
    29     info->lastObj = NULL;
    30     info->lastObjType = 0;
    31     info->v = NULL;
    32 }
    33 
    34 int
    35 should_skip(Parseinfo *inf, const char *el, const char **attr) {
    36     int skip = 0;
    37 
    38     if ( (*el == 's') && (strcmp("string",el) == 0) ) {
    39         skip = 1;
    40     }
    41 
    42     return skip;
    43 }
    44 
    45 static void XMLCALL
    46 start(void *data, const char *el, const char **attr)
    47 {
    48     Parseinfo *inf = (Parseinfo *) data;
    49 
    50     if ( (*el == 'i') && (strcmp("input",el) == 0) ) {
    51         inf->path->add(el);
    52     } else if ( (*el == 'n') && (strcmp("number",el) == 0) ) {
    53         inf->path->add(el);
    54         // null units kills number
    55         // Rappture::Number *n = new Rappture::Number(inf->path->path(NULL),NULL,0.0);
    56         Rappture::Number *n = new Rappture::Number(inf->path->path(),"",0.0);
    57         Rp_ChainAppend(inf->c,n);
    58         inf->v = n;
    59     } else {
    60         // add the tags to the hash table of the last object we created.
    61         // this means new objects must be declared here,
    62         // or they will become part of the last recognized object
    63         // this could lead to major problems.
    64         // we have no way to ignore stuff we don't recognize
    65 
    66         inf->path->add(el);
    67         if (inf->v) {
    68             // inf->v->property(el,);
    69         }
    70     }
    71 
    72     inf->depth++;
    73 }
    74 
    75 static void XMLCALL
    76 end(void *data, const char *el)
    77 {
    78     Parseinfo *inf = (Parseinfo *) data;
    79     inf->path->del();
    80     inf->depth--;
    81     // pop object off the end of the inf->c chain
    82     // add object to the tree object
    83 }
    84 
    85 void
    86 rawstart(void *data, const char *el, const char **attr) {
    87     Parseinfo *inf = (Parseinfo *) data;
    88 
    89     if (! inf->skip) {
    90         if (should_skip(inf, el, attr)) {
    91             inf->skip = inf->depth;
    92         }
    93         else {
    94             start(inf, el, attr);     /* This does rest of start handling */
    95         }
    96     }
    97 
    98     inf->depth++;
    99 }  /* End of rawstart */
    100 
    101 void
    102 rawend(void *data, const char *el) {
    103     Parseinfo *inf = (Parseinfo *) data;
    104 
    105     inf->depth--;
    106 
    107     if (! inf->skip) {
    108         end(inf, el);              /* This does rest of end handling */
    109     }
    110 
    111     if (inf->skip == inf->depth) {
    112         inf->skip = 0;
    113     }
    114 }  /* End rawend */
    115 
    116 /*
    117 void
    118 char_handler(void* data, XML_Char const* s, int len)
    119 {
    120     int total = 0;
    121     int total_old = 0;
    122     scew_element* current = NULL;
    123     Parseinfo *inf = (Parseinfo *) data;
    124 
    125     if (inf == NULL) {
    126         return;
    127     }
    128 
    129     if (inf->v == NULL)
    130     {
    131         return;
    132     }
    133 
    134     if (current->contents != NULL)
    135     {
    136         total_old = scew_strlen(current->contents);
    137     }
    138     total = (total_old + len + 1) * sizeof(XML_Char);
    139     current->contents = (XML_Char*) realloc(current->contents, total);
    140 
    141     if (total_old == 0)
    142     {
    143         current->contents[0] = '\0';
    144     }
    145 
    146     scew_strncat(current->contents, s, len);
    147 }
    148 */
    149 
    150 
    151 int
    152 main(int argc, char *argv[])
    153 {
    154     Parseinfo info;
    155     XML_Parser p = XML_ParserCreate(NULL);
    156     if (! p) {
    157         fprintf(stderr, "Couldn't allocate memory for parser\n");
    158         exit(-1);
    159     }
    160 
    161     init_info(&info);
    162     XML_SetUserData(p,&info);
    163 
    164     XML_SetElementHandler(p, rawstart, rawend);
    165     //XML_SetDefaultHandler(p, char_handler);
    166 
    167     for (;;) {
    168         int done;
    169         int len;
    170 
    171         len = fread(Buff, 1, BUFFSIZE, stdin);
    172         if (ferror(stdin)) {
    173             fprintf(stderr, "Read error\n");
    174             exit(-1);
    175         }
    176         done = feof(stdin);
    177 
    178         if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
    179             fprintf(stderr, "Parse error at line %lu:\n%s\n",
    180                     XML_GetCurrentLineNumber(p),
    181                     XML_ErrorString(XML_GetErrorCode(p)));
    182             exit(-1);
    183         }
    184 
    185         if (done) {
    186             break;
    187         }
    188     }
    189 
    190     Rp_ChainLink *lv = NULL;
    191     Rappture::Number *n = NULL;
    192     lv = Rp_ChainFirstLink(info.c);
    193     while (lv) {
    194         n = (Rappture::Number *) Rp_ChainGetValue(lv);
    195         printf("%s\n",n->xml());
    196         lv = Rp_ChainNextLink(lv);
    197     }
     1#include <iostream>
     2#include <errno.h>
     3#include <fstream>
     4#include <sys/stat.h>
     5#include "RpParserXML.h"
     6
     7size_t
     8readFile (
     9    const char *filePath,
     10    const char **buf)
     11{
     12    if (buf == NULL) {
     13        fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath);
     14        return 0;
     15    }
     16
     17    FILE *f;
     18    f = fopen(filePath, "rb");
     19    if (f == NULL) {
     20        fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno));
     21        return 0;
     22    }
     23    struct stat stat;
     24    if (fstat(fileno(f), &stat) < 0) {
     25        fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno));
     26        return 0;
     27    }
     28    off_t size;
     29    size = stat.st_size;
     30    char* memblock;
     31    memblock = new char [size+1];
     32    if (memblock == NULL) {
     33        fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s",
     34            size, filePath, strerror(errno));
     35        fclose(f);
     36        return 0;
     37    }
     38
     39    size_t nRead;
     40    nRead = fread(memblock, sizeof(char), size, f);
     41    fclose(f);
     42
     43    if (nRead != (size_t)size) {
     44        fprintf(stderr,"can't read %zu bytes from \"%s\": %s", size, filePath,
     45            strerror(errno));
     46        return 0;
     47    }
     48
     49    memblock[size] = '\0';
     50    *buf = memblock;
     51    return nRead;
     52}
     53
     54int xmlparser_0_0 ()
     55{
     56    const char *desc = "test creating xml parser";
     57    const char *testname = "xmlparser_0_0";
     58    int retVal = 0;
     59
     60    const char *expected = "initialized ParserXml Object";
     61    const char *received = NULL;
     62
     63    Rp_ParserXml *p = NULL;
     64
     65    p = Rp_ParserXmlCreate();
     66
     67    if (p == NULL) {
     68        printf("Error: %s\n", testname);
     69        printf("\t%s\n", desc);
     70        printf("\texpected \"%s\"\n",expected);
     71        printf("\treceived \"%s\"\n",received);
     72        retVal = 1;
     73    }
     74
     75    if (p) {
     76        Rp_ParserXmlDestroy(&p);
     77    }
     78
     79    return retVal;
     80}
     81
     82int xmlparser_1_0 ()
     83{
     84    const char *desc = "test sending xml text to xml parser";
     85    const char *testname = "xmlparser_1_0";
     86    const char *infile = "xmlparser_1_0_in.xml";
     87    const char *outfile = "xmlparser_1_0_out.xml";
     88    int retVal = 0;
     89
     90    const char *xmltext = NULL;
     91    const char *expected = NULL;
     92
     93    readFile(infile,&xmltext);
     94    readFile(outfile,&expected);
     95
     96    const char *received = NULL;
     97
     98    Rp_ParserXml *p = NULL;
     99
     100    p = Rp_ParserXmlCreate();
     101
     102    Rp_ParserXmlParse(p, xmltext);
     103
     104    received = Rp_ParserXmlXml(p);
     105
     106
     107    if (strcmp(expected,received) != 0) {
     108        printf("Error: %s\n", testname);
     109        printf("\t%s\n", desc);
     110        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
     111        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
     112        retVal = 1;
     113    }
     114
     115    if (p) {
     116        Rp_ParserXmlDestroy(&p);
     117    }
     118
     119    return retVal;
     120}
     121
     122
     123int xmlparser_2_0 ()
     124{
     125    const char *desc = "test printing xml as path/val combos";
     126    const char *testname = "xmlparser_2_0";
     127    const char *infile = "xmlparser_2_0_in.xml";
     128    int retVal = 0;
     129
     130    const char *xmltext = NULL;
     131
     132    readFile(infile,&xmltext);
     133
     134    const char *expected = "\
     135run.tool.about Press Simulate to view results.\n\
     136run.tool.command tclsh @tool/fermi.tcl @driver\n\
     137run.input.number(Ef).about.label Fermi Level\n\
     138run.input.number(Ef).about.description Energy at center of distribution.\n\
     139run.input.number(Ef).units eV\n\
     140run.input.number(Ef).min -10eV\n\
     141run.input.number(Ef).max 10eV\n\
     142run.input.number(Ef).default 0eV\n\
     143run.input.number(Ef).current 0eV\n";
     144
     145    const char *received = NULL;
     146
     147    Rp_ParserXml *p = NULL;
     148
     149    p = Rp_ParserXmlCreate();
     150
     151    Rp_ParserXmlParse(p, xmltext);
     152
     153    received = Rp_ParserXmlPathVal(p);
     154
     155
     156    if (strcmp(expected,received) != 0) {
     157        printf("Error: %s\n", testname);
     158        printf("\t%s\n", desc);
     159        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
     160        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
     161        retVal = 1;
     162    }
     163
     164    if (p) {
     165        Rp_ParserXmlDestroy(&p);
     166    }
     167
     168    return retVal;
     169}
     170
     171int xmlparser_3_0 ()
     172{
     173    const char *desc = "test getting xml value";
     174    const char *testname = "xmlparser_3_0";
     175    const char *infile = "xmlparser_2_0_in.xml";
     176    int retVal = 0;
     177
     178    const char *xmltext = NULL;
     179
     180    readFile(infile,&xmltext);
     181
     182    const char *expected = "0eV";
     183
     184    const char *received = NULL;
     185
     186    Rp_ParserXml *p = NULL;
     187
     188    p = Rp_ParserXmlCreate();
     189
     190    Rp_ParserXmlParse(p, xmltext);
     191
     192    received = Rp_ParserXmlGet(p,"input.number(Ef).current");
     193
     194
     195    if (strcmp(expected,received) != 0) {
     196        printf("Error: %s\n", testname);
     197        printf("\t%s\n", desc);
     198        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
     199        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
     200        retVal = 1;
     201    }
     202
     203    if (p) {
     204        Rp_ParserXmlDestroy(&p);
     205    }
     206
     207    return retVal;
     208}
     209
     210int xmlparser_4_0 ()
     211{
     212    const char *desc = "test putting xml value";
     213    const char *testname = "xmlparser_4_0";
     214    const char *infile = "xmlparser_2_0_in.xml";
     215    int retVal = 0;
     216
     217    const char *xmltext = NULL;
     218
     219    readFile(infile,&xmltext);
     220
     221    const char *expected = "hi mom";
     222
     223    const char *received = NULL;
     224
     225    Rp_ParserXml *p = NULL;
     226
     227    p = Rp_ParserXmlCreate();
     228
     229    Rp_ParserXmlParse(p, xmltext);
     230
     231    Rp_ParserXmlPut(p,"input.number(gg).current","hi mom",0);
     232    received = Rp_ParserXmlGet(p,"input.number(gg).current");
     233
     234    if (strcmp(expected,received) != 0) {
     235        printf("Error: %s\n", testname);
     236        printf("\t%s\n", desc);
     237        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
     238        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
     239        retVal = 1;
     240    }
     241
     242    if (p) {
     243        Rp_ParserXmlDestroy(&p);
     244    }
     245
     246    return retVal;
     247}
     248
     249int xmlparser_5_0 ()
     250{
     251    const char *desc = "test printing xml with degrees of presets";
     252    const char *testname = "xmlparser_5_0";
     253    const char *infile = "xmlparser_5_0_in.xml";
     254    int retVal = 0;
     255
     256    const char *xmltext = NULL;
     257
     258    readFile(infile,&xmltext);
     259
     260    const char *expected = "\
     261run.tool.about Press Simulate to view results.\n\
     262run.tool.command tclsh @tool/fermi.tcl @driver\n\
     263run.input.number(Ef).about.label Fermi Level\n\
     264run.input.number(Ef).about.description Energy at center of distribution.\n\
     265run.input.number(Ef).units eV\n\
     266run.input.number(Ef).min -10eV\n\
     267run.input.number(Ef).max 10eV\n\
     268run.input.number(Ef).default 0eV\n\
     269run.input.number(Ef).current 0eV\n";
     270
     271    const char *received = NULL;
     272
     273    Rp_ParserXml *p = NULL;
     274
     275    p = Rp_ParserXmlCreate();
     276
     277    Rp_ParserXmlParse(p, xmltext);
     278
     279    received = Rp_ParserXmlPathVal(p);
     280
     281
     282    if (strcmp(expected,received) != 0) {
     283        printf("Error: %s\n", testname);
     284        printf("\t%s\n", desc);
     285        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
     286        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
     287        retVal = 1;
     288    }
     289
     290    if (p) {
     291        Rp_ParserXmlDestroy(&p);
     292    }
     293
     294    return retVal;
     295}
     296
     297// FIXME: test what happens when parser sees self closing tag <tag/>
     298// FIXME: test get function
     299// FIXME: test put function
     300// FIXME: look into why Rp_ParserXmlPathVal hits some nodes twice in gdb
     301
     302int main()
     303{
     304    xmlparser_0_0();
     305    xmlparser_1_0();
     306    xmlparser_2_0();
     307    xmlparser_3_0();
     308    xmlparser_4_0();
     309    xmlparser_5_0();
    198310
    199311    return 0;
Note: See TracChangeset for help on using the changeset viewer.