Results 1 to 9 of 9

Thread: Bring va_list args; to a QStringList or a qmap

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Bring va_list args; to a QStringList or a qmap

    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 ,...)

    Qt Code:
    1. PHP_RINIT_FUNCTION(xsl)
    2. {
    3. xsltSetGenericErrorFunc(NULL, php_libxml_error_handler);
    4. return SUCCESS;
    5. }
    6.  
    7. PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...)
    8. {
    9. va_list args;
    10. va_start(args, msg);
    11. php_libxml_internal_error_handler(PHP_LIBXML_ERROR, ctx, &msg, args);
    12. va_end(args);
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Quote Originally Posted by patrik08
    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:
    Qt Code:
    1. printf( "some string\n" );
    To copy to clipboard, switch view to plain text mode 
    but you can also do this:
    Qt Code:
    1. printf( "%s:%d: %s\n", file, line, msg );
    To copy to clipboard, switch view to plain text mode 
    In both cases you call the same printf() and you can do this because it was declared like this:
    Qt Code:
    1. int printf( const char *format, ... );
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void foo( int something, int somethingelse, ... );
    To copy to clipboard, switch view to plain text mode 
    you need:
    Qt Code:
    1. void foo( int something, int somethingelse, ... )
    2. {
    3. va_list args;
    4. va_start( args, somethingelse );
    5. // now you can use args
    6. va_end( args ); // clean up
    7. }
    To copy to clipboard, switch view to plain text mode 

    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

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Quote Originally Posted by jacek
    you need:
    Qt Code:
    1. void foo( int something, int somethingelse, ... )
    2. {
    3. va_list args;
    4. va_start( args, somethingelse );
    5. // now you can use args
    6. va_end( args ); // clean up
    7. }
    To copy to clipboard, switch view to plain text mode 

    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 ,...)

    Qt Code:
    1. static void foo( int something, int somethingelse, ... )
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    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 ....

    Qt Code:
    1. /* from class external function */
    2. void qt_libxml_error_handler(void *ctx, const char *msg, ...)
    3. {
    4. char buffer [50];
    5. va_list args;
    6. va_start(args, msg);
    7. /*sprintf (buffer,msg,args);*/
    8. qDebug() << "### qt_libxml_error_handler arg " << msg;
    9. qDebug() << "### qt_libxml_error_handler arg " << args;
    10. qDebug() << "### qt_libxml_error_handler ctx " << ctx;
    11. /*php_libxml_internal_error_handler(PHP_LIBXML_ERROR, ctx, &msg, args);*/
    12. va_end(args);
    13. }
    14.  
    15.  
    16.  
    17. /*class qt function */
    18.  
    19. QByteArray gocharxslt = DATA_CONVERTER.toAscii();
    20. xsltSetGenericErrorFunc(NULL, qt_libxml_error_handler); /* error callback */
    21. cur = xsltParseStylesheetFile( (const xmlChar*)gocharxslt.data() );
    22. doc = xmlParseFile( QFile::encodeName(DATA_FILE_XML) );
    23. outputDoc = xsltApplyStylesheet(cur, doc, params);
    24. xmlFreeDoc( doc ); /* free ram from xml! */
    25. doc = outputDoc; /* swap input and output */
    26. FILE* outfile = fopen( QFile::encodeName( SHORTFILE ), "w" );
    27. xsltSaveResultToFile( outfile, doc, cur );
    28. fclose( outfile );
    29. xsltFreeStylesheet(cur);
    30. xmlFreeDoc( outputDoc );
    31. xsltCleanupGlobals();
    32. xmlCleanupParser();
    To copy to clipboard, switch view to plain text mode 




    php6 make so .... " grep -R "php_libxml_internal_error_handler" * " Tank to grep
    Qt Code:
    1. static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
    2. {
    3. char *buf;
    4. int len, len_iter, output = 0;
    5.  
    6. TSRMLS_FETCH();
    7.  
    8. len = vspprintf(&buf, 0, *msg, ap);
    9. len_iter = len;
    10.  
    11. /* remove any trailing \n */
    12. while (len_iter && buf[--len_iter] == '\n') {
    13. buf[len_iter] = '\0';
    14. output = 1;
    15. }
    16.  
    17. smart_str_appendl(&LIBXML(error_buffer), buf, len);
    18.  
    19. efree(buf);
    20.  
    21. if (output == 1) {
    22. if (LIBXML(error_list)) {
    23. _php_list_set_error_structure(NULL, LIBXML(error_buffer).c);
    24. } else {
    25. switch (error_type) {
    26. case PHP_LIBXML_CTX_ERROR:
    27. php_libxml_ctx_error_level(E_WARNING, ctx, LIBXML(error_buffer).c TSRMLS_CC);
    28. break;
    29. case PHP_LIBXML_CTX_WARNING:
    30. php_libxml_ctx_error_level(E_NOTICE, ctx, LIBXML(error_buffer).c TSRMLS_CC);
    31. break;
    32. default:
    33. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", LIBXML(error_buffer).c);
    34. }
    35. }
    36. smart_str_free(&LIBXML(error_buffer));
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Qt Code:
    1. void qt_libxml_error_handler(void *ctx, const char *msg, ...)
    2. {
    3. va_list args;
    4. va_start(args, msg);
    5. char *buffer = new char[255];
    6. sprintf(buffer, msg, args);
    7. QString message = QString( "Buffer say %1" ).arg( QString::fromAscii(buffer) );
    8. qDebug() << "### qt_libxml_error_handler msg " << message;
    9. va_end(args);
    10. }
    To copy to clipboard, switch view to plain text mode 

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

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

    output....
    Qt Code:
    1. ### qt_libxml_error_handler msg "Buffer say ?┐\♦"
    2. I/O warning : failed to load external entity "file:///C%3A/include/_colonna_dest
    3. ra.xsl"
    4. ### qt_libxml_error_handler msg "Buffer say 4↑▓: file file:///C%3A/include/_c
    5. olonna_destra.xsl line 2009252579 element ?&#9472;♦?F∟?└t
    6. &#9658;?─♦?F ?└t
    7. &#9658;?─♦?↨?▬?G♦?F♦?O♀?N♀?W¶?V¶?G0?F0?O$?N$?W(?V(?G0?T$∟?F0?O,?D$►?n►]??N,?L$►[?F∟_?V
    8. &#8593;?N 3└^?├_?╚ ^?├╠╠╠╠╠╠╠╠╠╠?ý►?D$$UV3÷?t$¶?D$♀ÞÏ
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Quote Originally Posted by patrik08
    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 ??
    Qt Code:
    1. QString message;
    2. message.sprintf( msg, args );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Quote Originally Posted by jacek
    Qt Code:
    1. QString message;
    2. message.sprintf( msg, args );
    To copy to clipboard, switch view to plain text mode 
    Crasch and frozen qt....

    i tested so...

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

    Qt Code:
    1. void qt_libxml_error_handler(void *ctx, const char *msg, ...)
    2. {
    3. va_list args;
    4. va_start(args, msg);
    5. qt_libxml_error_line(ctx, msg, args);
    6. va_end(args);
    7. }
    8.  
    9. void qt_libxml_error_line(void *ctx, const char *msg, va_list ap)
    10. {
    11. char *buf;
    12. int len, len_iter, output = 0;
    13. len = vsprintf( buf , msg, ap);
    14. len_iter = len;
    15.  
    16. if (len > 0) {
    17. qDebug() << "### int is set ";
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    now is running if i cann not fill a qstringlist .. fill a file and remove on class function....

    Qt Code:
    1. void qt_libxml_error_handler(void *ctx, const char *msg, ...)
    2. {
    3. FILE* outfile = fopen( QFile::encodeName( XMLERROR_FILE ), "a" );;
    4. va_list args;
    5. va_start(args, msg);
    6. vfprintf(outfile, msg, args);
    7. va_end(args);
    8. fclose( outfile );
    9. }
    To copy to clipboard, switch view to plain text mode 


    hard work to display error!

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bring va_list args; to a QStringList or a qmap

    Quote Originally Posted by patrik08
    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).

    Qt Code:
    1. void qt_libxml_error_handler(void *ctx, const char *msg, ...)
    2. {
    3. va_list args;
    4. QString message;
    5.  
    6. in size = 256;
    7. char * buf = new char[ size ];
    8.  
    9. while( 1 ) {
    10. va_start(args, msg);
    11. int retval = ::vsnprintf( buf, size, msg, args );
    12. va_end(args);
    13.  
    14. if( -1 < retval && retval < size ) { // everything was OK
    15. message = buf;
    16. break;
    17. }
    18. else if( retval > -1 ) { // buffer too small
    19. size = retval + 1;
    20. delete [] buf;
    21. buf = new char[ size ];
    22. }
    23. else { // error
    24. // ...
    25. break;
    26. }
    27. }
    28.  
    29. delete [] buf;
    30.  
    31. // use message
    32. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to jacek for this useful post:

    patrik08 (18th June 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.