我在我的Ubuntu機器上安裝了LZO,我想用ti來壓縮char *類型的字符串。LZO compress char *
在這個例子中的文件,我發現這個代碼片斷(我已經編輯它只是一點點我的應用程序):
int r;
lzo_bytep in;
lzo_bytep out;
lzo_voidp wrkmem;
lzo_uint in_len;
lzo_uint out_len;
lzo_uint new_len;
int strToCompressLen; // I have added this
/*
* Step 1: initialize the LZO library
*/
if (lzo_init() != LZO_E_OK)
{
cout << "internal error - lzo_init() failed !!!" << endl;
cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
//return 4;
}
// here I get the data I want to compress
char* imageData = (char*)imageIn->getFrame();
/*
* Step 2: allocate blocks and the work-memory
*/
strToCompressLen = strlen(imageData);
in = (lzo_bytep) xmalloc(strToCompressLen);
out = (lzo_bytep) xmalloc((strToCompressLen + strToCompressLen/16 + 64 + 3));
wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
if (in == NULL || out == NULL || wrkmem == NULL)
{
cout << "out of memory" << endl;
//return 3;
}
/*
* Step 3: prepare the input block that will get compressed.
* We just fill it with zeros in this example program,
* but you would use your real-world data here.
*/
in_len = strToCompressLen;
lzo_memset(in,0,in_len);
/*
* Step 4: compress from 'in' to 'out' with LZO1X-1
*/
r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
if (r != LZO_E_OK)
{
/* this should NEVER happen */
cout << "internal error - compression failed: " << r << endl;
//return 2;
}
/* check for an incompressible block */
if (out_len >= in_len)
{
cout << "This block contains incompressible data." << endl;
//return 0;
}
但它所做的只是填零。我需要壓縮一個char *變量。
我想我需要編輯這些行:
in_len = strToCompressLen;
lzo_memset(in,0,in_len);
我有我想在這個變量來壓縮字符串:
char* imageData = (char*)imageIn->getFrame();
我需要將它轉換爲其他類型?
LZO的文檔不是很有幫助(或者我不能正確使用它)。
'<<'是'C'中的左移運算符。您正在使用它並放棄結果...所以,最好的情況是,這些陳述沒有任何影響。 – pmg 2010-11-18 22:05:41
這是C++代碼。 – 2010-11-18 22:16:40
'c'標籤被移除 – pmg 2010-11-18 22:29:51