Changeset 200 for trunk/src


Ignore:
Timestamp:
Feb 20, 2006, 2:37:44 PM (19 years ago)
Author:
dkearney
Message:

adjusted copy and remove functions in core library.
added a bit of error checking in other core functions.
updated test file accordingly.

File:
1 edited

Legend:

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

    r192 r200  
    6262
    6363    // listLen should be the highest index + 1
    64     for (   pos = path.find(".",0);
     64    for (   pos = path.find(".",NO_CREATE_PATH);
    6565            (pos != std::string::npos) || (index >= listLen);
    6666            pos = path.find(".",pos)   )
     
    405405
    406406        if (node == NULL) {
    407             if (create == 0) {
     407            if (create == NO_CREATE_PATH) {
    408408                // break out instead of returning because we still need to
    409409                // free the list variable
     
    413413            }
    414414            else {
    415                 // create == 1
     415                // create == CREATE_PATH
    416416                // we should create the rest of the path
    417417
     
    469469/// Search the path of a xml tree and return a RpLibrary node.
    470470/**
     471 * It is the user's responsibility to delete the object when
     472 * they are finished using it?, else i need to make this static
    471473 */
    472474
     
    476478    RpLibrary* retLib = NULL;
    477479    scew_element* retNode = NULL;
     480
     481    if (!this->root) {
     482        // library doesn't exist, do nothing;
     483        return NULL;
     484    }
    478485
    479486    if (path.empty()) {
     
    483490
    484491    // get the node located at path
    485     retNode = _find(path,0);
     492    retNode = _find(path,NO_CREATE_PATH);
    486493
    487494    // if the node exists, create a rappture library object for it.
     
    497504/// Search the path of a xml tree and return its parent.
    498505/**
     506 * It is the user's responsibility to delete the object when
     507 * they are finished using it?, else i need to make this static
    499508 */
    500509
     
    506515    std::string::size_type pos = 0;
    507516    scew_element* retNode = NULL;
     517
     518    if (!this->root) {
     519        // library doesn't exist, do nothing;
     520        return NULL;
     521    }
    508522
    509523    if (path.empty()) {
     
    520534
    521535        // search for hte parent's node
    522         retNode = _find(parentPath,0);
     536        retNode = _find(parentPath,NO_CREATE_PATH);
    523537
    524538        if (retNode) {
     
    544558{
    545559    RpLibrary* value = NULL;
     560
     561    if (!this->root) {
     562        // library doesn't exist, do nothing;
     563        return *this;
     564    }
    546565
    547566    if (fromObj == NULL) {
     
    581600    scew_element* childNode = NULL;
    582601    std::string childName = "";
     602
     603    if (!this->root) {
     604        // library doesn't exist, do nothing;
     605        return NULL;
     606    }
    583607
    584608
     
    601625        // we need to search for a new parentNode
    602626        else {
    603             parentNode = _find(path,0);
     627            parentNode = _find(path,NO_CREATE_PATH);
    604628            if (parentNode == NULL) {
    605629                // node not found
     
    685709RpLibrary::getString (std::string path)
    686710{
    687     scew_element* retNode = _find(path,0);
     711    scew_element* retNode = NULL;
    688712    XML_Char const* retCStr = NULL;
     713
     714    if (!this->root) {
     715        // library doesn't exist, do nothing;
     716        return std::string("");
     717    }
     718
     719    retNode = _find(path,NO_CREATE_PATH);
    689720
    690721    if (retNode == NULL) {
     
    714745    double retValDbl = 0;
    715746
     747    if (!this->root) {
     748        // library doesn't exist, do nothing;
     749        return retValDbl;
     750    }
     751
    716752    retValStr = this->getString(path);
    717753    // think about changing this to strtod()
     
    737773    const char* contents = NULL;
    738774
    739 //    if (! path.empty()) {
    740         retNode = _find(path,1);
    741 
    742         if (retNode) {
    743 
    744             if (append) {
    745                 if ( (contents = scew_element_contents(retNode)) ) {
    746                     tmpVal = std::string(contents);
    747                 }
    748                 value = tmpVal + value;
    749             }
    750 
    751             scew_element_set_contents(retNode,value.c_str());
    752         }
    753 //    }
     775    if (!this->root) {
     776        // library doesn't exist, do nothing;
     777        return *this;
     778    }
     779
     780    retNode = _find(path,CREATE_PATH);
     781
     782    if (retNode) {
     783
     784        if (append) {
     785            if ( (contents = scew_element_contents(retNode)) ) {
     786                tmpVal = std::string(contents);
     787            }
     788            value = tmpVal + value;
     789        }
     790
     791        scew_element_set_contents(retNode,value.c_str());
     792    }
    754793
    755794    return *this;
     
    766805{
    767806    std::stringstream valStr;
     807
     808    if (!this->root) {
     809        // library doesn't exist, do nothing;
     810        return *this;
     811    }
    768812
    769813    valStr << value;
     
    786830    int retVal = 1;
    787831
    788     retNode = _find(path,1);
    789 
    790     if (value) {
     832    if (!this->root) {
     833        // library doesn't exist, do nothing;
     834        return *this;
     835    }
     836
     837    // you cannot put a null RpLibrary into the tree
     838    if (!value) {
    791839        // need to send back an error saying that user specified a null value
    792840        return *this;
    793841    }
     842
     843    retNode = _find(path,CREATE_PATH);
    794844
    795845    if (retNode) {
     
    805855
    806856
    807 /*
    808 RpLibrary&
     857/**********************************************************************/
     858// METHOD: remove()
     859/// Remove the provided path from this RpLibrary
     860/**
     861 */
     862
     863RpLibrary*
    809864RpLibrary::remove ( std::string path )
    810865{
    811     scew_element* ele = _find(path,0);
    812     RpLibrary* retLib;
     866    scew_element* ele = NULL;
     867    int setNULL = 0;
     868    RpLibrary* retLib = NULL;
     869
     870    if (!this->root) {
     871        // library doesn't exist, do nothing;
     872        return NULL;
     873    }
     874
     875    if ( !path.empty() ) {
     876        ele = _find(path,NO_CREATE_PATH);
     877    }
     878    else {
     879        // telling this function to remove "" is essentially destroying
     880        // the object. most functions will fail after a call like this.
     881        ele = this->root;
     882        setNULL++;
     883    }
     884
    813885    if (ele) {
    814         retLib = new RpLibrary(ele)
    815     }
    816 
    817     return *retLib
    818 }
    819 */
     886        scew_element_free(ele);
     887        if (setNULL) {
     888            // this is the case where user specified an empty path.
     889            // the object is useless, and will be deleted.
     890            this->root = NULL;
     891            delete this;
     892            retLib = NULL;
     893        }
     894        else {
     895            retLib = this;
     896        }
     897    }
     898
     899    return retLib;
     900}
    820901
    821902/**********************************************************************/
     
    830911    std::stringstream outString;
    831912
     913    if (!this->root) {
     914        // library doesn't exist, do nothing;
     915        return std::string("");
     916    }
     917
    832918    outString << "<?xml version=\"1.0\"?>\n";
    833919    print_element(this->root, 0, outString);
     
    845931RpLibrary::nodeType ()
    846932{
     933    if (!this->root) {
     934        // library doesn't exist, do nothing;
     935        return std::string("");
     936    }
     937
    847938    return std::string(scew_element_name(root));
    848939}
     
    857948RpLibrary::nodeId ()
    858949{
     950    if (!this->root) {
     951        // library doesn't exist, do nothing;
     952        return std::string("");
     953    }
     954
    859955    return _node2name(root);
    860956}
     
    869965RpLibrary::nodeComp ()
    870966{
     967    if (!this->root) {
     968        // library doesn't exist, do nothing;
     969        return std::string("");
     970    }
     971
    871972    return _node2comp(root);
    872973}
     
    881982RpLibrary::nodePath ()
    882983{
     984    if (!this->root) {
     985        // library doesn't exist, do nothing;
     986        return std::string("");
     987    }
     988
    883989    return _node2path(root);
    884990}
     
    9051011    time_t t = 0;
    9061012
    907     outputFile << "run" << (int)time(&t) << ".xml";
    908     file.open(outputFile.str().c_str(),std::ios::out);
    909 
    910     if ( file.is_open() ) {
    911         xmlText = xml();
    912         if (!xmlText.empty()) {
    913             file << xmlText;
    914         }
    915     }
    916     std::cout << "=RAPPTURE-RUN=>" << outputFile.str() << std::endl;
     1013    if (this->root) {
     1014        outputFile << "run" << (int)time(&t) << ".xml";
     1015        file.open(outputFile.str().c_str(),std::ios::out);
     1016
     1017        if ( file.is_open() ) {
     1018            xmlText = xml();
     1019            if (!xmlText.empty()) {
     1020                file << xmlText;
     1021            }
     1022        }
     1023        std::cout << "=RAPPTURE-RUN=>" << outputFile.str() << std::endl;
     1024    }
    9171025}
    9181026
Note: See TracChangeset for help on using the changeset viewer.