Changeset 759 for trunk


Ignore:
Timestamp:
Jun 8, 2007 1:48:39 PM (17 years ago)
Author:
dkearney
Message:

fixes for the copy function within the core rappture library. this fix also includes updates to the rappture library flavor of the put command and cleans up some commented out code.

Location:
trunk/src/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpLibrary.cc

    r758 r759  
    44 *
    55 * ======================================================================
    6  *  AUTHOR:  Derrick Kearney, Purdue University
     6 *  AUTHOR:  Derrick S. Kearney, Purdue University
    77 *  Copyright (c) 2004-2007  Purdue Research Foundation
    88 *
     
    320320    scew_attribute* attribute = NULL;
    321321    std::string attrVal;
    322     // int attrValSize = 0;
    323322
    324323    if (element != NULL)
     
    625624RpLibrary::_find(std::string path, int create) const
    626625{
    627     // std::string* tagName;
    628     // std::string* id;
    629626    std::string tagName = "";
    630627    std::string id = "";
     
    645642
    646643    if (path.empty()) {
    647         //user gave an empty path
    648         // return this;
     644        // user gave an empty path
    649645        return tmpElement;
    650646    }
     
    663659        _splitPath(*(list[listIdx]),tagName,&index,id);
    664660
    665         // if (id->empty()) {
    666661        if (id.empty()) {
    667662            /*
     
    671666            */
    672667
    673             // eleList = scew_element_list(tmpElement, tagName->c_str(), &count);
    674668            eleList = scew_element_list(tmpElement, tagName.c_str(), &count);
    675669            tmpCount = count;
     
    697691            */
    698692
    699             // eleList = scew_element_list(tmpElement, tagName->c_str(), &count);
    700693            if (!tagName.empty()) {
    701694                eleList = scew_element_list(tmpElement, tagName.c_str(), &count);
     
    709702                tmpId = _get_attribute(eleList[lcv], "id");
    710703                if (!tmpId.empty()) {
    711                     // if (*id == tmpId) {
    712704                    if (id == tmpId) {
    713705                        node = eleList[lcv];
     
    730722                // break out instead of returning because we still need to
    731723                // free the list variable
    732                 // return node;
    733724                tmpElement = node;
    734725                break;
     
    740731                // create the new element
    741732                // need to figure out how to properly number the new element
    742                 // node = scew_element_add(tmpElement,tagName->c_str());
    743733                node = scew_element_add(tmpElement,tagName.c_str());
    744734                if (! node) {
    745                     // a new element was not created 
     735                    // a new element was not created
    746736                }
    747737
    748738                // add an attribute and attrValue to the new element
    749                 // if (id && !id->empty()) {
    750                     // scew_element_add_attr_pair(node,"id",id->c_str());
    751739                if (!id.empty()) {
    752740                    scew_element_add_attr_pair(node,"id",id.c_str());
     
    755743        }
    756744
    757 
    758         // change this so youre not allocating and deallocating so much.
    759         // make it static or something.
    760         // if (tagName)    { delete (tagName); }
    761         // if (id)         { delete (id); }
    762745        tagName = "";
    763746        id = "";
     
    785768}
    786769
     770/**********************************************************************/
     771// METHOD: _checkPathConflict(scew_element *nodeA,scew_element *nodeB)
     772/// check to see if nodeA is in nodeB's path
     773/**
     774 * This is used by put() function (the RpLibrary flavor). It is
     775 * used to check if nodeA can be safely deleted and not effect nodeB
     776 */
     777
     778int
     779RpLibrary::_checkPathConflict(scew_element *nodeA, scew_element *nodeB) const
     780{
     781    scew_element *testNode = NULL;
     782
     783    if ( (nodeA == NULL) || (nodeB == NULL) ) {
     784        return 0;
     785    }
     786
     787    if (nodeA == nodeB) {
     788        return 1;
     789    }
     790
     791    testNode = nodeB;
     792
     793    while ((testNode = scew_element_parent(testNode)) != NULL) {
     794        if (testNode == nodeA) {
     795            return 1;
     796        }
     797    }
     798
     799    return 0;
     800}
    787801
    788802/**********************************************************************/
     
    11801194RpLibrary&
    11811195RpLibrary::copy (   std::string toPath,
    1182                     std::string fromPath,
    1183                     RpLibrary* fromObj )
     1196                    RpLibrary* fromObj,
     1197                    std::string fromPath )
    11841198{
    11851199    RpLibrary* value = NULL;
    1186     // RpLibrary* child = NULL;
    11871200
    11881201    if (!this->root) {
     
    11961209    }
    11971210
     1211    if ( (fromObj == this) && (toPath == fromPath) ) {
     1212        /* cannot copy over myself, causes path to disappear */
     1213        return (*this);
     1214    }
     1215
    11981216    value = fromObj->element(fromPath);
    11991217
    12001218    if ( !value ) {
    1201         // need a good way to raise error, and this is not it.
     1219        status.error("fromPath could not be found within fromObj");
     1220        status.addContext("RpLibrary::copy");
    12021221        return *this;
    12031222    }
    12041223
    12051224    this->put(toPath, value);
     1225    status.addContext("RpLibrary::copy");
    12061226    delete value;
    12071227
     
    13231343
    13241344RpLibrary*
    1325 RpLibrary::children (   std::string path, 
    1326                         RpLibrary* rpChildNode, 
     1345RpLibrary::children (   std::string path,
     1346                        RpLibrary* rpChildNode,
    13271347                        std::string type,
    13281348                        int* childCount)
     
    17251745    if (!this->root) {
    17261746        // library doesn't exist, do nothing;
     1747        status.error("invalid library object");
     1748        status.addContext("RpLibrary::put()");
    17271749        return *this;
    17281750    }
     
    17891811    std::stringstream valStr;
    17901812
    1791     if (!this->root) {
     1813    if (this->root == NULL) {
    17921814        // library doesn't exist, do nothing;
     1815        status.error("invalid library object");
     1816        status.addContext("RpLibrary::put()");
    17931817        return *this;
    17941818    }
     
    18031827/**********************************************************************/
    18041828// METHOD: put()
    1805 /// Put a RpLibrary* value into the xml.
     1829/// Put a RpLibrary* value into the xml. This is used by copy()
    18061830/**
    18071831 *  Append flag adds additional nodes, it does not merge same
     
    18151839                    unsigned int append )
    18161840{
    1817     scew_element* retNode   = NULL;
    1818     // scew_element* old_elem  = NULL;
    1819     scew_element* new_elem = NULL;
    1820     scew_element* childNode = NULL;
    1821     // std::string nodeName    = "";
    1822 
    1823     int retVal = 1;
    1824 
    1825     if (!this->root) {
     1841    scew_element *retNode   = NULL;
     1842    scew_element *new_elem  = NULL;
     1843    scew_element *childNode = NULL;
     1844    scew_element *tmpNode  = NULL;
     1845    const char *contents    = NULL;
     1846    int retVal              = 1;
     1847    int deleteTmpNode       = 0;
     1848
     1849    if (this->root == NULL) {
    18261850        // library doesn't exist, do nothing;
     1851        status.error("invalid library object");
     1852        status.addContext("RpLibrary::put()");
    18271853        return *this;
    18281854    }
    18291855
    1830     // you cannot put a null RpLibrary into the tree
    1831     if (!value) {
    1832         // need to send back an error saying that user specified a null value
     1856    if (value == NULL) {
     1857        // you cannot put a null RpLibrary into the tree
     1858        // user specified a null value
     1859        status.error("user specified NULL value");
     1860        status.addContext("RpLibrary::put()");
    18331861        return *this;
    18341862    }
    18351863
    1836     // nodeName = value->nodeComp();
    1837     // old_elem = _find(path+"."+nodeName,NO_CREATE_PATH);
     1864    if (value->root == NULL) {
     1865        // you cannot put a null RpLibrary into the tree
     1866        // user specified a null value
     1867        status.error("user specified uninitialized RpLibrary object");
     1868        status.addContext("RpLibrary::put()");
     1869        return *this;
     1870    }
     1871
     1872    tmpNode = value->root;
    18381873
    18391874    if (append == RPLIB_OVERWRITE) {
    18401875        retNode = _find(path,NO_CREATE_PATH);
    18411876        if (retNode) {
    1842             scew_element_free(retNode);
     1877            // compare roots to see if they are part of the
     1878            // same xml tree, if so, make a tmp copy of the
     1879            // tree to be copied before freeing it.
     1880            if (_checkPathConflict(retNode,tmpNode)) {
     1881                tmpNode = scew_element_copy(tmpNode);
     1882                deleteTmpNode = 1;
     1883            }
     1884            contents = scew_element_contents(tmpNode);
     1885            if (contents) {
     1886                scew_element_set_contents(retNode, "");
     1887            }
     1888
     1889            while ( (childNode = scew_element_next(retNode,childNode)) ) {
     1890                scew_element_free(childNode);
     1891            }
    18431892        }
    18441893        else {
     
    18511900
    18521901    if (retNode) {
    1853         while ( (childNode = scew_element_next(value->root,childNode)) ) {
     1902        contents = scew_element_contents(tmpNode);
     1903        if (contents) {
     1904            scew_element_set_contents(retNode, contents);
     1905        }
     1906
     1907        while ( (childNode = scew_element_next(tmpNode,childNode)) ) {
    18541908            if ((new_elem = scew_element_copy(childNode))) {
    18551909                if (scew_element_add_elem(retNode, new_elem)) {
     
    18601914                else {
    18611915                    // adding new element failed
     1916                    status.error("error while adding child node");
     1917                    status.addContext("RpLibrary::put()");
    18621918                }
    18631919            }
    18641920            else {
    18651921                // copying new element failed
    1866             }
     1922                status.error("error while copying child node");
     1923                status.addContext("RpLibrary::put()");
     1924            }
     1925        }
     1926
     1927        if (deleteTmpNode == 1) {
     1928            scew_element_free(tmpNode);
    18671929        }
    18681930    }
    18691931    else {
    18701932        // path did not exist and was not created.
     1933        status.error("error while creating child node");
     1934        status.addContext("RpLibrary::put()");
    18711935    }
    18721936
     
    21462210        file.open(outputFile.str().c_str(),std::ios::out);
    21472211
    2148         put("tool.version.rappture.date","$LastChangedDate$");
    21492212        put("tool.version.rappture.revision","$LastChangedRevision$");
    2150         put("tool.version.rappture.url","$URL$");
     2213        put("tool.version.rappture.modified","$LastChangedDate$");
     2214        if ( "" == get("tool.version.rappture.language") ) {
     2215            put("tool.version.rappture.language","c++");
     2216        }
    21512217
    21522218        // generate a timestamp for the run file
     
    21752241            // errors while writing the run.xml file.
    21762242            if (   (!file.good())
    2177                 || ((long)xmlText.length() != (file.tellp()-(long)1)) ) {
     2243                || ((long)xmlText.length() != ((long)file.tellp()-(long)1))
     2244               ) {
    21782245                 status.error("Error while writing run file");
    21792246                 status.addContext("RpLibrary::result()");
     
    22292296    if (element != NULL)
    22302297    {
    2231         /**
    2232          * Iterates through the element's attribute list, printing the
    2233          * pair name-value.
    2234          */
    2235         attribute = NULL;
    2236         while ((attribute = scew_attribute_next(element, attribute)) != NULL)
    2237         {
    2238             outString << " " << scew_attribute_name(attribute) << "=\"" <<
    2239                    scew_attribute_value(attribute) << "\"";
     2298        if (scew_attribute_count(element) > 0) {
     2299            /**
     2300             * Iterates through the element's attribute list, printing the
     2301             * pair name-value.
     2302             */
     2303            attribute = NULL;
     2304            while((attribute=scew_attribute_next(element, attribute)) != NULL)
     2305            {
     2306                outString << " " << scew_attribute_name(attribute) << "=\"" <<
     2307                       scew_attribute_value(attribute) << "\"";
     2308            }
    22402309        }
    22412310    }
  • trunk/src/core/RpLibrary.h

    r758 r759  
    44 *
    55 * ======================================================================
    6  *  AUTHOR:  Derrick Kearney, Purdue University
     6 *  AUTHOR:  Derrick S. Kearney, Purdue University
    77 *  Copyright (c) 2004-2007  Purdue Research Foundation
    88 *
     
    6767
    6868        RpLibrary& copy       ( std::string toPath,
    69                                 std::string fromPath,
    70                                 RpLibrary* fromObj = NULL);
     69                                RpLibrary* fromObj,
     70                                std::string fromPath);
    7171
    7272        std::string get       ( std::string path = "",
     
    199199                        std::string& id ) const;
    200200        scew_element* _find (std::string path, int create) const;
     201        int _checkPathConflict (scew_element *nodeA, scew_element *nodeB) const;
    201202        void print_indent ( unsigned int indent,
    202203                            std::stringstream& outString) const;
Note: See TracChangeset for help on using the changeset viewer.