PDA

View Full Version : Bring va_list args; to a QStringList or a qmap



patrik08
17th June 2006, 20:36
I wand capture error from the http://xmlsoft.org/XSLT/ static libs

To display on a xml/xslt editor in order to communicate the customer where it mistakes on xml or xslt....

I found the error handler from php6 on C# code xslt extension

How i can read va_list args; ? what is? and why a function argument end ,...)



PHP_RINIT_FUNCTION(xsl)
{
xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
return SUCCESS;
}

PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
php_libxml_internal_error_handler(PHP_LIBXML_ERROR , ctx, &msg, args);
va_end(args);
}

jacek
17th June 2006, 22:08
How i can read va_list args; ? what is? and why a function argument end ,...)
On very rare occasions you need a function that can take any number of arguments --- that's when you must use "...".

The most common example is the printf() function. You can pass a single string to it:
printf( "some string\n" );but you can also do this:
printf( "%s:%d: %s\n", file, line, msg );In both cases you call the same printf() and you can do this because it was declared like this:
int printf( const char *format, ... );

Now the problem is: how to access those parameters? You don't know their names. That's where va_list comes in. It allows you, together with va_arg, va_start and va_end macros, to access those anonymous arguments.

With va_start you can initialize a list of arguments (you must pass the last named argument to it), so if you have:
void foo( int something, int somethingelse, ... );you need:
void foo( int something, int somethingelse, ... )
{
va_list args;
va_start( args, somethingelse );
// now you can use args
va_end( args ); // clean up
}

With va_args you can access those arguments' values, but you must know their types a priori.

You can find more here: http://www.cppreference.com/stdother/va_arg.html

patrik08
18th June 2006, 09:19
you need:
void foo( int something, int somethingelse, ... )
{
va_list args;
va_start( args, somethingelse );
// now you can use args
va_end( args ); // clean up
}

With va_args you can access those arguments' values, but you must know their types a priori.

You can find more here: http://www.cppreference.com/stdother/va_arg.html


i suppose static void ... i tested a qt4 class + Q_OBJECT not accept ,...):confused:


static void foo( int something, int somethingelse, ... )

patrik08
18th June 2006, 10:11
i become ... now.......

### qt_libxml_error_handler ctx 0x0
### qt_libxml_error_handler msg "%s"
### qt_libxml_error_handler arg ?┐\♦
### qt_libxml_error_handler ctx 0x0
I/O warning : failed to load external entity "file:///C%3A/include/_header_page.

QString & QString::sprintf ( const char * cformat, ... )
char buffer [50];
QString message = QString::sprintf (buffer,msg,args); /* crach & qt frozen */

How to reformat this on QString::sprintf ??


the qt code ....



/* from class external function */
void qt_libxml_error_handler(void *ctx, const char *msg, ...)
{
char buffer [50];
va_list args;
va_start(args, msg);
/*sprintf (buffer,msg,args);*/
qDebug() << "### qt_libxml_error_handler arg " << msg;
qDebug() << "### qt_libxml_error_handler arg " << args;
qDebug() << "### qt_libxml_error_handler ctx " << ctx;
/*php_libxml_internal_error_handler(PHP_LIBXML_ERRO R, ctx, &msg, args);*/
va_end(args);
}



/*class qt function */

QByteArray gocharxslt = DATA_CONVERTER.toAscii();
xsltSetGenericErrorFunc(NULL, qt_libxml_error_handler); /* error callback */
cur = xsltParseStylesheetFile( (const xmlChar*)gocharxslt.data() );
doc = xmlParseFile( QFile::encodeName(DATA_FILE_XML) );
outputDoc = xsltApplyStylesheet(cur, doc, params);
xmlFreeDoc( doc ); /* free ram from xml! */
doc = outputDoc; /* swap input and output */
FILE* outfile = fopen( QFile::encodeName( SHORTFILE ), "w" );
xsltSaveResultToFile( outfile, doc, cur );
fclose( outfile );
xsltFreeStylesheet(cur);
xmlFreeDoc( outputDoc );
xsltCleanupGlobals();
xmlCleanupParser();






php6 make so .... " grep -R "php_libxml_internal_error_handler" * " Tank to grep :)


static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
{
char *buf;
int len, len_iter, output = 0;

TSRMLS_FETCH();

len = vspprintf(&buf, 0, *msg, ap);
len_iter = len;

/* remove any trailing \n */
while (len_iter && buf[--len_iter] == '\n') {
buf[len_iter] = '\0';
output = 1;
}

smart_str_appendl(&LIBXML(error_buffer), buf, len);

efree(buf);

if (output == 1) {
if (LIBXML(error_list)) {
_php_list_set_error_structure(NULL, LIBXML(error_buffer).c);
} else {
switch (error_type) {
case PHP_LIBXML_CTX_ERROR:
php_libxml_ctx_error_level(E_WARNING, ctx, LIBXML(error_buffer).c TSRMLS_CC);
break;
case PHP_LIBXML_CTX_WARNING:
php_libxml_ctx_error_level(E_NOTICE, ctx, LIBXML(error_buffer).c TSRMLS_CC);
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", LIBXML(error_buffer).c);
}
}
smart_str_free(&LIBXML(error_buffer));
}
}

patrik08
18th June 2006, 10:35
void qt_libxml_error_handler(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
char *buffer = new char[255];
sprintf(buffer, msg, args);
QString message = QString( "Buffer say %1" ).arg( QString::fromAscii(buffer) );
qDebug() << "### qt_libxml_error_handler msg " << message;
va_end(args);
}

Now qDebug() << "### qt_libxml_error_handler msg " << message;
report this ...

I not say how leng .... char *buffer = new char[255];

output....

### qt_libxml_error_handler msg "Buffer say ?┐\♦"
I/O warning : failed to load external entity "file:///C%3A/include/_colonna_dest
ra.xsl"
### qt_libxml_error_handler msg "Buffer say 4↑▓: file file:///C%3A/include/_c
olonna_destra.xsl line 2009252579 element ?─♦?F∟?└t
►?─♦?F ?└t
►?─♦?↨?▬?G♦?F♦?O♀?N♀?W¶?V¶?G0?F0?O$?N$?W(?V( ?G0?T$∟?F0?O,?D$►?n►]??N,?L$►[?F∟_?V
↑?N 3└^?├_?╚ ^?├╠╠╠╠╠╠╠╠╠╠?ý►?D$$UV3÷?t$¶?D$♀Þà 

jacek
18th June 2006, 11:25
QString & QString::sprintf ( const char * cformat, ... )
char buffer [50];
QString message = QString::sprintf (buffer,msg,args); /* crach & qt frozen */

How to reformat this on QString::sprintf ??

QString message;
message.sprintf( msg, args );

patrik08
18th June 2006, 12:52
QString message;
message.sprintf( msg, args );

Crasch and frozen qt....

i tested so...

other sample code go to ...
http://www.opengroup.org/pubs/online/7908799/xsh/vfprintf.html
reference http://www.stylusstudio.com/xsllist/200503/post90770.html



void qt_libxml_error_handler(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
qt_libxml_error_line(ctx, msg, args);
va_end(args);
}

void qt_libxml_error_line(void *ctx, const char *msg, va_list ap)
{
char *buf;
int len, len_iter, output = 0;
len = vsprintf( buf , msg, ap);
len_iter = len;

if (len > 0) {
qDebug() << "### int is set ";
}

}

patrik08
18th June 2006, 13:12
now is running :) if i cann not fill a qstringlist .. fill a file and remove on class function....


void qt_libxml_error_handler(void *ctx, const char *msg, ...)
{
FILE* outfile = fopen( QFile::encodeName( XMLERROR_FILE ), "a" );;
va_list args;
va_start(args, msg);
vfprintf(outfile, msg, args);
va_end(args);
fclose( outfile );
}


:D hard work to display error!

jacek
18th June 2006, 13:31
Crasch and frozen qt....
OK, the problem is that you can't use va_list instead "...", because it will be treated as a single anonymous parameter and indeed vsprintf is a way to go (or rather vsnprintf as it is safer).


void qt_libxml_error_handler(void *ctx, const char *msg, ...)
{
va_list args;
QString message;

in size = 256;
char * buf = new char[ size ];

while( 1 ) {
va_start(args, msg);
int retval = ::vsnprintf( buf, size, msg, args );
va_end(args);

if( -1 < retval && retval < size ) { // everything was OK
message = buf;
break;
}
else if( retval > -1 ) { // buffer too small
size = retval + 1;
delete [] buf;
buf = new char[ size ];
}
else { // error
// ...
break;
}
}

delete [] buf;

// use message
}