本文整理汇总了C++中filebuf::len方法的典型用法代码示例。如果您正苦于以下问题:C++ filebuf::len方法的具体用法?C++ filebuf::len怎么用?C++ filebuf::len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类filebuf
的用法示例。
在下文中一共展示了filebuf::len方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: convert
filebuf IconvWrapper::convert(const filebuf& input)
{
size_t ret = 0;
char* BUFFER;
size_t BUFFSIZE;
size_t inbytesleft = 0;
size_t outbytesleft = 0;
char* input_ptr = 0;
char* output_ptr = 0;
char* output = 0; // The final result will go here...
size_t output_size = 0;
// How much data should be put avaiable to iconv?
// How about twice as much as input's length?
inbytesleft = input.len();
BUFFSIZE = 2 * inbytesleft;
outbytesleft = BUFFSIZE;
// Get memory to hold our converted data.
BUFFER = new char[ BUFFSIZE ];
input_ptr = (char*)input.current;
output_ptr = BUFFER;
ret = iconv( cd,
&input_ptr, &inbytesleft,
&output_ptr, &outbytesleft);
if (ret == size_t(-1)) {
delete[] BUFFER;
throw IconvError("While converting...");
}
// Got here? So everything went fine.
// Copy data to a more memory-efficient location
output_size = BUFFSIZE - outbytesleft;
output = new char[output_size];
memcpy(output, BUFFER, output_size);
delete[] BUFFER;
return filebuf(output, output_size);
}
示例2: decompress
filebuf decompress(filebuf data)
{
/* This code is based on zLib's usage example, available
* at http://www.zlib.net/zlib_how.html
*/
// Zlib stuff
int ret;
z_stream strm;
unsigned int have;
// Outputing stuff
size_t OUTBUF_LEN = DECOMPRESS_RESERVE;
char outbuf[OUTBUF_LEN];
std::vector<char> accbuf; // accumulator buffer
accbuf.reserve(DECOMPRESS_RESERVE);
if (data.len() == 0) {
// Empty filebufs result in empty filebufs.
return filebuf();
}
/* allocate inflate state */
const int windowBits = 15 + 16; /* 15 is the default value for
* windownBits in inflateInit().
*
* Bits+16 means "use gzip" in
* zlib >= 1.2. Use 32 instead
* of 16 to force automatic header
* detection for gzip/zlib.
*/
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = data.len();
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, windowBits);
if (ret != Z_OK){
throw ZLibException("in inflateInit2", ret);
}
strm.avail_in = data.len();
strm.next_in = (unsigned char*) data.current;
/* decompress until deflate stream ends or an error happens */
do {
/* Remember: all input data _is_ available! All we can and
* must do is loop until zlib reports that it has
* finished or an error occurs.
*/
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = OUTBUF_LEN;
strm.next_out = (unsigned char*) outbuf;
ret = inflate(&strm, Z_NO_FLUSH);
// make sure state not clobbered
if (ret == Z_STREAM_ERROR) {
throw ZLibException("in inflate", ret);
}
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; // and fall through
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
throw ZLibException("in inflate", ret);
}
have = OUTBUF_LEN - strm.avail_out;
accbuf.insert( accbuf.end(),
outbuf,
&outbuf[have]);
} while (strm.avail_out == 0);
/* done when inflate() says it's done */
} while(ret != Z_STREAM_END);
/* clean up */
(void)inflateEnd(&strm);
/* Ugly and lazy copy to an array */
char* final_buffer = new char[accbuf.size()];
std::copy(accbuf.begin(), accbuf.end(), final_buffer);
return filebuf(final_buffer, accbuf.size());
}