Re: g_strdup_strftime



> I think what you'd have to do is allocate an initial size buffer
> that was most likely large enough (say 256 chararacters) - that
> could be automatic on the stack, and then if necessary, increase
> the size of that buffer until strftime succeeded.

Here's a version without a storage leak.

    gchar *
    g_strdup_strftime (const gchar *format, const struct tm *time)
      {
        gchar buffer[256];
        size_t length;
        size_t bigger_buffer_size;
        gchar* bigger_buffer;
        
        /* Note that a too-small buffer passed in to strftime results
         * in either a return value of 0 (as specified by the ISO C
         * standard) or the size of the entire buffer (as done in some
         * historical implementations and in glibc).
         */
        
        /* Handle the case where it fits into a small buffer. */
        length = strftime (buffer, G_N_ELEMENTS (buffer),
                           format, time);
        if ((length != 0 || format[0] == '\0')
            && length < G_N_ELEMENTS (buffer))
          {
            return g_strdup (buffer);
          }

        /* Handle the unusual case where we need a bigger buffer. */
        bigger_buffer_size = G_N_ELEMENTS (buffer);
        while (1)
          {
            bigger_buffer_size *= 2;
            bigger_buffer = g_malloc (bigger_buffer_size);
            length = strftime (bigger_buffer, bigger_buffer_size,
                               format, time);
            if (length != 0 && length < G_N_ELEMENTS (buffer))
              {
                return bigger_buffer;
              }
            g_free (bigger_buffer);
          }
      }

Owen, would you consider adding this? Or should I resubmit it in the form of
a patch?

    -- Darin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]