Changeset 671 for trunk/src/core


Ignore:
Timestamp:
Apr 15, 2007, 10:53:36 PM (17 years ago)
Author:
dkearney
Message:

abstracted the encoding module out to a src/core module and linked tcl and python code to the core c++ module.
make small fix to the src/tcl configure script so it correctly finds rappture lib directory.
setup core library module to automatically encode and decode data as needed.
added a missin return value to function in !Ptr.h.

Location:
trunk/src/core
Files:
2 added
2 edited

Legend:

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

    r625 r671  
    1414#include "RpLibrary.h"
    1515#include "RpEntityRef.h"
     16#include "RpEncode.h"
    1617#include <algorithm>
    1718#include <iostream>
     
    4849        freeRoot    (1)
    4950{
     51    std::stringstream msg;
    5052
    5153    if (filePath.length() != 0) {
     
    6365            printf("Unable to load file (error #%d: %s)\n", code,
    6466                   scew_error_string(code));
    65 
    66             /*
    67             std::cout << "Unable to load file (error #" \
    68                       << code << ": " << scew_error_string(code) \
    69                       << ")\n" << std::endl;
    70             */
     67            msg << "Unable to load file (error #" << code
     68                << ": " << scew_error_string(code) << ")\n";
    7169
    7270            if (code == scew_error_expat)
     
    7977                       scew_error_expat_column(parser),
    8078                       scew_error_expat_string(expat_code));
    81             }
    82             // should probably exit program or something
    83             // return EXIT_FAILURE;
     79                msg << "Expat error #" << expat_code << " (line "
     80                    << scew_error_expat_line(parser) << ", column "
     81                    << scew_error_expat_column(parser) << "): "
     82                    << "\n";
     83            }
     84
    8485            fflush(stdout);
    8586            scew_parser_free(parser);
    8687            parser = NULL;
    87         }
    88         else
     88
     89            // update the status of the call
     90            status.error(msg.str().c_str());
     91            status.addContext("RpLibrary::RpLibrary()");
     92        }
     93        else
    8994        {
    9095            tree = scew_parser_tree(parser);
     
    111116      freeRoot  (1)
    112117{
     118    std::stringstream msg;
    113119    std::string buffer;
    114120    int buffLen;
     
    132138            printf("Unable to load buffer (error #%d: %s)\n", code,
    133139                   scew_error_string(code));
     140            msg << "Unable to load file (error #" << code
     141                << ": " << scew_error_string(code) << ")\n";
    134142
    135143            if (code == scew_error_expat)
     
    142150                       scew_error_expat_column(parser),
    143151                       scew_error_expat_string(expat_code));
     152                msg << "Expat error #" << expat_code << " (line "
     153                    << scew_error_expat_line(parser) << ", column "
     154                    << scew_error_expat_column(parser) << "): "
     155                    << "\n";
    144156            }
    145157
     
    148160
    149161            parser = NULL;
     162
     163            // update the status of the call
     164            status.error(msg.str().c_str());
     165            status.addContext("RpLibrary::RpLibrary()");
    150166        }
    151167        else {
     
    172188RpLibrary::operator= (const RpLibrary& other) {
    173189
     190    std::stringstream msg;
    174191    std::string buffer;
    175192    int buffLen;
     
    207224                printf("Unable to load buffer (error #%d: %s)\n", code,
    208225                       scew_error_string(code));
     226                msg << "Unable to load file (error #" << code
     227                    << ": " << scew_error_string(code) << ")\n";
    209228
    210229                if (code == scew_error_expat)
     
    217236                           scew_error_expat_column(parser),
    218237                           scew_error_expat_string(expat_code));
     238                    msg << "Expat error #" << expat_code << " (line "
     239                        << scew_error_expat_line(parser) << ", column "
     240                        << scew_error_expat_column(parser) << "): "
     241                        << "\n";
    219242                }
    220243
     
    225248                // return this object to its previous state.
    226249                parser = tmp_parser;
     250
     251                // update the status of the call
     252                status.error(msg.str().c_str());
     253                status.addContext("RpLibrary::RpLibrary()");
    227254            }
    228255            else {
     
    14691496    const char* translatedContents = NULL;
    14701497    std::string retStr = "";
     1498    Rappture::Buffer inData;
    14711499
    14721500    if (!this->root) {
     
    14881516    }
    14891517
     1518    inData = Rappture::Buffer(retCStr);
     1519    status &= Rappture::encoding::decode(inData,0);
     1520    status.addContext("RpLibrary::getSting");
     1521    inData.append("\0",1);
     1522
    14901523    if (translateFlag == RPLIB_TRANSLATE) {
    1491         translatedContents = ERTranslator.decode(retCStr);
     1524        translatedContents = ERTranslator.decode(inData.bytes());
    14921525        if (translatedContents == NULL) {
    14931526            // translation failed
    1494             return retStr;
    1495         }
    1496         retStr = std::string(translatedContents);
    1497         translatedContents = NULL;
     1527            if (!status) {
     1528                status.error("Error while translating entity references");
     1529                status.addContext("RpLibrary::getSting");
     1530            }
     1531        }
     1532        else {
     1533            retStr = std::string(translatedContents);
     1534            translatedContents = NULL;
     1535        }
    14981536    }
    14991537    else {
    1500         retStr = std::string(retCStr);
    1501     }
     1538        retStr = std::string(inData.bytes());
     1539    }
     1540
     1541    inData.clear();
    15021542
    15031543    return retStr;
     
    15211561    }
    15221562
    1523     retValStr = this->getString(path);
     1563    retValStr = this->getString(path);
     1564    status.addContext("RpLibrary::getDouble");
    15241565    // think about changing this to strtod()
    15251566    retValDbl = atof(retValStr.c_str());
    15261567
    1527     // how do we raise error?
    1528     // currently we depend on getString to raise the error
    15291568    return retValDbl;
    15301569}
     
    15481587    }
    15491588
    1550     retValStr = this->getString(path);
     1589    retValStr = this->getString(path);
     1590    status.addContext("RpLibrary::getInt");
    15511591    // think about changing this to strtod()
    15521592    retValInt = atoi(retValStr.c_str());
    15531593
    1554     // how do we raise error?
    1555     // currently we depend on getString to raise the error
    15561594    return retValInt;
    15571595}
     
    15771615
    15781616    retValStr = this->getString(path);
     1617    status.addContext("RpLibrary::getBool");
    15791618    std::transform (retValStr.begin(),retValStr.end(),retValStr.begin(),tolower);
    15801619    retValLen = retValStr.length();
     
    15991638    }
    16001639
    1601     // how do we raise error?
    1602     // currently we depend on getString to raise the error
    16031640    return retValBool;
    16041641}
     
    16121649
    16131650Rappture::Buffer
    1614 RpLibrary::getData (std::string path, Rappture::Outcome& status) const
     1651RpLibrary::getData (std::string path) const
    16151652{
    16161653    Rappture::EntityRef ERTranslator;
     
    16391676
    16401677    if (retCStr == NULL) {
    1641         status.error("element located at path is empty");
    1642         status.addContext("RpLibrary::getData()");
     1678        // element located at path is empty
    16431679        return buf;
    16441680    }
     
    16481684        if (translatedContents == NULL) {
    16491685            // translation failed
    1650             return buf;
    1651         }
    1652         len = strlen(translatedContents);
    1653         buf.append(translatedContents,len);
    1654         translatedContents = NULL;
     1686            if (!status) {
     1687                status.error("Error while translating entity references");
     1688                status.addContext("RpLibrary::getData()");
     1689            }
     1690        }
     1691        else {
     1692            len = strlen(translatedContents);
     1693            buf.append(translatedContents,len);
     1694            translatedContents = NULL;
     1695        }
    16551696    }
    16561697    else {
     
    16871728    }
    16881729
     1730    // check for binary data
     1731    if (Rappture::encoding::isbinary(value.c_str(),value.length()) != 0) {
     1732        putData(path,value.c_str(),value.length(),append);
     1733        status.addContext("RpLibrary::put() - putString");
     1734        return *this;
     1735    }
     1736
    16891737    retNode = _find(path,CREATE_PATH);
    16901738
     
    17001748        if (translateFlag == RPLIB_TRANSLATE) {
    17011749            translatedContents = ERTranslator.encode(value.c_str());
    1702             if (translatedContents == NULL) {
    1703                 // translation failed
    1704                 return *this;
    1705             }
     1750        }
     1751        else {
     1752            translatedContents = value.c_str();
     1753        }
     1754
     1755        if (translatedContents == NULL) {
     1756            // entity referene translation failed
     1757            if (!status) {
     1758                status.error("Error while translating entity references");
     1759            }
     1760        }
     1761        else {
    17061762            scew_element_set_contents(retNode,translatedContents);
    17071763            translatedContents = NULL;
    17081764        }
    1709         else {
    1710             scew_element_set_contents(retNode,value.c_str());
    1711         }
    1712     }
    1713 
     1765    }
     1766    else {
     1767        // node not found, set error
     1768        if (!status) {
     1769            status.error("Error while searching for node: node not found");
     1770        }
     1771    }
     1772
     1773    status.addContext("RpLibrary::put() - putString");
    17141774    return *this;
    17151775}
     
    17361796    valStr << value;
    17371797
    1738     return this->put(path,valStr.str(),id,append);
     1798    put(path,valStr.str(),id,append);
     1799    status.addContext("RpLibrary::put() - putDouble");
     1800    return *this;
    17391801}
    17401802
     
    18291891    scew_element* retNode = NULL;
    18301892    const char* contents = NULL;
    1831     Rappture::Buffer buf;
     1893    Rappture::Buffer inData;
    18321894    unsigned int bytesWritten = 0;
     1895    int flags = 0;
    18331896
    18341897    if (!this->root) {
     
    18431906        if (append == RPLIB_APPEND) {
    18441907            if ( (contents = scew_element_contents(retNode)) ) {
    1845                 buf.append(contents);
     1908                inData.append(contents);
    18461909                // base64 decode and un-gzip the data
    1847                 buf.decode();
    1848             }
    1849         }
    1850 
    1851         buf.append(bytes,nbytes);
     1910                status &= Rappture::encoding::decode(inData,0);
     1911                if (int(status) != 0) {
     1912                    status.addContext("RpLibrary::putData()");
     1913                    return *this;
     1914                }
     1915            }
     1916        }
     1917
     1918        inData.append(bytes,nbytes);
    18521919        // gzip and base64 encode the data
    1853         buf.encode();
    1854 
    1855         bytesWritten = (unsigned int) buf.size();
    1856         scew_element_set_contents_binary(retNode,buf.bytes(),&bytesWritten);
    1857     }
    1858 
     1920        flags = RPENC_Z|RPENC_B64|RPENC_HDR;
     1921        status &= Rappture::encoding::encode(inData,flags);
     1922
     1923        bytesWritten = (unsigned int) inData.size();
     1924        scew_element_set_contents_binary(retNode,inData.bytes(),&bytesWritten);
     1925    }
     1926    else {
     1927        // node not found, set error
     1928        if (!status) {
     1929            status.error("Error while searching for node: node not found");
     1930        }
     1931    }
     1932
     1933    status.addContext("RpLibrary::putData()");
    18591934    return *this;
    18601935}
     
    18921967        put(path,fileBuf.bytes(),"",append,RPLIB_TRANSLATE);
    18931968    }
     1969    status.addContext("RpLibrary::putFile()");
    18941970    return *this;
    18951971}
     
    20282104
    20292105    return _node2path(root);
     2106}
     2107
     2108/**********************************************************************/
     2109// METHOD: outcome()
     2110/// Return the status object of this library object.
     2111/**
     2112 */
     2113
     2114Rappture::Outcome&
     2115RpLibrary::outcome() const
     2116{
     2117    return status;
    20302118}
    20312119
     
    20402128 * ======================================================================
    20412129 *  AUTHOR:  Michael McLennan, Purdue University
    2042  *  Copyright (c) 2004-2005
     2130 *  Copyright (c) 2004-2007
    20432131 *  Purdue Research Foundation, West Lafayette, IN
    20442132 * ======================================================================
     
    20582146        file.open(outputFile.str().c_str(),std::ios::out);
    20592147
    2060         put("tool.repository.rappture.date","$Date$");
    2061         put("tool.repository.rappture.revision","$Rev$");
    2062         put("tool.repository.rappture.url","$URL$");
     2148        put("tool.version.rappture.date","$LastChangedDate$");
     2149        put("tool.version.rappture.revision","$LastChangedRevision$");
     2150        put("tool.version.rappture.url","$URL$");
    20632151
    20642152        // generate a timestamp for the run file
     
    20852173            file.close();
    20862174        }
     2175        else {
     2176            status.error("Error while opening run file");
     2177            status.addContext("RpLibrary::result()");
     2178        }
    20872179        std::cout << "=RAPPTURE-RUN=>" << outputFile.str() << std::endl;
    20882180    }
  • trunk/src/core/RpLibrary.h

    r625 r671  
    7878        int         getInt    ( std::string path = "") const;
    7979        bool        getBool   ( std::string path = "") const;
    80         Rappture::Buffer getData ( std::string path,
    81                                    Rappture::Outcome& status) const;
     80        Rappture::Buffer getData ( std::string path = "") const;
    8281
    8382        /*
     
    134133        std::string nodeComp() const;
    135134        std::string nodePath() const;
     135
     136        Rappture::Outcome& outcome() const;
    136137
    137138        void result();
     
    171172        int freeRoot;
    172173
     174        mutable Rappture::Outcome status;
    173175
    174176        RpLibrary ( scew_element* node, scew_tree* tree)
Note: See TracChangeset for help on using the changeset viewer.