-
Notifications
You must be signed in to change notification settings - Fork 435
Description
I am using TidyHtml to clean up html files in my application in a Linux envorment. I do realize that that libtidy is a C library. I did include it in the extern "C"{} syntax as indicated in my code. This is the example from the TidyHtml website adapted by me to use in a C++ example and I added the printf() function in conditions.
There are two problems. The first starting with tidyOptSetBool() which fails and the printf("%s\n", "1"); line is never reached. The second is
tidyBufFree( &output ); tidyBufFree( &errbuf );
lines cause a Segmentation fault (core dumped).
This is the full code example:
`
#include <stdio.h>
#include <tidy.h>
#include <tidybuffio.h>
#include <stdio.h>
#include <errno.h>
using namespace std;
extern "C" {
int tidyHtml(){
const char* input = "<title>Foo</title>
Foo!";
TidyBuffer output = {0};
TidyBuffer errbuf = {0};
int rc = -1;
Bool ok;
TidyDoc tdoc = tidyCreate(); // Initialize "document"
printf( "Tidying:\t%s\n", input );
ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML
if ( ok ){
rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics
printf("%s\n", "1");
}
if ( rc >= 0 ){
rc = tidyParseString( tdoc, input ); // Parse the input
printf("%s\n", "2");
}
if ( rc >= 0 ){
rc = tidyCleanAndRepair( tdoc ); // Tidy it up!
printf("%s\n", "3");
}
if ( rc >= 0 ){
rc = tidyRunDiagnostics( tdoc ); // Kvetch
printf("%s\n", "4");
}
if ( rc > 1 ){ // If error, force output.
rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 );
printf("%s\n", "5");
}
if ( rc >= 0 ){
rc = tidySaveBuffer( tdoc, &output ); // Pretty Print
printf("%s\n", "6");
}
if ( rc >= 0 )
{
if ( rc > 0 )
printf( "\nDiagnostics:\n\n%s", errbuf.bp );
printf( "\nAnd here is the result:\n\n%s", output.bp );
}
else
printf( "A severe error (%d) occurred.\n", rc );
tidyBufFree( &output );
tidyBufFree( &errbuf );
tidyRelease( tdoc );
return rc;
}
}
int main(int argc, char *argv[]){
tidyHtml();
}`
Here is my compile statement g++ -o main main.cpp -ltidy which reports 0 errors.
and this is output when the application is run:
Tidying: <title>Foo</title><p>Foo! A severe error (-1) occurred. Segmentation fault (core dumped)