Re: [evolution-patches] g_strftime
- From: Jeffrey Stedfast <fejj ximian com>
- To: Mike Kestner <mkestner ximian com>
- Cc: evolution-patches ximian com
- Subject: Re: [evolution-patches] g_strftime
- Date: 23 May 2003 18:36:02 -0400
as it turns out, that was wrong too s/buflen/max/
anyways, attached is a simpler method that doesn't build it
incrementally (which is probably better anyway).
Jeff
On Fri, 2003-05-23 at 18:22, Jeffrey Stedfast wrote:
> oops, forgot to save my emacs buffer before attaching. lets try this
> again.
>
> Jeff
>
> On Fri, 2003-05-23 at 18:13, Jeffrey Stedfast wrote:
> > an idea for implementing a workable strftime implementation that returns
> > a malloc'd buffer. also works around the fact that strftime is Broken By
> > Design (tm).
> >
> > Jeff
--
Jeffrey Stedfast
Evolution Hacker - Ximian, Inc.
fejj ximian com - www.ximian.com
char *
g_strftime (const char *format, const struct tm *tm)
{
const char *start, *inptr;
char *time_str, *buf;
size_t max, n;
GString *str;
char fmt[4];
fmt[0] = '%';
fmt[2] = ' ';
fmt[3] = '\0';
max = 64;
buf = g_malloc (max);
str = g_string_new ("");
inptr = format;
while (*inptr) {
start = inptr;
while (*inptr && *inptr != '%')
inptr++;
g_string_append_len (str, start, inptr - start);
if (*inptr == '\0')
break;
inptr++;
if (*inptr == '%') {
g_string_append_c (str, *inptr++);
} else {
fmt[1] = *inptr++;
retry:
if ((n = strftime (buf, max, fmt, tm)) != 0) {
g_string_append_len (str, buf, n - 1);
} else {
max <<= 1;
buf = g_realloc (buf, max);
goto retry;
}
}
}
g_free (buf);
time_str = str->str;
g_string_free (str, FALSE);
return time_str;
}
char *
g_strftime (const char *format, const struct tm *tm)
{
char *fmt, *buf;
size_t max, n;
max = strlen (format);
fmt = g_alloca (max + 2);
buf = g_stpcpy (fmt, format);
*buf++ = ' ';
*buf = '\0';
max = (max + 2) << 1;
buf = g_malloc (max);
str = g_string_new ("");
while ((n = strftime (buf, max, fmt, tm)) == 0) {
max <<= 1;
buf = g_realloc (buf, max);
}
buf[n - 1] = '\0';
return buf;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]