Deutsch   English   Français   Italiano  
<utl4m5$3700q$6@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.os.linux.advocacy
Subject: Re: Tabs As Syntax
Date: Fri, 22 Mar 2024 23:36:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <utl4m5$3700q$6@dont-email.me>
References: <17be73f7d87a06e9$246570$4075406$802601b3@news.usenetexpress.com>
	<utejt0$1fq95$1@dont-email.me> <uterkv$1haln$2@dont-email.me>
	<l624u3Fn16lU13@mid.individual.net> <uthf4s$28e7g$1@dont-email.me>
	<l64bi6F2m1hU17@mid.individual.net> <utjt1t$2tns5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 22 Mar 2024 23:36:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="50afb5c9c454d4fc23edeea8f731b72c";
	logging-data="3375130"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+RcR+x5aL6IB1QTZmlnwGG"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:lmYTsYey4qPuf/ZFxXA3TxKfY6c=
Bytes: 3110

On Fri, 22 Mar 2024 08:20:11 -0400, Chris Ahlstrom wrote:

> operator << is useful with std::ostringstream when you want to avoid
> having deal with allocating enough output space. Still, I use this a
> lot:
> 
>     char temp[32];
>     snprintf(temp, sizeof temp, "We have %d idiots here", idcount);
> 
> Sometime a printf specification is just easier to read.

I wrote this a long time ago, for use in some projects in C++. You can
tell it was a long time ago, from the checking for glibc versions:

std::string string_printf
  (
    const char * Format,
    ...
  )
  /* does an sprintf of its remaining arguments according to Format,
    returning the formatted result. */
  {
    std::string Result;
    char * TempBuf;
    ulong TempBufSize;
    TempBuf = nil;
    TempBufSize = 128; /* something reasonable to begin with */
    for (;;)
      {
        TempBuf = (char *)malloc(TempBufSize);
        if (TempBuf == nil)
            break;
        va_list Args;
        va_start(Args, Format);
        const long BufSizeNeeded = vsnprintf(TempBuf, TempBufSize, Format, Args);
          /* not including trailing null */
        va_end(Args);
        if (BufSizeNeeded >= 0 and BufSizeNeeded < TempBufSize)
          {
          /* the whole thing did fit */
            TempBufSize = BufSizeNeeded; /* not including trailing null */
            break;
          } /*if*/
        free(TempBuf);
        if (BufSizeNeeded < 0)
          {
          /* glibc < 2.1 */
            TempBufSize *= 2; /* try something bigger */
          }
        else
          {
          /* glibc >= 2.1 */
            TempBufSize = BufSizeNeeded + 1;
              /* now I know how much I need, including trailing null */
          } /*if*/
      } /*for*/
    if (TempBuf != nil)
      {
        Result.append(TempBuf, TempBufSize);
        free(TempBuf);
      } /*if*/
    return
        Result;
  } /*string_printf*/