想用cocos2d-x-2.1.4调用tidy html库来整理html,发现一个问题,就是
用:TidyBuffer 定义变量output,但是是output.bp是byte格式,如何转换成CCLabelTTF格式,就需要用CCString来转换。
转换核心代码如下:
//转换
CCString* ns=CCString::createWithFormat("%s",(char *)output.bp);
const char *mystring= ns->getCString();
//显示
CCLabelTTF* pLabel = CCLabelTTF::create(mystring,"Arial",24);
#include "tidy/tidy.h";
#include "tidy/tidybuffio.h";
const char* input = "<title>Hello</title><p>World!";
TidyBuffer output = {0};
TidyBuffer errbuf = {0};
int rc = -1;
Bool ok;
// Initialize "document"
TidyDoc tdoc = tidyCreate();
CCLog( "Tidying:\t%s\n",input );
// Convert to XHTML
ok = tidyOptSetBool( tdoc,TidyXhtmlOut,yes );
if ( ok )
rc = tidySetErrorBuffer( tdoc,&errbuf ); // Capture diagnostics
if ( rc >= 0 )
rc = tidyParseString( tdoc,input ); // Parse the input
if ( rc >= 0 )
rc = tidyCleanAndRepair( tdoc ); // Tidy it up!
if ( rc >= 0 )
rc = tidyRunDiagnostics( tdoc ); // Kvetch
if ( rc > 1 ) // If error,force output.
rc = ( tidyOptSetBool(tdoc,TidyForceOutput,yes) ? rc : -1 );
if ( rc >= 0 )
rc = tidySaveBuffer( tdoc,&output ); // Pretty Print
if ( rc >= 0 )
{
if ( rc > 0 )
CCLog( "\nDiagnostics:\n\n%s",errbuf.bp );
CCLog( "\nAnd here is the result:\n\n%s",output.bp );
}
else
CCLog( "A severe error (%d) occurred.\n",rc );
//在释放前转换
CCString* ns=CCString::createWithFormat("%s",(char *)output.bp);
const char *mystring= ns->getCString();
//
tidyBufFree( &output );
tidyBufFree( &errbuf );
tidyRelease( tdoc );
///不要在这里转换,因为已经释放了
//显示
CCLabelTTF* pLabel = CCLabelTTF::create(mystring,24);其他略,在bool HelloWorld::init()对应添加,同时在classes创建一个tidy目录,然后把tidy的src目录和include拷贝到classes的tidy目录就可以了。