source: trunk/src/core/RpLibrary.cc @ 597

Last change on this file since 597 was 597, checked in by dkearney, 14 years ago

adjusted core makefile to not remove setup.py when doing a make clean
adjusted putFile and putData commands and flags in matlab, octave,fortran,perl
changed the RPLIB_[TEXT,BINARY] flags to RPLIB_COMPRESS and RPLIB_NO_COMPRESS flags
adjusted the c-example for compress to incorporate newly named flags.
adjusted rappture library header to find rappture buffer header files in rappture2 directory.

  • Property svn:keywords set to Date Rev URL
File size: 50.2 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2004-2007  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include "RpLibrary.h"
15#include "RpEntityRef.h"
16
17static RpEntityRef ERTranslator;
18
19/**********************************************************************/
20// METHOD: _get_attribute()
21/// Return the attribute value matching the provided attribute name.
22/**
23 */
24
25std::string
26RpLibrary::_get_attribute(scew_element* element, std::string attributeName)
27{
28    scew_attribute* attribute = NULL;
29    std::string attrVal;
30    // int attrValSize = 0;
31
32    if (element != NULL)
33    {
34        if (scew_attribute_count(element) > 0) {
35
36            while((attribute=scew_attribute_next(element, attribute)) != NULL)
37            {
38                if (    strcmp( scew_attribute_name(attribute),
39                                attributeName.c_str()) == 0     ){
40                    attrVal = scew_attribute_value(attribute);
41                }
42            }
43        }
44        else {
45            // there are no attributes, count == 0
46        }
47    }
48
49    return attrVal;
50}
51
52/**********************************************************************/
53// METHOD: _path2list()
54/// Convert a path into a list of element names.
55/**
56 */
57
58int
59RpLibrary::_path2list (std::string& path, std::string** list, int listLen)
60{
61    std::string::size_type pos = 0;
62    std::string::size_type start = 0;
63    std::string::size_type end = path.length();
64    int index = 0;
65    int retVal = 0;
66    unsigned int parenDepth = 0;
67
68    for (   pos = 0; (pos < end) && (index < listLen); pos++) {
69        if (path[pos] == '(') {
70            parenDepth++;
71            continue;
72        }
73
74        if (path[pos] == ')') {
75            parenDepth--;
76            continue;
77        }
78
79        if ( (path[pos] == '.') && (parenDepth == 0) ) {
80            list[index] = new std::string(path.substr(start,pos-start));
81            index++;
82            start = pos + 1;
83        }
84    }
85
86    // add the last path to the list
87    // error checking for path names like p1.p2.
88    if ( (start < end) && (pos == end) ) {
89        list[index] = new std::string(path.substr(start,pos-start));
90    }
91    retVal = index;
92    index++;
93
94    // null out the rest of the pointers so we know where to stop free'ing
95    while (index < listLen) {
96        list[index++] = NULL;
97    }
98
99    return retVal;
100}
101
102/**********************************************************************/
103// METHOD: _node2name()
104/// Retrieve the id of a node.
105/**
106 */
107
108std::string
109RpLibrary::_node2name (scew_element* node)
110{
111    // XML_Char const* name = _get_attribute(node,"id");
112    std::string name = _get_attribute(node,"id");
113    std::stringstream retVal;
114    XML_Char const* type = NULL;
115    scew_element* parent = NULL;
116    scew_element** siblings = NULL;
117    unsigned int count = 0;
118    int tmpCount = 0;
119    int index = 0;
120    std::string indexStr;
121
122    type = scew_element_name(node);
123    parent = scew_element_parent(node);
124
125    if (parent) {
126
127        // if (name == NULL) {
128        if (name.empty()) {
129            siblings = scew_element_list(parent, type, &count);
130            if (count > 0) {
131                tmpCount = count;
132                while ((index < tmpCount) && (siblings[index] != node)) {
133                    index++;
134                }
135
136                if (index < tmpCount) {
137
138                    if (index > 0) {
139
140                        retVal << type << --index;
141                    }
142                    else {
143
144                        retVal << type;
145                    }
146
147                    /*
148                    if (retVal == NULL) {
149                        // error with allocating space
150                        return NULL;
151                    }
152                    */
153                }
154            }
155
156            scew_element_list_free(siblings);
157
158        }
159        else {
160
161            retVal << name;
162        }
163    }
164
165    return (retVal.str());
166}
167
168/**********************************************************************/
169// METHOD: _node2comp()
170/// Retrieve the component name of a node
171/**
172 */
173
174std::string
175RpLibrary::_node2comp (scew_element* node)
176{
177    // XML_Char const* name = _get_attribute(node,"id");
178    std::string id = _get_attribute(node,"id");
179    std::stringstream retVal;
180    XML_Char const* type = NULL;
181    scew_element* parent = NULL;
182    scew_element** siblings = NULL;
183    unsigned int count = 0;
184    int tmpCount = 0;
185    int index = 0;
186    std::string indexStr;
187
188    type = scew_element_name(node);
189    parent = scew_element_parent(node);
190
191    if (parent) {
192        if (id.empty()) {
193            siblings = scew_element_list(parent, type, &count);
194            if (count > 0) {
195                tmpCount = count;
196                // figure out what the index value should be
197                while ((index < tmpCount) && (siblings[index] != node)) {
198                    index++;
199                }
200
201                if (index < tmpCount) {
202                    if (index > 0) {
203                        // retVal << type << --index;
204                        retVal << type << index;
205                    }
206                    else {
207                        retVal << type;
208                    }
209                }
210
211            }
212            else {
213                // count == 0 ??? this state should never be reached
214            }
215            scew_element_list_free(siblings);
216
217        }
218        else {
219            // node has attribute id
220            retVal << type << "(" << id << ")";
221
222        }
223    }
224
225    return (retVal.str());
226}
227
228/**********************************************************************/
229// METHOD: _node2path()
230/// Retrieve the full path name of a node
231/**
232 */
233
234std::string
235RpLibrary::_node2path (scew_element* node)
236{
237
238    std::stringstream path;
239    scew_element* snode = node;
240    scew_element* parent = NULL;
241
242    if (snode) {
243        parent = scew_element_parent(snode);
244        path.clear();
245
246        while (snode && parent) {
247
248            if (!path.str().empty()) {
249                path.str(_node2comp(snode) + "." + path.str());
250                // path.str("." + _node2comp(snode) + path.str());
251            }
252            else {
253                path.str(_node2comp(snode));
254                // path.str("." + _node2comp(snode));
255            }
256
257            snode = scew_element_parent(snode);
258            parent = scew_element_parent(snode);
259        }
260    }
261
262    return (path.str());
263}
264
265/**********************************************************************/
266// METHOD: _splitPath()
267/// Split a path (component) name into its tag name, index, and id
268/**
269 */
270
271int
272RpLibrary::_splitPath (std::string& path, std::string& tagName, int* idx, std::string& id )
273{
274    int stop = 0;
275    int start = 0;
276    int index = path.length();
277
278    if (index) {
279        index--;
280    }
281
282    if (!path.empty()) {
283        if (path[index] == ')') {
284            stop = index;
285            while (path[index] != '(') {
286                index--;
287            }
288            start = index+1;
289            // strncpy(id,path+start,stop-start);
290            // id = new std::string(path.substr(start,stop-start));
291            id = path.substr(start,stop-start);
292            index--;
293        }
294        if (isdigit(path[index])) {
295            stop = index;
296            while (isdigit(path[index])) {
297                index--;
298            }
299            // sscanf(path[index+1],"%d",idx);
300            sscanf(path.c_str()+index+1,"%d",idx);
301        }
302        if (isalpha(path[index])) {
303            start = 0;
304            stop = index+1;
305            // tagName = new std::string(path.substr(start,stop-start));
306            tagName = path.substr(start,stop-start);
307            // strncpy(tagName,path+start,stop-start);
308        }
309    }
310    else {
311        tagName = "";
312        *idx = 0;
313        id = "";
314    }
315
316    return 1;
317}
318
319/**********************************************************************/
320// METHOD: _find()
321/// Find or create a node and return it.
322/**
323 */
324
325scew_element*
326RpLibrary::_find(std::string path, int create)
327{
328    // std::string* tagName;
329    // std::string* id;
330    std::string tagName = "";
331    std::string id = "";
332    int index = 0;
333    int listLen = (path.length()/2)+1;
334    std::string** list;
335    int path_size = 0;
336    int listIdx = 0;
337    unsigned int count = 0;
338    int tmpCount = 0;
339    int lcv = 0;
340    std::string tmpId;
341
342    scew_element* tmpElement = this->root;
343    scew_element* node = NULL;
344    scew_element** eleList = NULL;
345
346
347    if (path.empty()) {
348        //user gave an empty path
349        // return this;
350        return tmpElement;
351    }
352
353    list = (std::string **) calloc(listLen, sizeof( std::string * ) );
354
355    if (!list) {
356        // error calloc'ing space for list
357        return NULL;
358    }
359
360    path_size = _path2list (path,list,listLen);
361
362    while ( (listIdx <= path_size) && (tmpElement != NULL ) ){
363
364        _splitPath(*(list[listIdx]),tagName,&index,id);
365
366        // if (id->empty()) {
367        if (id.empty()) {
368            /*
369            # If the name is like "type2", then look for elements with
370            # the type name and return the one with the given index.
371            # If the name is like "type", then assume the index is 0.
372            */
373
374            // eleList = scew_element_list(tmpElement, tagName->c_str(), &count);
375            eleList = scew_element_list(tmpElement, tagName.c_str(), &count);
376            tmpCount = count;
377            if (index < tmpCount) {
378                node = eleList[index];
379            }
380            else {
381                /* there is no element with the specified index */
382                node = NULL;
383            }
384
385            scew_element_list_free(eleList);
386            eleList = NULL;
387        }
388        else {
389
390            /* what if its like type2(id) ? */
391            /* still unresolved */
392
393            /*
394            # If the name is like "type(id)", then look for elements
395            # that match the type and see if one has the requested name.
396            # if the name is like "(id)", then look for any elements
397            # with the requested name.
398            */
399
400            // eleList = scew_element_list(tmpElement, tagName->c_str(), &count);
401            if (!tagName.empty()) {
402                eleList = scew_element_list(tmpElement, tagName.c_str(), &count);
403            }
404            else {
405                eleList = scew_element_list_all(tmpElement, &count);
406            }
407
408            tmpCount = count;
409            for (lcv = 0; (lcv < tmpCount); lcv++) {
410                tmpId = _get_attribute(eleList[lcv], "id");
411                if (!tmpId.empty()) {
412                    // if (*id == tmpId) {
413                    if (id == tmpId) {
414                        node = eleList[lcv];
415                        break;
416                    }
417                }
418            }
419
420            if (lcv >= tmpCount) {
421                node = NULL;
422            }
423
424            scew_element_list_free(eleList);
425            eleList = NULL;
426
427        }
428
429        if (node == NULL) {
430            if (create == NO_CREATE_PATH) {
431                // break out instead of returning because we still need to
432                // free the list variable
433                // return node;
434                tmpElement = node;
435                break;
436            }
437            else {
438                // create == CREATE_PATH
439                // we should create the rest of the path
440
441                // create the new element
442                // need to figure out how to properly number the new element
443                // node = scew_element_add(tmpElement,tagName->c_str());
444                node = scew_element_add(tmpElement,tagName.c_str());
445                if (! node) {
446                    // a new element was not created
447                }
448
449                // add an attribute and attrValue to the new element
450                // if (id && !id->empty()) {
451                    // scew_element_add_attr_pair(node,"id",id->c_str());
452                if (!id.empty()) {
453                    scew_element_add_attr_pair(node,"id",id.c_str());
454                }
455            }
456        }
457
458
459        // change this so youre not allocating and deallocating so much.
460        // make it static or something.
461        // if (tagName)    { delete (tagName); }
462        // if (id)         { delete (id); }
463        tagName = "";
464        id = "";
465        index = 0;
466        tmpElement = node;
467        listIdx++;
468    }
469
470    // need to free the strings inside of list
471
472    if (list) {
473        for (listIdx = 0; listIdx < listLen; listIdx++) {
474            if (list[listIdx]) {
475                delete(list[listIdx]);
476                list[listIdx] = NULL;
477            }
478        }
479
480        free(list);
481        list = NULL;
482    }
483
484
485    return tmpElement;
486}
487
488
489/**********************************************************************/
490// METHOD: element()
491/// Search the path of a xml tree and return a RpLibrary node.
492/**
493 * It is the user's responsibility to delete the object when
494 * they are finished using it?, else i need to make this static
495 */
496
497RpLibrary*
498RpLibrary::element (std::string path)
499{
500    RpLibrary* retLib = NULL;
501    scew_element* retNode = NULL;
502
503    if (!this->root) {
504        // library doesn't exist, do nothing;
505        return NULL;
506    }
507
508    /*
509    if (path.empty()) {
510        // an empty path returns the current RpLibrary
511        return this;
512    }
513    */
514
515    if (path.empty()) {
516        // this should be a smart pointer,
517        // if someone deletes the original this->root, this object is void
518        // and will be a memory leak.
519        // retNode = this->root;
520        retLib = new RpLibrary(*this);
521    }
522    else {
523        // get the node located at path
524        retNode = _find(path,NO_CREATE_PATH);
525
526        // if the node exists, create a rappture library object for it.
527        if (retNode) {
528            retLib = new RpLibrary( retNode,this->tree );
529        }
530    }
531
532    return retLib;
533}
534
535/**********************************************************************/
536// METHOD: entities()
537/// Search the path of a xml tree and return a list of its entities.
538/**
539 */
540
541std::list<std::string>
542RpLibrary::entities  (std::string path)
543{
544    std::list<std::string> queue;
545    std::list<std::string>::iterator iter;
546    std::list<std::string> retList;
547    std::list<std::string> childList;
548    std::list<std::string>::iterator childIter;
549
550    RpLibrary* ele = NULL;
551    std::string pathBack = "";
552
553    RpLibrary* child = NULL;
554    std::string childType = "";
555    std::string childPath = "";
556    std::string paramsPath = "";
557
558    RpLibrary* cchild = NULL;
559    std::string cchildType = "";
560    std::string cchildPath = "";
561
562    queue.push_back(path);
563    iter = queue.begin();
564
565    while( iter != queue.end() ) {
566        ele = this->element(*iter);
567        child = NULL;
568
569        if ((*iter).empty()) {
570            pathBack = "";
571        }
572        else {
573            pathBack = *iter + ".";
574        }
575
576        while ( ele && (child = ele->children("",child)) != NULL ) {
577            childList.push_back(child->nodeComp());
578            delete child;
579        }
580
581        childIter = childList.begin();
582
583        while (childIter != childList.end()) {
584            child = ele->element(*childIter);
585
586            childType = child->nodeType();
587            childPath = child->nodeComp();
588            if ( (childType == "group") || (childType == "phase") ) {
589                // add this path to the queue for paths to search
590                queue.push_back(pathBack+childPath);
591            }
592            else if (childType == "structure") {
593                // add this path to the return list
594                retList.push_back(pathBack+child->nodeComp());
595
596                // check to see if there is a ".current.parameters" node
597                // if so, add them to the queue list for paths to search
598                paramsPath = "current.parameters";
599                if (child->element(paramsPath) != NULL) {
600                    queue.push_back((pathBack+child->nodeComp()+"."+paramsPath));
601                }
602            }
603            else {
604                // add this path to the return list
605                retList.push_back(pathBack+child->nodeComp());
606
607                // look for embedded groups and phases
608                // add them to the queue list for paths to search
609                cchild = NULL;
610                while ( (cchild = child->children("",cchild)) != NULL ) {
611                    cchildType = cchild->nodeType();
612                    cchildPath = cchild->nodePath();
613                    if ( (cchildType == "group") || (cchildType == "phase") ) {
614                        // add this path to the queue for paths to search
615                        queue.push_back(cchildPath);
616                    }
617                    delete cchild;
618                }
619            }
620
621            childList.erase(childIter);
622            childIter = childList.begin();
623            delete child;
624        }
625
626        queue.erase(iter);
627        iter = queue.begin();
628    }
629
630    return retList;
631}
632
633/**********************************************************************/
634// METHOD: diff()
635/// find the differences between two xml trees.
636/**
637 */
638
639std::list<std::string>
640RpLibrary::diff (RpLibrary* otherLib, std::string path)
641{
642
643    std::list<std::string> thisVal; // two node list of specific entity's value
644    std::list<std::string> otherVal; // two node list of specific entity's value
645
646    std::list<std::string> thisv; // list of this library's entities
647    std::list<std::string>::iterator thisIter;
648
649    std::list<std::string> otherv; // list of other library's entities
650    std::list<std::string>::iterator otherIter;
651
652    std::list<std::string> retList;
653
654    std::string entry = "";
655    std::string thisSpath = "";  // temp string
656    std::string otherSpath = ""; // temp string
657
658    if ( (!this->root) || (!otherLib->root) ) {
659        // library doesn't exist, do nothing;
660        return retList;
661    }
662
663
664    thisv = this->entities(path);
665    otherv = otherLib->entities(path);
666
667    thisIter = thisv.begin();
668
669    while (thisIter != thisv.end() ) {
670        // reset otherIter for a new search.
671        otherIter = otherv.begin();
672        while ( (otherIter != otherv.end()) && (*otherIter != *thisIter) ) {
673            otherIter++;
674        }
675
676        //if (!path.empty()) {
677        //    thisSpath = path + "." + *thisIter;
678        //}
679        //else {
680        //    thisSpath = *thisIter;
681        //}
682        thisSpath = *thisIter;
683
684        if (otherIter == otherv.end()) {
685            // we've reached the end of the search
686            // and did not find anything, mark this as a '-'
687            thisVal = this->value(thisSpath);
688            retList.push_back("-");
689            retList.push_back(thisSpath);
690            retList.push_back(thisVal.front());
691            retList.push_back("");
692        }
693        else {
694
695            //if (!path.empty()) {
696            //    otherSpath = path + "." + *otherIter;
697            //}
698            //else {
699            //    otherSpath = *otherIter;
700            //}
701            otherSpath = *otherIter;
702
703            thisVal = this->value(thisSpath);
704            otherVal = otherLib->value(otherSpath);
705            if (thisVal.back() != otherVal.back()) {
706                // add the difference to the return list
707                retList.push_back("c");
708                retList.push_back(otherSpath);
709                retList.push_back(thisVal.front());
710                retList.push_back(otherVal.front());
711            }
712
713            // remove the last processed value from otherv
714            otherv.erase(otherIter);
715        }
716
717        // increment thisv's iterator.
718        thisIter++;
719    }
720
721    // add any left over values in otherv to the return list
722    otherIter = otherv.begin();
723    while ( otherIter != otherv.end() ) {
724
725        //if (!path.empty()) {
726        //    otherSpath = path + "." + *otherIter;
727        //}
728        //else {
729        //    otherSpath = *otherIter;
730        //}
731        otherSpath = *otherIter;
732
733        otherVal = otherLib->value(otherSpath);
734
735        retList.push_back("+");
736        retList.push_back(otherSpath);
737        retList.push_back("");
738        retList.push_back(otherVal.front());
739
740        otherv.erase(otherIter);
741        otherIter = otherv.begin();
742    }
743
744    return retList;
745}
746
747/**********************************************************************/
748// METHOD: value(path)
749/// Return a 2 element list containing the regular and normalized values.
750/**
751 */
752
753std::list<std::string>
754RpLibrary::value (std::string path)
755{
756    std::list<std::string> retArr;
757
758    std::string raw = "";
759    std::string val = "";
760
761    RpLibrary* ele = NULL;
762    RpLibrary* tele = NULL;
763
764    int childCount = 0;
765    std::stringstream valStr;
766
767    ele = this->element(path);
768
769    if (ele != NULL ) {
770
771        if (ele->nodeType() == "structure") {
772            raw = path;
773            // try to find a label to represent the structure
774            val = ele->get("about.label");
775
776            if (val == "") {
777               val = ele->get("current.about.label");
778            }
779
780            if (val == "") {
781               tele = ele->element("current");
782               if ( (tele != NULL) && (tele->nodeComp() != "") ) {
783                   tele->children("components",NULL,"",&childCount);
784                   valStr << "<structure> with " << childCount  << " components";
785                   val = valStr.str();
786               }
787            }
788
789        }
790        /*
791        else if (ele->nodeType() == "number") {
792            raw = "";
793            retArr[1] = "";
794            if ( (tele = ele->element("current")) != NULL) {
795                raw = tele->get();
796            }
797            else if ( (tele = ele->element("default")) != NULL) {
798                raw = tele->get();
799            }
800            val = raw
801            if ( "" != raw) {
802                // normalize the units
803                units = ele->get("units");
804                if ( "" != units) {
805
806                }
807        }
808        */
809        else {
810            raw = "";
811            if ( (tele = ele->element("current")) != NULL) {
812                raw = tele->get();
813            }
814            else if ( (tele = ele->element("default")) != NULL) {
815                raw = tele->get();
816            }
817            val = raw;
818        }
819    }
820
821    retArr.push_back(raw);
822    retArr.push_back(val);
823
824    return retArr;
825}
826
827/**********************************************************************/
828// METHOD: parent()
829/// Search the path of a xml tree and return its parent.
830/**
831 * It is the user's responsibility to delete the object when
832 * they are finished using it?, else i need to make this static
833 */
834
835RpLibrary*
836RpLibrary::parent (std::string path)
837{
838    RpLibrary* retLib = NULL;
839    std::string parentPath = "";
840    // std::string::size_type pos = 0;
841    scew_element* ele = NULL;
842    scew_element* retNode = NULL;
843
844    if (!this->root) {
845        // library doesn't exist, do nothing;
846        return NULL;
847    }
848
849    if (path.empty()) {
850        // an empty path returns the parent of the current RpLibrary
851        ele = this->root;
852    }
853    else {
854        // else find the node representing the provided path
855        ele = _find(path,NO_CREATE_PATH);
856    }
857
858    if (ele != NULL) {
859        retNode = scew_element_parent(ele);
860        if (retNode) {
861            // allocate a new rappture library object for the node
862            retLib = new RpLibrary( retNode,this->tree );
863        }
864    }
865    else {
866        // path was not found by _find
867    }
868
869
870    return retLib;
871}
872
873/**********************************************************************/
874// METHOD: copy()
875/// Copy the value from fromPath to toPath.
876//  Copy the value from fromObj.fromPath to toPath
877//  of the current rappture library object. This can copy rappture
878//  library elements within or between xml trees.
879/**
880 */
881
882RpLibrary&
883RpLibrary::copy (std::string toPath, std::string fromPath, RpLibrary* fromObj)
884{
885    RpLibrary* value = NULL;
886    // RpLibrary* child = NULL;
887
888    if (!this->root) {
889        // library doesn't exist, do nothing;
890        // need a good way to raise error, and this is not it.
891        return *this;
892    }
893
894    if (fromObj == NULL) {
895        fromObj = this;
896    }
897
898    value = fromObj->element(fromPath);
899
900    if ( !value ) {
901        // need a good way to raise error, and this is not it.
902        return *this;
903    }
904
905    this->put(toPath, value);
906    delete value;
907
908    return (*this);
909
910}
911
912/**********************************************************************/
913// METHOD: children()
914/// Return the next child of the node located at 'path'
915//
916// The lookup is reset when you send a NULL rpChilNode.
917// User is responsible for deleting returned values
918//
919/**
920 */
921
922/*
923RpLibrary*
924RpLibrary::children (   std::string path,
925                        RpLibrary* rpChildNode,
926                        std::string type,
927                        int* childCount)
928{
929    static std::string old_path = "";
930    static RpLibrary* retLib = NULL;
931    int myChildCount = 0;
932    scew_element* parentNode = NULL;
933    scew_element* childNode = NULL;
934    std::string childName = "";
935
936    if (!this->root) {
937        // library doesn't exist, do nothing;
938        return NULL;
939    }
940
941
942    if (path.empty()) {
943        // an empty path uses the current RpLibrary as parent
944        parentNode = this->root;
945    }
946    else {
947        // check to see if this path is the same as the one set
948        // in the last call to this function.
949        // if so, then we dont need to reset the parentNode
950        //
951        // this check is probably more dependent on rpChildNode
952        // because we want to see if the person want to continue
953        // an old search or start from the beginning of the child list
954        //
955        if ( (path.compare(old_path) == 0) && (rpChildNode != NULL) ) {
956            parentNode = NULL;
957        }
958        // we need to search for a new parentNode
959        else {
960            parentNode = _find(path,NO_CREATE_PATH);
961            if (parentNode == NULL) {
962                // node not found
963                // add error code here
964                return NULL;
965            }
966        }
967    }
968
969    old_path = path;
970
971
972    if (rpChildNode) {
973        childNode = rpChildNode->root;
974    }
975
976    if (parentNode) {
977        myChildCount = scew_element_count(parentNode);
978    }
979
980    if (childCount) {
981        *childCount = myChildCount;
982    }
983
984    // clean up old memory
985    delete retLib;
986
987    if ( (childNode = scew_element_next(parentNode,childNode)) ) {
988
989        if (!type.empty()) {
990            childName = scew_element_name(childNode);
991            // we are searching for a specific child name
992            // keep looking till we find a name that matches the type.
993            // if the current name does not match out search type,
994            // grab the next child and check to see if its null
995            // if its not null, get its name and test for type again
996            while (  (type != childName)
997                  && (childNode = scew_element_next(parentNode,childNode)) ) {
998
999                childName = scew_element_name(childNode);
1000            }
1001            if (type == childName) {
1002                // found a child with a name that matches type
1003                retLib = new RpLibrary( childNode,this->tree );
1004            }
1005            else {
1006                // no children with names that match 'type' were found
1007                retLib = NULL;
1008            }
1009        }
1010        else {
1011            retLib = new RpLibrary( childNode,this->tree );
1012        }
1013    }
1014    else {
1015        // somthing happened within scew, get error code and display
1016        // its probable there are no more child elements left to report
1017        retLib = NULL;
1018    }
1019
1020    return retLib;
1021}
1022*/
1023
1024RpLibrary*
1025RpLibrary::children (   std::string path,
1026                        RpLibrary* rpChildNode,
1027                        std::string type,
1028                        int* childCount)
1029{
1030    // this is static for efficency reasons
1031    static std::string old_path = "";
1032    // this was static so user never has to delete the retLib.
1033    // should be replaced by a smart pointer
1034    // static RpLibrary* retLib = NULL;
1035    RpLibrary* retLib = NULL;
1036    int myChildCount = 0;
1037    scew_element* parentNode = NULL;
1038    scew_element* childNode = NULL;
1039    std::string childName = "";
1040
1041    if (!this->root) {
1042        // library doesn't exist, do nothing;
1043        return NULL;
1044    }
1045
1046
1047    // check to see if the last call to this function
1048    // was searching for children of the same path.
1049    if ( (path.compare(old_path) == 0) && (rpChildNode != NULL) ) {
1050        parentNode = NULL;
1051    }
1052    else if (path.empty()) {
1053        // searching for children in a new path.
1054        // an empty path uses the current RpLibrary as parent
1055        parentNode = this->root;
1056    }
1057    else {
1058        // searching for children in a new, non-empty, path.
1059        parentNode = _find(path,NO_CREATE_PATH);
1060        if (parentNode == NULL) {
1061            // node not found
1062            // add error code here
1063            return NULL;
1064        }
1065    }
1066
1067    old_path = path;
1068
1069    if (rpChildNode) {
1070        childNode = rpChildNode->root;
1071    }
1072
1073    if (parentNode) {
1074        myChildCount = scew_element_count(parentNode);
1075        if (childCount) {
1076            *childCount = myChildCount;
1077        }
1078    }
1079
1080    // clean up old memory
1081    // delete retLib;
1082
1083    if ( (childNode = scew_element_next(parentNode,childNode)) ) {
1084
1085        if (!type.empty()) {
1086            childName = scew_element_name(childNode);
1087            // we are searching for a specific child name
1088            // keep looking till we find a name that matches the type.
1089            // if the current name does not match out search type,
1090            // grab the next child and check to see if its null
1091            // if its not null, get its name and test for type again
1092            while (  (type != childName)
1093                  && (childNode = scew_element_next(parentNode,childNode)) ) {
1094
1095                childName = scew_element_name(childNode);
1096            }
1097            if (type == childName) {
1098                // found a child with a name that matches type
1099                retLib = new RpLibrary( childNode,this->tree );
1100            }
1101            else {
1102                // no children with names that match 'type' were found
1103                retLib = NULL;
1104            }
1105        }
1106        else {
1107            retLib = new RpLibrary( childNode,this->tree );
1108        }
1109    }
1110    else {
1111        // somthing happened within scew, get error code and display
1112        // its probable there are no more child elements left to report
1113        retLib = NULL;
1114    }
1115
1116    return retLib;
1117}
1118
1119/**********************************************************************/
1120// METHOD: children()
1121/// Returns a std::list<RpLibrary*> of all children under 'path'
1122//
1123//
1124/**
1125 */
1126
1127RpLibrary&
1128RpLibrary::childCount ( std::string path,
1129                        int* childCount)
1130{
1131    scew_element* parentNode;
1132    int myChildCount = 0;
1133
1134    if (this->root) {
1135
1136        if (path.empty()) {
1137            // an empty path uses the current RpLibrary as parent
1138            parentNode = this->root;
1139        }
1140
1141        if (parentNode) {
1142            myChildCount = scew_element_count(parentNode);
1143        }
1144
1145        if (childCount) {
1146            *childCount = myChildCount;
1147        }
1148
1149    }
1150
1151    return *this;
1152}
1153
1154/**********************************************************************/
1155// METHOD: isNull()
1156/// returns whether this RpLibrary is a valid library node
1157//
1158//
1159/**
1160 */
1161
1162bool
1163RpLibrary::isNull ()
1164{
1165    if (this->root) {
1166        return false;
1167    }
1168
1169    return true;
1170}
1171
1172/**********************************************************************/
1173// METHOD: get()
1174/// Return the string value of the object held at location 'path'
1175/**
1176 */
1177
1178std::string
1179RpLibrary::get (std::string path, int translateFlag)
1180{
1181    return (this->getString(path, translateFlag));
1182}
1183
1184/**********************************************************************/
1185// METHOD: getString()
1186/// Return the string value of the object held at location 'path'
1187/**
1188 */
1189
1190std::string
1191RpLibrary::getString (std::string path, int translateFlag)
1192{
1193    scew_element* retNode = NULL;
1194    XML_Char const* retCStr = NULL;
1195    char* translatedContents = NULL;
1196    std::string retStr = "";
1197    int len = 0;
1198
1199    if (!this->root) {
1200        // library doesn't exist, do nothing;
1201        return std::string("");
1202    }
1203
1204    retNode = _find(path,NO_CREATE_PATH);
1205
1206    if (retNode == NULL) {
1207        // need to raise error
1208        return std::string("");
1209    }
1210
1211    retCStr = scew_element_contents(retNode);
1212
1213    if (!retCStr) {
1214        return std::string("");
1215    }
1216
1217    if (translateFlag == RPLIB_TRANSLATE) {
1218        // translatedContents = new char[strlen(retCStr)+1];
1219        len = strlen(retCStr);
1220        translatedContents = (char*) calloc((len+1),sizeof(char));
1221        if (translatedContents) {
1222            strncpy(translatedContents, retCStr, len);
1223            _translateOut(translatedContents);
1224            retStr = std::string(translatedContents);
1225            // delete[] translatedContents;
1226            free(translatedContents);
1227        }
1228        else {
1229            // error allocating space
1230            return std::string("");
1231        }
1232    }
1233    else {
1234        retStr = std::string(retCStr);
1235    }
1236
1237    return retStr;
1238}
1239
1240/**********************************************************************/
1241// METHOD: getDouble()
1242/// Return the double value of the object held at location 'path'
1243/**
1244 */
1245
1246double
1247RpLibrary::getDouble (std::string path)
1248{
1249    std::string retValStr = "";
1250    double retValDbl = 0;
1251
1252    if (!this->root) {
1253        // library doesn't exist, do nothing;
1254        return retValDbl;
1255    }
1256
1257    retValStr = this->getString(path);
1258    // think about changing this to strtod()
1259    retValDbl = atof(retValStr.c_str());
1260
1261    // how do we raise error?
1262    // currently we depend on getString to raise the error
1263    return retValDbl;
1264}
1265
1266
1267/**********************************************************************/
1268// METHOD: getInt()
1269/// Return the integer value of the object held at location 'path'
1270/**
1271 */
1272
1273int
1274RpLibrary::getInt (std::string path)
1275{
1276    std::string retValStr = "";
1277    int retValInt = 0;
1278
1279    if (!this->root) {
1280        // library doesn't exist, do nothing;
1281        return retValInt;
1282    }
1283
1284    retValStr = this->getString(path);
1285    // think about changing this to strtod()
1286    retValInt = atoi(retValStr.c_str());
1287
1288    // how do we raise error?
1289    // currently we depend on getString to raise the error
1290    return retValInt;
1291}
1292
1293
1294/**********************************************************************/
1295// METHOD: getBool()
1296/// Return the boolean value of the object held at location 'path'
1297/**
1298 */
1299
1300bool
1301RpLibrary::getBool (std::string path)
1302{
1303    std::string retValStr = "";
1304    bool retValBool = false;
1305    int retValLen = 0;
1306
1307    if (!this->root) {
1308        // library doesn't exist, do nothing;
1309        return retValBool;
1310    }
1311
1312    retValStr = this->getString(path);
1313    transform (retValStr.begin(),retValStr.end(),retValStr.begin(),tolower);
1314    retValLen = retValStr.length();
1315
1316    if ((retValStr.compare(0,retValLen,"1",0,retValLen) == 0  )   ||
1317        (retValStr.compare(0,retValLen,"yes",0,retValLen) == 0 )  ||
1318        (retValStr.compare(0,retValLen,"true",0,retValLen) == 0 ) ||
1319        (retValStr.compare(0,retValLen,"on",0,retValLen) == 0))
1320    {
1321        retValBool = true;
1322    }
1323    else if((retValStr.compare(0,retValLen,"0",0,retValLen) == 0  )   ||
1324            (retValStr.compare(0,retValLen,"no",0,retValLen) == 0 )  ||
1325            (retValStr.compare(0,retValLen,"false",0,retValLen) == 0 ) ||
1326            (retValStr.compare(0,retValLen,"off",0,retValLen) == 0))
1327    {
1328        retValBool = false;
1329    }
1330    else {
1331        // default to false?
1332        retValBool = false;
1333    }
1334
1335    // how do we raise error?
1336    // currently we depend on getString to raise the error
1337    return retValBool;
1338}
1339
1340
1341/**********************************************************************/
1342// METHOD: getData()
1343/// Return a pointer and memory size of the object held at location 'path'
1344/**
1345 */
1346
1347/*
1348Rappture::Buffer&
1349RpLibrary::getData (std::string path)
1350{
1351    const char* retCStr = NULL;
1352    Rappture::Buffer buf;
1353
1354    if (!this->root) {
1355        // library doesn't exist, do nothing;
1356        return buf;
1357    }
1358
1359    retNode = _find(path,NO_CREATE_PATH);
1360
1361    if (retNode == NULL) {
1362        // need to raise error
1363        return buf;
1364    }
1365
1366    retCStr = scew_element_contents(retNode);
1367
1368    if (retCStr == NULL) {
1369        return buf;
1370    }
1371
1372    len = strlen(retCStr);
1373    buf.append(retCStr,len);
1374    return buf;
1375}
1376*/
1377
1378
1379/**********************************************************************/
1380// METHOD: put()
1381/// Put a string value into the xml.
1382/**
1383 */
1384
1385RpLibrary&
1386RpLibrary::put (    std::string path,
1387                    std::string value,
1388                    std::string id,
1389                    unsigned int append,
1390                    unsigned int translateFlag)
1391{
1392    scew_element* retNode = NULL;
1393    std::string tmpVal = "";
1394    const char* contents = NULL;
1395    std::string translatedContents = "";
1396
1397    if (!this->root) {
1398        // library doesn't exist, do nothing;
1399        return *this;
1400    }
1401
1402    retNode = _find(path,CREATE_PATH);
1403
1404    if (retNode) {
1405
1406        if (append == RPLIB_APPEND) {
1407            if ( (contents = scew_element_contents(retNode)) ) {
1408                tmpVal = std::string(contents);
1409            }
1410            value = tmpVal + value;
1411        }
1412
1413        if (translateFlag == RPLIB_TRANSLATE) {
1414            _translateIn(value,translatedContents);
1415            scew_element_set_contents(retNode,translatedContents.c_str());
1416        }
1417        else {
1418            scew_element_set_contents(retNode,value.c_str());
1419        }
1420    }
1421
1422    return *this;
1423}
1424
1425/**********************************************************************/
1426// METHOD: put()
1427/// Put a double value into the xml.
1428/**
1429 */
1430
1431RpLibrary&
1432RpLibrary::put (    std::string path,
1433                    double value,
1434                    std::string id,
1435                    unsigned int append )
1436{
1437    std::stringstream valStr;
1438
1439    if (!this->root) {
1440        // library doesn't exist, do nothing;
1441        return *this;
1442    }
1443
1444    valStr << value;
1445
1446    return this->put(path,valStr.str(),id,append);
1447}
1448
1449/**********************************************************************/
1450// METHOD: put()
1451/// Put a RpLibrary* value into the xml.
1452/**
1453 *  Append flag adds additional nodes, it does not merge same
1454 *  named nodes together
1455 */
1456
1457RpLibrary&
1458RpLibrary::put (    std::string path,
1459                    RpLibrary* value,
1460                    std::string id,
1461                    unsigned int append )
1462{
1463    scew_element* retNode   = NULL;
1464    // scew_element* old_elem  = NULL;
1465    scew_element* new_elem  = NULL;
1466    scew_element* childNode = NULL;
1467    // std::string nodeName    = "";
1468
1469    int retVal = 1;
1470
1471    if (!this->root) {
1472        // library doesn't exist, do nothing;
1473        return *this;
1474    }
1475
1476    // you cannot put a null RpLibrary into the tree
1477    if (!value) {
1478        // need to send back an error saying that user specified a null value
1479        return *this;
1480    }
1481
1482    // nodeName = value->nodeComp();
1483    // old_elem = _find(path+"."+nodeName,NO_CREATE_PATH);
1484
1485    if (append == RPLIB_OVERWRITE) {
1486        retNode = _find(path,NO_CREATE_PATH);
1487        if (retNode) {
1488            scew_element_free(retNode);
1489        }
1490        else {
1491            // path did not exist and was not created
1492            // do nothing
1493        }
1494    }
1495
1496    retNode = _find(path,CREATE_PATH);
1497
1498    if (retNode) {
1499        while ( (childNode = scew_element_next(value->root,childNode)) ) {
1500            if ((new_elem = scew_element_copy(childNode))) {
1501                if (scew_element_add_elem(retNode, new_elem)) {
1502                    // maybe we want to count the number of children
1503                    // that we have successfully added?
1504                    retVal = 0;
1505                }
1506                else {
1507                    // adding new element failed
1508                }
1509            }
1510            else {
1511                // copying new element failed
1512            }
1513        }
1514    }
1515    else {
1516        // path did not exist and was not created.
1517    }
1518
1519    return *this;
1520}
1521
1522
1523/**********************************************************************/
1524// METHOD: putData()
1525/// Put a data from a buffer into the xml.
1526/**
1527 *  Append flag adds additional nodes, it does not merge same
1528 *  named nodes together
1529 */
1530
1531RpLibrary&
1532RpLibrary::putData (std::string path,
1533                    const char* bytes,
1534                    int nbytes,
1535                    unsigned int append  )
1536{
1537    scew_element* retNode = NULL;
1538    const char* contents = NULL;
1539    Rappture::Buffer buf;
1540    unsigned int bytesWritten = 0;
1541
1542    if (!this->root) {
1543        // library doesn't exist, do nothing;
1544        return *this;
1545    }
1546
1547    retNode = _find(path,CREATE_PATH);
1548
1549    if (retNode) {
1550
1551        if (append == RPLIB_APPEND) {
1552            if ( (contents = scew_element_contents(retNode)) ) {
1553                buf.append(contents);
1554                // base64 decode and un-gzip the data
1555                buf.decode();
1556            }
1557        }
1558
1559        buf.append(bytes,nbytes);
1560        // gzip and base64 encode the data
1561        buf.encode();
1562
1563        bytesWritten = (unsigned int) buf.size();
1564        scew_element_set_contents_binary(retNode,buf.bytes(),&bytesWritten);
1565    }
1566
1567    return *this;
1568}
1569
1570
1571/**********************************************************************/
1572// METHOD: putFile()
1573/// Put data from a file into the xml.
1574/**
1575 *  Append flag adds additional nodes, it does not merge same
1576 *  named nodes together
1577 */
1578
1579RpLibrary&
1580RpLibrary::putFile (std::string path,
1581                    std::string fileName,
1582                    unsigned int compress,
1583                    unsigned int append  )
1584{
1585    scew_element* retNode = NULL;
1586    const char* contents = NULL;
1587    Rappture::Buffer buf;
1588    Rappture::Buffer fileBuf;
1589    unsigned int bytesWritten = 0;
1590    std::string value = "";
1591    std::string transContents = "";
1592
1593    if (!this->root) {
1594        // library doesn't exist, do nothing;
1595        return *this;
1596    }
1597
1598    retNode = _find(path,CREATE_PATH);
1599
1600    if (retNode) {
1601
1602        if (append == RPLIB_APPEND) {
1603            if ( (contents = scew_element_contents(retNode)) ) {
1604                buf.append(contents);
1605                if (compress == RPLIB_COMPRESS) {
1606                    // base64 decode and un-gzip the data
1607                    buf.decode();
1608                }
1609            }
1610        }
1611
1612        fileBuf.load(fileName.c_str());
1613        buf += fileBuf;
1614        fileBuf.clear();
1615
1616        if (compress == RPLIB_COMPRESS) {
1617            // gzip and base64 encode the data
1618            buf.encode();
1619        }
1620        else {
1621            value = std::string(buf.bytes(),buf.size());
1622            buf.clear();
1623            _translateIn(value,transContents);
1624            buf.append(transContents.c_str(),transContents.size());
1625        }
1626
1627        bytesWritten = buf.size();
1628        scew_element_set_contents_binary(retNode,buf.bytes(),&bytesWritten);
1629
1630        if (bytesWritten == buf.size()) {
1631            // error writing data to xml
1632        }
1633
1634    }
1635
1636    return *this;
1637}
1638
1639
1640/**********************************************************************/
1641// METHOD: remove()
1642/// Remove the provided path from this RpLibrary
1643/**
1644 */
1645
1646RpLibrary*
1647RpLibrary::remove ( std::string path )
1648{
1649    scew_element* ele = NULL;
1650    int setNULL = 0;
1651    RpLibrary* retLib = NULL;
1652
1653    if (!this->root) {
1654        // library doesn't exist, do nothing;
1655        return NULL;
1656    }
1657
1658    if ( !path.empty() ) {
1659        ele = _find(path,NO_CREATE_PATH);
1660    }
1661    else {
1662        // telling this function to remove "" is essentially destroying
1663        // the object. most functions will fail after a call like this.
1664        ele = this->root;
1665        setNULL++;
1666    }
1667
1668    if (ele) {
1669        scew_element_free(ele);
1670        if (setNULL != 0) {
1671            // this is the case where user specified an empty path.
1672            // the object is useless, and will be deleted.
1673            this->root = NULL;
1674            retLib = NULL;
1675        }
1676        else {
1677            retLib = this;
1678        }
1679    }
1680
1681    return retLib;
1682}
1683
1684/**********************************************************************/
1685// METHOD: xml()
1686/// Return the xml text held in this RpLibrary
1687/**
1688 */
1689
1690std::string
1691RpLibrary::xml ()
1692{
1693    std::stringstream outString;
1694
1695    if (!this->root) {
1696        // library doesn't exist, do nothing;
1697        return std::string("");
1698    }
1699
1700    outString << "<?xml version=\"1.0\"?>\n";
1701    print_element(this->root, 0, outString);
1702
1703    return outString.str();
1704}
1705
1706/**********************************************************************/
1707// METHOD: nodeType()
1708/// Return the type name of this node
1709/**
1710 */
1711
1712std::string
1713RpLibrary::nodeType ()
1714{
1715    if (!this->root) {
1716        // library doesn't exist, do nothing;
1717        return std::string("");
1718    }
1719
1720    return std::string(scew_element_name(root));
1721}
1722
1723/**********************************************************************/
1724// METHOD: nodeId()
1725/// Return the id of this node.
1726/**
1727 */
1728
1729std::string
1730RpLibrary::nodeId ()
1731{
1732    if (!this->root) {
1733        // library doesn't exist, do nothing;
1734        return std::string("");
1735    }
1736
1737    return _node2name(root);
1738}
1739
1740/**********************************************************************/
1741// METHOD: nodeComp()
1742/// Return the component name of this node.
1743/**
1744 */
1745
1746std::string
1747RpLibrary::nodeComp ()
1748{
1749    if (!this->root) {
1750        // library doesn't exist, do nothing;
1751        return std::string("");
1752    }
1753
1754    return _node2comp(root);
1755}
1756
1757/**********************************************************************/
1758// METHOD: nodePath()
1759/// Return the component name of this node's path.
1760/**
1761 */
1762
1763std::string
1764RpLibrary::nodePath ()
1765{
1766    if (!this->root) {
1767        // library doesn't exist, do nothing;
1768        return std::string("");
1769    }
1770
1771    return _node2path(root);
1772}
1773
1774/*
1775 * ----------------------------------------------------------------------
1776 *  METHOD: result
1777 *
1778 *  Clients call this function at the end of their simulation, to
1779 *  pass the simulation result back to the Rappture GUI.  It writes
1780 *  out the given XML object to a runXXX.xml file, and then writes
1781 *  out the name of that file to stdout.
1782 * ======================================================================
1783 *  AUTHOR:  Michael McLennan, Purdue University
1784 *  Copyright (c) 2004-2005
1785 *  Purdue Research Foundation, West Lafayette, IN
1786 * ======================================================================
1787 */
1788void
1789RpLibrary::result() {
1790    std::stringstream outputFile;
1791    std::fstream file;
1792    std::string xmlText = "";
1793    time_t t = 0;
1794    struct tm* timeinfo = NULL;
1795    std::string timestamp = "";
1796
1797    if (this->root) {
1798        outputFile << "run" << (int)time(&t) << ".xml";
1799        file.open(outputFile.str().c_str(),std::ios::out);
1800
1801        put("tool.repository.rappture.date","$Date: 2007-03-01 19:24:02 +0000 (Thu, 01 Mar 2007) $");
1802        put("tool.repository.rappture.revision","$Rev: 597 $");
1803        put("tool.repository.rappture.url","$URL: trunk/src/core/RpLibrary.cc $");
1804
1805        // generate a timestamp for the run file
1806        timeinfo = localtime(&t);
1807        timestamp = std::string(ctime(&t));
1808        // erase the 24th character because it is a newline
1809        timestamp.erase(24);
1810        // concatinate the timezone
1811        timestamp.append(" ");
1812        timestamp.append(timeinfo->tm_zone);
1813
1814        // add the timestamp to the run file
1815        put("output.time", timestamp);
1816
1817        if ( file.is_open() ) {
1818            xmlText = xml();
1819            if (!xmlText.empty()) {
1820                file << xmlText;
1821            }
1822        }
1823        std::cout << "=RAPPTURE-RUN=>" << outputFile.str() << std::endl;
1824    }
1825}
1826
1827/**********************************************************************/
1828// METHOD: print_indent()
1829/// Add indentations to the requested stringstream object.
1830/**
1831 */
1832
1833void
1834RpLibrary::print_indent(unsigned int indent, std::stringstream& outString)
1835{
1836
1837    // keep this around incase you want to use tabs instead of spaces
1838    // while ( (indent--) > 0)
1839    // {
1840    //     outString << "\t";
1841    // }
1842
1843    // keep this around incase you want to use spaces instead of tabs
1844    int cnt = indent*INDENT_SIZE;
1845    while ( (cnt--) > 0)
1846    {
1847        outString << " ";
1848    }
1849
1850}
1851
1852/**********************************************************************/
1853// METHOD: print_attributes()
1854/// Print the attribute names and values for the provided xml node
1855/**
1856 */
1857
1858void
1859RpLibrary::print_attributes(scew_element* element, std::stringstream& outString)
1860{
1861    scew_attribute* attribute = NULL;
1862
1863    if (element != NULL)
1864    {
1865        /**
1866         * Iterates through the element's attribute list, printing the
1867         * pair name-value.
1868         */
1869        attribute = NULL;
1870        while ((attribute = scew_attribute_next(element, attribute)) != NULL)
1871        {
1872            outString << " " << scew_attribute_name(attribute) << "=\"" <<
1873                   scew_attribute_value(attribute) << "\"";
1874        }
1875    }
1876}
1877
1878
1879/**********************************************************************/
1880// METHOD: print_element()
1881/// Print the value of the node and its attributes to a stringstream object
1882/**
1883 */
1884
1885void
1886RpLibrary::print_element(   scew_element* element,
1887                            unsigned int indent,
1888                            std::stringstream& outString    )
1889{
1890    scew_element* child = NULL;
1891    XML_Char const* contents = NULL;
1892
1893    if (element == NULL)
1894    {
1895        return;
1896    }
1897
1898    /**
1899     * Prints the starting element tag with its attributes.
1900     */
1901    print_indent(indent, outString);
1902    outString << "<" << scew_element_name(element);
1903    print_attributes(element,outString);
1904    outString << ">";
1905    contents = scew_element_contents(element);
1906    if (contents == NULL)
1907    {
1908        outString << "\n";
1909    }
1910
1911    /**
1912     * Call print_element function again for each child of the
1913     * current element.
1914     */
1915    child = NULL;
1916    while ((child = scew_element_next(element, child)) != NULL)
1917    {
1918        print_element(child, indent + 1, outString);
1919    }
1920
1921    /* Prints element's content. */
1922    if (contents != NULL)
1923    {
1924        outString << contents;
1925    }
1926    else
1927    {
1928        print_indent(indent, outString);
1929    }
1930
1931    /**
1932     * Prints the closing element tag.
1933     */
1934    outString << "</" << scew_element_name(element) << ">\n" ;
1935}
1936
1937/**********************************************************************/
1938// METHOD: translateIn()
1939/// Translate entity and character reference text coming in from the user
1940/**
1941 */
1942
1943int
1944RpLibrary::_translateIn(std::string& outStr,std::string& translatedStr)
1945{
1946    ERTranslator.appendEscaped(outStr, translatedStr);
1947    return 0;
1948}
1949
1950
1951/**********************************************************************/
1952// METHOD: translateOut()
1953/// Translate entity and character reference text going out to the user
1954/**
1955 */
1956
1957int
1958RpLibrary::_translateOut(char* inStr)
1959{
1960    int newLen = 0;
1961    ERTranslator.TranslateEntityRefs(inStr, &newLen);
1962    return 0;
1963}
Note: See TracBrowser for help on using the repository browser.