Ignore:
Timestamp:
Mar 27, 2009 4:54:09 PM (14 years ago)
Author:
gah
Message:

Begin reorganizing Rappture C++ API, starting with the use of
Rappture::Outcome.

Instead of returning an Outcome and expecting every routine to
save it, pass it as a reference. Let's strip it down to what's
needed right now and add back functionality as required.

Right now too many return values are ignored in the rappture library.
We need to check all results and return the error messages via
the Outcome. At the topmost level where is touches the developer
API we can drop the error checking or turn it on via an environment
variable.

Example. Not enough checks are made for memory allocation failure.
If the application is running on an over-committed server memory
allocation errors will be silently passed on. It's okay to be
fault tolerant where possible, but if we fail to check in the internal
library functions, it's too late.

--gah

File:
1 edited

Legend:

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

    r1264 r1366  
    15201520    Rappture::Buffer inData;
    15211521
     1522    status.addContext("RpLibrary::getString");
    15221523    if (!this->root) {
    15231524        // library doesn't exist, do nothing;
     
    15331534
    15341535    retCStr = scew_element_contents(retNode);
    1535 
    15361536    if (!retCStr) {
    15371537        return retStr;
    15381538    }
    1539 
    15401539    inData = Rappture::Buffer(retCStr);
    15411540
     
    15451544        // there is no reason to do entity translation
    15461545        // because base64 character set does not include xml entity chars
    1547         status &= Rappture::encoding::decode(inData,0);
    1548         status.addContext("RpLibrary::getSting");
     1546        if (!Rappture::encoding::decode(status, inData, 0)) {
     1547            return retStr;
     1548        }
    15491549        retStr = std::string(inData.bytes(),inData.size());
    15501550    } else {
    15511551        // check translateFlag to see if we need to translate entity refs
    15521552        if (translateFlag == RPLIB_TRANSLATE) {
    1553             translatedContents = ERTranslator.decode(inData.bytes(),inData.size());
     1553            translatedContents = ERTranslator.decode(inData.bytes(),
     1554                                                     inData.size());
    15541555            if (translatedContents == NULL) {
    15551556                // translation failed
    15561557                if (!status) {
    15571558                    status.error("Error while translating entity references");
    1558                     status.addContext("RpLibrary::getSting");
     1559                    return retStr;
    15591560                }
    15601561            } else {
     
    15651566        }
    15661567    }
    1567 
    15681568    inData.clear();
    1569 
    15701569    return retStr;
    15711570}
     
    17381737
    17391738RpLibrary&
    1740 RpLibrary::put (    std::string path,
    1741                     std::string value,
    1742                     std::string id,
    1743                     unsigned int append,
    1744                     unsigned int translateFlag)
     1739RpLibrary::put (std::string path, std::string value, std::string id,
     1740                unsigned int append, unsigned int translateFlag)
    17451741{
    17461742    Rappture::EntityRef ERTranslator;
     
    17501746    const char* translatedContents = NULL;
    17511747
     1748    status.addContext("RpLibrary::put() - putString");
     1749
    17521750    if (!this->root) {
    17531751        // library doesn't exist, do nothing;
    17541752        status.error("invalid library object");
    1755         status.addContext("RpLibrary::put() - putString");
    17561753        return *this;
    17571754    }
    17581755
    17591756    // check for binary data
    1760     if (Rappture::encoding::isbinary(value.c_str(),value.length()) != 0) {
    1761         putData(path,value.c_str(),value.length(),append);
    1762         status.addContext("RpLibrary::put() - putString");
     1757    // FIXME: I've already appended a NUL-byte of this assuming that
     1758    //        it's a ASCII string. This test must come before.
     1759    if (Rappture::encoding::isbinary(value.c_str(), value.length()) != 0) {
     1760        putData(path, value.c_str(), value.length(), append);
    17631761        return *this;
    17641762    }
    17651763
    1766     retNode = _find(path,CREATE_PATH);
    1767 
     1764    retNode = _find(path, CREATE_PATH);
    17681765    if (retNode == NULL) {
    17691766        // node not found, set error
    17701767        status.error("Error while searching for node: node not found");
    1771         status.addContext("RpLibrary::put() - putString");
    17721768        return *this;
    17731769    }
     
    17791775            if (!status) {
    17801776                status.error("Error while translating entity references");
     1777                return *this;
    17811778            }
    17821779        }
     
    17861783        }
    17871784    }
    1788 
    17891785    if (append == RPLIB_APPEND) {
    17901786        contents = scew_element_contents(retNode);
     
    17941790        }
    17951791    }
    1796 
    17971792    scew_element_set_contents(retNode,value.c_str());
    1798 
    1799     status.addContext("RpLibrary::put() - putString");
    18001793    return *this;
    18011794}
     
    18081801
    18091802RpLibrary&
    1810 RpLibrary::put (    std::string path,
    1811                     double value,
    1812                     std::string id,
    1813                     unsigned int append )
     1803RpLibrary::put (std::string path, double value, std::string id,
     1804                unsigned int append)
    18141805{
    18151806    std::stringstream valStr;
     
    19631954    size_t flags = 0;
    19641955
     1956    status.addContext("RpLibrary::putData()");
    19651957    if (!this->root) {
    19661958        // library doesn't exist, do nothing;
    19671959        return *this;
    19681960    }
    1969 
    19701961    retNode = _find(path,CREATE_PATH);
    19711962
    1972     if (retNode) {
    1973 
    1974         if (append == RPLIB_APPEND) {
    1975             if ( (contents = scew_element_contents(retNode)) ) {
    1976                 inData.append(contents);
    1977                 // base64 decode and un-gzip the data
    1978                 status &= Rappture::encoding::decode(inData,0);
    1979                 if (int(status) != 0) {
    1980                     status.addContext("RpLibrary::putData()");
    1981                     return *this;
    1982                 }
    1983             }
    1984         }
    1985 
    1986         inData.append(bytes,nbytes);
    1987         // gzip and base64 encode the data
    1988         flags = RPENC_Z|RPENC_B64|RPENC_HDR;
    1989         status &= Rappture::encoding::encode(inData,flags);
    1990 
    1991         bytesWritten = (unsigned int) inData.size();
    1992         scew_element_set_contents_binary(retNode,inData.bytes(),&bytesWritten);
    1993     }
    1994     else {
    1995         // node not found, set error
    1996         if (!status) {
    1997             status.error("Error while searching for node: node not found");
    1998         }
    1999     }
    2000 
    2001     status.addContext("RpLibrary::putData()");
     1963    if (retNode == NULL) {
     1964        status.addError("can't create node from path \"%s\"", path.c_str());
     1965        return *this;
     1966    }
     1967    if (append == RPLIB_APPEND) {
     1968        if ( (contents = scew_element_contents(retNode)) ) {
     1969            inData.append(contents);
     1970            // base64 decode and un-gzip the data
     1971            if (!Rappture::encoding::decode(status, inData, 0)) {
     1972                return *this;
     1973            }
     1974        }
     1975    }
     1976    if (inData.append(bytes, nbytes) != nbytes) {
     1977        status.addError("can't append %d bytes", nbytes);
     1978        return *this;
     1979    }   
     1980    // gzip and base64 encode the data
     1981    flags = RPENC_Z|RPENC_B64|RPENC_HDR;
     1982    if (!Rappture::encoding::encode(status, inData,flags)) {
     1983        return *this;
     1984    }
     1985    bytesWritten = (unsigned int) inData.size();
     1986    scew_element_set_contents_binary(retNode,inData.bytes(),&bytesWritten);
    20021987    return *this;
    20031988}
     
    20131998
    20141999RpLibrary&
    2015 RpLibrary::putFile (std::string path,
    2016                     std::string fileName,
    2017                     unsigned int compress,
    2018                     unsigned int append  )
     2000RpLibrary::putFile(std::string path, std::string fileName,
     2001                   unsigned int compress, unsigned int append)
    20192002{
    20202003    Rappture::Buffer buf;
     
    20272010    }
    20282011
    2029     fileBuf.load(fileName.c_str());
     2012    if (!fileBuf.load(err, fileName.c_str())) {
     2013        fprintf(stderr, "error loading file: %s\n", err.remark());
     2014        return *this;
     2015    }
    20302016    if (compress == RPLIB_COMPRESS) {
    2031         putData(path,fileBuf.bytes(),fileBuf.size(),append);
    2032     }
    2033     else {
    2034         fileBuf.append("\0",1);
    2035         put(path,fileBuf.bytes(),"",append,RPLIB_TRANSLATE);
     2017        putData(path, fileBuf.bytes(), fileBuf.size(), append);
     2018    } else {
     2019        /* Always append a NUL-byte to the end of ASCII strings. */
     2020        fileBuf.append("\0", 1);
     2021        put(path, fileBuf.bytes(), "", append, RPLIB_TRANSLATE);
    20362022    }
    20372023    status.addContext("RpLibrary::putFile()");
Note: See TracChangeset for help on using the changeset viewer.