Changeset 1366 for trunk/src/core/RpLibrary.cc
- Timestamp:
- Mar 27, 2009 4:54:09 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpLibrary.cc
r1264 r1366 1520 1520 Rappture::Buffer inData; 1521 1521 1522 status.addContext("RpLibrary::getString"); 1522 1523 if (!this->root) { 1523 1524 // library doesn't exist, do nothing; … … 1533 1534 1534 1535 retCStr = scew_element_contents(retNode); 1535 1536 1536 if (!retCStr) { 1537 1537 return retStr; 1538 1538 } 1539 1540 1539 inData = Rappture::Buffer(retCStr); 1541 1540 … … 1545 1544 // there is no reason to do entity translation 1546 1545 // 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 } 1549 1549 retStr = std::string(inData.bytes(),inData.size()); 1550 1550 } else { 1551 1551 // check translateFlag to see if we need to translate entity refs 1552 1552 if (translateFlag == RPLIB_TRANSLATE) { 1553 translatedContents = ERTranslator.decode(inData.bytes(),inData.size()); 1553 translatedContents = ERTranslator.decode(inData.bytes(), 1554 inData.size()); 1554 1555 if (translatedContents == NULL) { 1555 1556 // translation failed 1556 1557 if (!status) { 1557 1558 status.error("Error while translating entity references"); 1558 status.addContext("RpLibrary::getSting");1559 return retStr; 1559 1560 } 1560 1561 } else { … … 1565 1566 } 1566 1567 } 1567 1568 1568 inData.clear(); 1569 1570 1569 return retStr; 1571 1570 } … … 1738 1737 1739 1738 RpLibrary& 1740 RpLibrary::put ( std::string path, 1741 std::string value, 1742 std::string id, 1743 unsigned int append, 1744 unsigned int translateFlag) 1739 RpLibrary::put (std::string path, std::string value, std::string id, 1740 unsigned int append, unsigned int translateFlag) 1745 1741 { 1746 1742 Rappture::EntityRef ERTranslator; … … 1750 1746 const char* translatedContents = NULL; 1751 1747 1748 status.addContext("RpLibrary::put() - putString"); 1749 1752 1750 if (!this->root) { 1753 1751 // library doesn't exist, do nothing; 1754 1752 status.error("invalid library object"); 1755 status.addContext("RpLibrary::put() - putString");1756 1753 return *this; 1757 1754 } 1758 1755 1759 1756 // 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); 1763 1761 return *this; 1764 1762 } 1765 1763 1766 retNode = _find(path,CREATE_PATH); 1767 1764 retNode = _find(path, CREATE_PATH); 1768 1765 if (retNode == NULL) { 1769 1766 // node not found, set error 1770 1767 status.error("Error while searching for node: node not found"); 1771 status.addContext("RpLibrary::put() - putString");1772 1768 return *this; 1773 1769 } … … 1779 1775 if (!status) { 1780 1776 status.error("Error while translating entity references"); 1777 return *this; 1781 1778 } 1782 1779 } … … 1786 1783 } 1787 1784 } 1788 1789 1785 if (append == RPLIB_APPEND) { 1790 1786 contents = scew_element_contents(retNode); … … 1794 1790 } 1795 1791 } 1796 1797 1792 scew_element_set_contents(retNode,value.c_str()); 1798 1799 status.addContext("RpLibrary::put() - putString");1800 1793 return *this; 1801 1794 } … … 1808 1801 1809 1802 RpLibrary& 1810 RpLibrary::put ( std::string path, 1811 double value, 1812 std::string id, 1813 unsigned int append ) 1803 RpLibrary::put (std::string path, double value, std::string id, 1804 unsigned int append) 1814 1805 { 1815 1806 std::stringstream valStr; … … 1963 1954 size_t flags = 0; 1964 1955 1956 status.addContext("RpLibrary::putData()"); 1965 1957 if (!this->root) { 1966 1958 // library doesn't exist, do nothing; 1967 1959 return *this; 1968 1960 } 1969 1970 1961 retNode = _find(path,CREATE_PATH); 1971 1962 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); 2002 1987 return *this; 2003 1988 } … … 2013 1998 2014 1999 RpLibrary& 2015 RpLibrary::putFile (std::string path, 2016 std::string fileName, 2017 unsigned int compress, 2018 unsigned int append ) 2000 RpLibrary::putFile(std::string path, std::string fileName, 2001 unsigned int compress, unsigned int append) 2019 2002 { 2020 2003 Rappture::Buffer buf; … … 2027 2010 } 2028 2011 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 } 2030 2016 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); 2036 2022 } 2037 2023 status.addContext("RpLibrary::putFile()");
Note: See TracChangeset
for help on using the changeset viewer.