I'm currently using a script which validates an invalid XML
file or string using tidy library
.
A sample code to test it's running:
#include <tidy.h>
#include <tidybuffio.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv )
{
const char* input = "<title>Foo</title><p>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
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 )
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;
}
on running
gcc -I/usr/include/tidy tidy_example.c
I get this output on my terminal:
/tmp/cclFfP4I.o: In function
main': tidy_exa.c:(.text+0x6e): undefined reference to
tidyCreate' tidy_exa.c:(.text+0x9e): undefined reference totidyOptSetBool' tidy_exa.c:(.text+0xba): undefined reference to
tidySetErrorBuffer' tidy_exa.c:(.text+0xd6): undefined reference totidyParseString' tidy_exa.c:(.text+0xeb): undefined reference to
tidyCleanAndRepair' tidy_exa.c:(.text+0x100): undefined reference totidyRunDiagnostics' tidy_exa.c:(.text+0x11f): undefined reference to
tidyOptSetBool' tidy_exa.c:(.text+0x149): undefined reference totidySaveBuffer' tidy_exa.c:(.text+0x1a6): undefined reference to
tidyBufFree' tidy_exa.c:(.text+0x1b2): undefined reference totidyBufFree' tidy_exa.c:(.text+0x1be): undefined reference to
tidyRelease' collect2: error: ld returned 1 exit status
Any Idea as to how to resolve this issue, or any other library to do the same thing on a file or a string (invalid XML) in c/c++.
Any suggestions will also be welcomed.