Menu

"bvformat" var arg functions revisited...

gwideman
2007-03-03
2013-04-29
  • gwideman

    gwideman - 2007-03-03

    Paul:

    After going down the "where's my bassignvformat" path (and noting the discussion started by jpo234), it occured to me that the following might be an acceptable hack.

    Define a function:
    int bassignvformat_try(bstring b, int trylen, char * fmt, va_list arglist);

    trylen:
    0  : use default calculation for new length
    >0 : try this length

    returns:
    0  = OK,
    -1 = Error
    >= 1 failed due to buffer too short, but try this new length next time.
    (Or use int * trylen and return new trylen value there... which works for bvformat too.)

    So then this can be used by the caller's program in a loop:

        do {
            va_start(arglist, fmt);
            trylen = bassignvformat_try(bstr, trylen, fmt, arglist);
            va_end(arglist);
        } while(trylen > 0);

    ... which can all be turned into a macro, so that what the calling program contain is:

        va_list arglist
        [...]
        BASSIGNVFORMAT(bstr, fmt, arglist)

    which might look acceptably innocuous to users of the library.

    ----------
    So of course I haven't actually tried this because I can see that hacking with variable args is liable to provide an "enjoyable evening of entertainment", one of which I'm already having, and wouldn't want to overdo it :-).

    Regards, Graham

     
    • Paul Hsieh

      Paul Hsieh - 2007-03-04

      Wow!  This is an excellent suggestion!  I really thought there was no saving bvformat, but in fact your suggestion seems to do just that.  I, in fact have gone with bvcformata (bstr, count, fmt, arglist) which appends to bstr, since its the most general (also I made the return code < BSTR_ERR rather than >=1 as you suggest).  Then I implemented a bvformata(ret,bstr,fmt,lastarg) macro which does things in a manner very similar to what you suggest.  It all seems to work well.  I've checked in an implementation into the CVS tree, if you want to try it out.  It will be made generally available in the next release.  Thanks for the excellent insight.

      --
      Paul

       
      • gwideman

        gwideman - 2007-03-04

        That's great -- I look forward to trying this -- that'll make error message functions *much* cleaner! 
        And nothing like writing an explanation of why it *can't* be done (as I did in jpo234'sthread) to cause the brain to think up ways that w..e..l..l maybe there's a workaround :-).
        Not only that, now you've produced some sample code for handling the whole "ellipsis-to-va_list" business. Double bonus!

        Graham

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.