Re: [evolution-patches] Re: 43485: strftime usage in e-cell-date



On Fri, 2003-05-23 at 16:25, Jeffrey Stedfast wrote:
> probably better to use memcpy, especially since s and buf do not
> overlap.
> 
> also:
> 
> sz = MIN (sz, max - 1);
> memcpy (s, buf, sz);
> s[sz] = '\0';

I know we went off on a tangent looking for the beautiful solution
returning dynamically allocated strings, but in the interest of thinking
small in RC mode I've updated the previous patch to address Jeff's
feedback.  This patch uses memcpy and also uses g_utf8_find_prev_char to
truncate to a string that will fit in the buffer provided.

Maybe this is still wrong though, since the strftime behavior would be
to return 0 and an undefined buffer if the utf8 converted text doesn't
fit in the buffer.  So, I can either commit this patch or one altered to
just return 0 in the sz >= max case.

Mike
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gal/ChangeLog,v
retrieving revision 1.780
diff -u -p -r1.780 ChangeLog
--- ChangeLog	21 May 2003 16:26:15 -0000	1.780
+++ ChangeLog	27 May 2003 16:17:01 -0000
@@ -1,3 +1,9 @@
+2003-05-23  Mike Kestner  <mkestner ximian com>
+
+	* configure.in : bump libtool version for api addition
+	* gal/util/e-util.c (e_utf8_strftime): new, utf8 in/out
+	(e_utf8_strftime_fix_am_pm): new, utf8 in/out
+
 2003-05-21  Mike Kestner  <mkestner ximian com>
 
 	* configure.in : releasing 1.99.6
Index: configure.in
===================================================================
RCS file: /cvs/gnome/gal/configure.in,v
retrieving revision 1.264
diff -u -p -r1.264 configure.in
--- configure.in	21 May 2003 16:28:12 -0000	1.264
+++ configure.in	27 May 2003 16:17:01 -0000
@@ -21,20 +21,20 @@ GAL_API_VERSION=2.0
 
 dnl libtool versioning
 
-dnl gal-1.99.6 = 0
+dnl gal-1.99.7 = 1
 dnl increment if any interfaces have been added; set to 0
 dnl  if any interfaces have been removed. removal has 
 dnl  precedence over adding, so set to 0 if both happened.
-GAL_AGE=0
+GAL_AGE=1
 
-dnl gal-1.99.6 = 1
+dnl gal-1.99.7 = 0
 dnl increment any time the source changes; set to 
 dnl  0 if you increment CURRENT
-GAL_REVISION=1
+GAL_REVISION=0
 
-dnl gal-1.99.6 = 3
+dnl gal-1.99.7 = 4
 dnl increment if the interface has additions, changes, removals.
-GAL_CURRENT=3
+GAL_CURRENT=4
 
 AC_SUBST(GAL_API_VERSION)
 AC_SUBST(GAL_CURRENT)
Index: gal/util/e-util.c
===================================================================
RCS file: /cvs/gnome/gal/gal/util/e-util.c,v
retrieving revision 1.58
diff -u -p -r1.58 e-util.c
--- gal/util/e-util.c	8 May 2003 14:58:57 -0000	1.58
+++ gal/util/e-util.c	27 May 2003 16:17:02 -0000
@@ -768,6 +768,27 @@ size_t e_strftime(char *s, size_t max, c
 #endif
 }
 
+size_t 
+e_utf8_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
+{
+	size_t sz;
+	char *locale_fmt = g_locale_from_utf8(fmt, -1, NULL, &sz, NULL);
+	size_t ret = e_strftime(s, max, locale_fmt, tm);
+	char *buf = g_locale_to_utf8(s, ret, NULL, &sz, NULL);
+	if (sz >= max) {
+		char *tmp = buf + max - 1;
+		tmp = g_utf8_find_prev_char(buf, tmp);
+		if (tmp)
+			sz = tmp - buf;
+		else
+			sz = 0;
+	}
+	memcpy(s, buf, sz);
+	s[sz] = '\0';
+	g_free(locale_fmt);
+	g_free(buf);
+	return sz;
+}
 
 /**
  * Function to do a last minute fixup of the AM/PM stuff if the locale
@@ -828,6 +849,28 @@ size_t e_strftime_fix_am_pm(char *s, siz
 		}
 	}
 	return(ret);
+}
+
+size_t 
+e_utf8_strftime_fix_am_pm(char *s, size_t max, const char *fmt, const struct tm *tm)
+{
+	size_t sz;
+	char *locale_fmt = g_locale_from_utf8(fmt, -1, NULL, &sz, NULL);
+	size_t ret = e_strftime_fix_am_pm(s, max, locale_fmt, tm);
+	char *buf = g_locale_to_utf8(s, ret, NULL, &sz, NULL);
+	if (sz >= max) {
+		char *tmp = buf + max - 1;
+		tmp = g_utf8_find_prev_char(buf, tmp);
+		if (tmp)
+			sz = tmp - buf;
+		else
+			sz = 0;
+	}
+	memcpy(s, buf, sz);
+	s[sz] = '\0';
+	g_free(locale_fmt);
+	g_free(buf);
+	return sz;
 }
 
 /**
Index: gal/util/e-util.h
===================================================================
RCS file: /cvs/gnome/gal/gal/util/e-util.h,v
retrieving revision 1.54
diff -u -p -r1.54 e-util.h
--- gal/util/e-util.h	5 Dec 2002 18:31:30 -0000	1.54
+++ gal/util/e-util.h	27 May 2003 16:17:02 -0000
@@ -196,6 +196,16 @@ size_t   e_strftime		(char              
 				 const char        *fmt,
 				 const struct tm   *tm);
 
+size_t   e_utf8_strftime_fix_am_pm  (char             *s,
+				     size_t            max,
+				     const char       *fmt,
+				     const struct tm  *tm);
+
+size_t   e_utf8_strftime	(char              *s,
+				 size_t             max,
+				 const char        *fmt,
+				 const struct tm   *tm);
+
 /* String to/from double conversion functions */
 gdouble   e_flexible_strtod     (const gchar       *nptr,
 				 gchar            **endptr);
Index: gal/e-table/ChangeLog
===================================================================
RCS file: /cvs/gnome/gal/gal/e-table/ChangeLog,v
retrieving revision 1.901
diff -u -p -r1.901 ChangeLog
--- gal/e-table/ChangeLog	21 May 2003 16:02:59 -0000	1.901
+++ gal/e-table/ChangeLog	27 May 2003 16:17:08 -0000
@@ -1,3 +1,8 @@
+2003-05-23  Mike Kestner  <mkestner ximian com>
+
+	* e-cell-date.c (ecd_get_text): use e_utf8_strftime_fix_am_pm
+	and remove locale to utf8 conversion at end.  [43485]
+
 2003-05-20  Ettore Perazzoli  <ettore ximian com>
 
 	* e-tree.c (tree_canvas_size_allocate): to really fix 42952
Index: gal/e-table/e-cell-date.c
===================================================================
RCS file: /cvs/gnome/gal/gal/e-table/e-cell-date.c,v
retrieving revision 1.13
diff -u -p -r1.13 e-cell-date.c
--- gal/e-table/e-cell-date.c	18 Mar 2003 20:49:59 -0000	1.13
+++ gal/e-table/e-cell-date.c	27 May 2003 16:17:08 -0000
@@ -43,8 +43,8 @@ ecd_get_text(ECellText *cell, ETableMode
 	time_t nowdate = time(NULL);
 	time_t yesdate;
 	struct tm then, now, yesterday;
-	char buf[26];
-	char *temp, *ret_val;
+	char buf[100];
+	char *temp;
 	gboolean done = FALSE;
 
 	if (date == 0) {
@@ -55,7 +55,7 @@ ecd_get_text(ECellText *cell, ETableMode
 	localtime_r (&nowdate, &now);
 
 	if (nowdate - date < 60 * 60 * 8 && nowdate > date) {
-		e_strftime_fix_am_pm (buf, 26, _("%l:%M %p"), &then);
+		e_utf8_strftime_fix_am_pm (buf, 100, _("%l:%M %p"), &then);
 		done = TRUE;
 	}
 
@@ -63,7 +63,7 @@ ecd_get_text(ECellText *cell, ETableMode
 		if (then.tm_mday == now.tm_mday &&
 		    then.tm_mon == now.tm_mon &&
 		    then.tm_year == now.tm_year) {
-			e_strftime_fix_am_pm (buf, 26, _("Today %l:%M %p"), &then);
+			e_utf8_strftime_fix_am_pm (buf, 100, _("Today %l:%M %p"), &then);
 			done = TRUE;
 		}
 	}
@@ -73,15 +73,7 @@ ecd_get_text(ECellText *cell, ETableMode
 		if (then.tm_mday == yesterday.tm_mday &&
 		    then.tm_mon == yesterday.tm_mon &&
 		    then.tm_year == yesterday.tm_year) {
-#if 0
-			if (nowdate - date < 60 * 60 * 12) {
-				e_strftime_fix_am_pm (buf, 26, _("Late Yesterday %l:%M %p"), &then);
-			} else {
-#endif
-				e_strftime_fix_am_pm (buf, 26, _("Yesterday %l:%M %p"), &then);
-#if 0
-			}
-#endif
+			e_utf8_strftime_fix_am_pm (buf, 100, _("Yesterday %l:%M %p"), &then);
 			done = TRUE;
 		}
 	}
@@ -93,7 +85,7 @@ ecd_get_text(ECellText *cell, ETableMode
 			if (then.tm_mday == yesterday.tm_mday &&
 			    then.tm_mon == yesterday.tm_mon &&
 			    then.tm_year == yesterday.tm_year) {
-				e_strftime_fix_am_pm (buf, 26, _("%a %l:%M %p"), &then);
+				e_utf8_strftime_fix_am_pm (buf, 100, _("%a %l:%M %p"), &then);
 				done = TRUE;
 				break;
 			}
@@ -101,26 +93,17 @@ ecd_get_text(ECellText *cell, ETableMode
 	}
 	if (!done) {
 		if (then.tm_year == now.tm_year) {
-			e_strftime_fix_am_pm (buf, 26, _("%b %d %l:%M %p"), &then);
+			e_utf8_strftime_fix_am_pm (buf, 100, _("%b %d %l:%M %p"), &then);
 		} else {
-			e_strftime_fix_am_pm (buf, 26, _("%b %d %Y"), &then);
+			e_utf8_strftime_fix_am_pm (buf, 100, _("%b %d %Y"), &then);
 		}
 	}
-#if 0
-#ifdef CTIME_R_THREE_ARGS
-	ctime_r (&date, buf, 26);
-#else
-	ctime_r (&date, buf);
-#endif
-#endif
 	temp = buf;
 	while ((temp = strstr (temp, "  "))) {
 		memmove (temp, temp + 1, strlen (temp));
 	}
 	temp = e_strdup_strip (buf);
-	ret_val = e_utf8_from_locale_string (temp);
-	g_free (temp);
-	return ret_val;
+	return temp;
 }
 
 static void


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