int streambuf::padn(char pad, int count) { #define PADSIZE 16 static char const blanks[PADSIZE] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; static char const zeroes[PADSIZE] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; char padbuf[PADSIZE]; const char *padptr; register int i; if (pad == ' ') padptr = blanks; else if (pad == '0') padptr = zeroes; else { for (i = PADSIZE; --i >= 0; ) padbuf[i] = pad; padptr = padbuf; } for (i = count; i >= PADSIZE; i -= PADSIZE) if (sputn(padptr, PADSIZE) != PADSIZE) return EOF; if (i > 0 && sputn(padptr, i) != i) return EOF; return pad; }
void FileContainer::writeCompressed(const void* ptr, unsigned int size) { uLongf CompressedSpace = size * 2; Bytef* CompressedSpaceBuffer = (Bytef*)malloc(CompressedSpace); compress(CompressedSpaceBuffer, &CompressedSpace, (Bytef*)ptr, size); sputn((const char*)&CompressedSpace, sizeof(CompressedSpace)); sputn((const char*)CompressedSpaceBuffer, (unsigned int)CompressedSpace); free(CompressedSpaceBuffer); }
iconvstreambuf::int_type iconvstreambuf::overflow(int_type c) { log_debug("overflow(" << c << ')'); if (sink == 0) { log_error("no sink"); return traits_type::eof(); } else if (pptr() == 0 || pptr() == buffer) { log_debug("empty put-area"); if (c != traits_type::eof()) { if (pptr() == 0) { log_debug("initialize buffer"); setp(buffer, buffer + (sizeof(buffer) - 1)); } *pptr() = (char_type)c; pbump(1); } return 0; } else { char outbuf[sizeof(buffer)*2];; size_t inbytesleft = pptr() - buffer; if (c != traits_type::eof()) { *pptr() = (char_type)c; ++inbytesleft; } size_t outbytesleft = sizeof(outbuf); ICONV_CONST char* inbufptr = buffer; char* outbufptr = outbuf; // convert as many charachters as possible log_debug("iconv(...) " << inbytesleft << " bytes"); bool iconv_ret = conv.convert(&inbufptr, &inbytesleft, &outbufptr, &outbytesleft); pos += inbufptr - buffer; iconv_ret = iconv_ret || errno == EINVAL || errno == E2BIG; log_debug("pass " << outbufptr - outbuf << " bytes to sink"); sink->write(outbuf, outbufptr - outbuf); if (sink->fail()) { log_warn("sink failed"); return traits_type::eof(); } log_debug("reinitialize put area"); setp(buffer, buffer + (sizeof(buffer) - 1)); if (!iconv_ret) { switch (mode) { case iconvstreambuf::mode_skip: log_warn("convert failed, skipping byte"); --inbytesleft; ++inbufptr; iconv_ret = true; break; case iconvstreambuf::mode_throw: log_warn("convert failed, throwing exception"); throw iconv_error(pos); default: return traits_type::eof(); } } // move left bytes to the start of buffer and reinitialize buffer if (inbytesleft > 0) { log_debug("move " << inbytesleft << " bytes to the start"); sputn(inbufptr, inbytesleft); } return 0; } }
void FileContainer::write(const std::string& str) { UINT size = (UINT)str.size(); sputn((const char*)&size, sizeof(size)); sputn(str.c_str(), size); }
void FileContainer::write(const void* ptr, unsigned int size) { sputn((const char*)ptr, size); }
iconvstreambuf::int_type iconvstreambuf::overflow(int_type c) { log_debug("overflow(" << c << ')'); if (sink == 0) { log_error("no sink"); return traits_type::eof(); } else if (pptr() == 0 || pptr() == buffer) { log_debug("empty put-area"); if (c != traits_type::eof()) { if (pptr() == 0) { log_debug("initialize buffer"); setp(buffer, buffer + (sizeof(buffer) - 1)); } *pptr() = (char_type)c; pbump(1); } return 0; } else { char outbuf[sizeof(buffer)*2];; size_t inbytesleft = pptr() - buffer; if (c != traits_type::eof()) { *pptr() = (char_type)c; ++inbytesleft; } size_t outbytesleft = sizeof(outbuf); ICONV_CONST char* inbufptr = buffer; char* outbufptr = outbuf; // convert as many charachters as possible log_debug("iconv(" << cd << ") " << inbytesleft << " bytes"); size_t s = iconv(cd, &inbufptr, &inbytesleft, &outbufptr, &outbytesleft); if (s < 0 && errno != 0 && errno != EINVAL && errno != E2BIG) { log_warn("convert failed"); return traits_type::eof(); } log_debug("pass " << outbufptr - outbuf << " bytes to sink"); sink->write(outbuf, outbufptr - outbuf); if (sink->fail()) { log_warn("sink failed"); return traits_type::eof(); } log_debug("reinitialize put area"); setp(buffer, buffer + (sizeof(buffer) - 1)); // move left bytes to the start of buffer and reinitialize buffer if (inbytesleft > 0) { log_debug("move " << inbytesleft << " bytes to the start"); sputn(inbufptr, inbytesleft); } return 0; } }