Menu

Missing functions

jpo
2006-10-25
2013-04-29
  • jpo

    jpo - 2006-10-25

    Hello all,
    as a first time user of the bstring library I'm missing some functions:

    1) a convenient way to assign c strings to bstrings, e.g.
    extern int bassigncstr (bstring b, const char *str);

    2) vararg capable equivalents to the formatting functions, e.g.:
    extern bstring bvformat (const char * fmt, va_list ap);
    extern int bvformata (bstring b, const char * fmt, va_list ap);
    extern int bassignvformat (bstring b, const char * fmt, va_list ap);

    Regards
      Joerg

     
    • Paul Hsieh

      Paul Hsieh - 2006-10-25

      1) This is a good idea.  I've added bassigncstr() and bassignblk() functions.  They will
      appear in the next version of the library.  Email me with your full name if you want to be acknowledged in the documentation.

      2) Unfortunately there is not an easy way to implement vararg versions of the format functions.  The problem is that the ANSI standard says that the va_arg() sequences can only be called once per va_start() call.  But the Bstrlib algorithm is to guess at the output length and iterate the process over and over until the result fits in the destination bstring.  This is not a minor pedantic ANSI point, as real compilers have been obvserved to fail at various attempts at working around this issue.  With C99 va_copy() could be used, however Bstrlib usage is not limited to C99 compilers.

       
    • jpo

      jpo - 2006-10-26

      Hello Paul,
      thanks for caring about my points.
      Regarding 1): No, this would elevate me way beyond what's reasonable for voicing an idea.
      Regarding 2): Do you have to do repeated va_arg() calls? Something like

      <PRE>
      va_list ap;

      va_start(ap, fmt);
      for (i = 0; i < 100; i++) {
        vsnprintf (buf, sizeof(buf), format, ap);
      }
      va_end(ap);
      </PRE>

      should be perfectly legal, shouldn't it?

      Regards
        Joerg

       
    • jpo

      jpo - 2006-10-26

      Hello Paul,
      please forget my previous babbling about stdarg. I just reread your post and my "solution" is obviously just a bug. However, http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html says:
      "Multiple traversals, each bracketed by va_start() ... va_end(), are possible.", which is exactly what you want, isn't it?

      Regards
        Joerg

       
    • gwideman

      gwideman - 2007-03-03

      In case anyone else stumbles in here looking for the same thing...

      http://c-faq.com/   FAQ 15.12
      http://c-faq.com/varargs/handoff.html

      ... appears to discuss this case. "The only real restriction on this technique is that a function like verror can scan the arguments just once; there is no way for it to reinvoke va_start."

      Ie: The problem seems to be that the caller of bassignvformat would be bracketing the call with va_start...va_end (in order to have a va_list to pass in), so internally bassignvformat would be restricted to one scan through the variables, whereas Paul's strategy needs multiple passes.

      This is a significant annoyance, as I'm sure within 2 days of encountering bstrings the thought occurs how great would be:

        my_log_errormessage(char *fmt, ...)

      that internally calls bassignvformat or friends to do the work.

      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.