[Xfce4-commits] r25740 - in libexo/trunk: . docs/reference docs/reference/tmpl exo

Benedikt Meurer benny at xfce.org
Tue May 22 18:15:32 CEST 2007


Author: benny
Date: 2007-05-22 16:15:32 +0000 (Tue, 22 May 2007)
New Revision: 25740

Modified:
   libexo/trunk/ChangeLog
   libexo/trunk/configure.in.in
   libexo/trunk/docs/reference/exo-sections.txt
   libexo/trunk/docs/reference/tmpl/exo-string.sgml
   libexo/trunk/exo/exo-string.c
   libexo/trunk/exo/exo-string.h
   libexo/trunk/exo/exo.symbols
Log:
2007-05-22	Benedikt Meurer <benny at xfce.org>

	* configure.in.in, exo/exo-string.{c,h}, exo/exo.symbols: Add new
	  function exo_strdup_strftime(), which is an UTF-8 aware wrapper
	  for strftime().
	* docs/reference/: Update reference manual.




Modified: libexo/trunk/ChangeLog
===================================================================
--- libexo/trunk/ChangeLog	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/ChangeLog	2007-05-22 16:15:32 UTC (rev 25740)
@@ -1,5 +1,12 @@
 2007-05-22	Benedikt Meurer <benny at xfce.org>
 
+	* configure.in.in, exo/exo-string.{c,h}, exo/exo.symbols: Add new
+	  function exo_strdup_strftime(), which is an UTF-8 aware wrapper
+	  for strftime().
+	* docs/reference/: Update reference manual.
+
+2007-05-22	Benedikt Meurer <benny at xfce.org>
+
 	* configure.in.in: Prefer -lmd over -lmd5.
 	* docs/reference/: Update reference manual.
 

Modified: libexo/trunk/configure.in.in
===================================================================
--- libexo/trunk/configure.in.in	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/configure.in.in	2007-05-22 16:15:32 UTC (rev 25740)
@@ -124,6 +124,26 @@
 AC_SEARCH_LIBS([MD5Init], [md md5 c], \
                [AC_DEFINE([HAVE_MD5INIT], [1], [Define if MD5Init present])])
 
+dnl ***************************************
+dnl *** Check for strftime() extensions ***
+dnl ***************************************
+AC_TRY_RUN([
+  #include <string.h>
+  #include <time.h>
+  int
+  main (int argc, char **argv)
+  {
+    struct tm tm;
+    char      buffer[16];
+    tm.tm_year = 81;
+    if (strftime (buffer, 16, "%EY", &tm) == 4 && strcmp (buffer, "1981") == 0)
+      return 0;
+    return 1;
+  }
+], [
+  AC_DEFINE([HAVE_STRFTIME_EXTENSION], 1, [Define if strftime supports %E and %O modifiers.])
+])
+
 dnl ******************************
 dnl *** Check for i18n support ***
 dnl ******************************

Modified: libexo/trunk/docs/reference/exo-sections.txt
===================================================================
--- libexo/trunk/docs/reference/exo-sections.txt	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/docs/reference/exo-sections.txt	2007-05-22 16:15:32 UTC (rev 25740)
@@ -442,6 +442,7 @@
 exo_str_elide_underscores
 exo_str_is_equal
 exo_str_replace
+exo_strdup_strftime
 exo_strndupv
 exo_intern_string
 exo_intern_static_string

Modified: libexo/trunk/docs/reference/tmpl/exo-string.sgml
===================================================================
--- libexo/trunk/docs/reference/tmpl/exo-string.sgml	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/docs/reference/tmpl/exo-string.sgml	2007-05-22 16:15:32 UTC (rev 25740)
@@ -47,6 +47,16 @@
 @Returns: 
 
 
+<!-- ##### FUNCTION exo_strdup_strftime ##### -->
+<para>
+
+</para>
+
+ at format: 
+ at tm: 
+ at Returns: 
+
+
 <!-- ##### FUNCTION exo_strndupv ##### -->
 <para>
 

Modified: libexo/trunk/exo/exo-string.c
===================================================================
--- libexo/trunk/exo/exo-string.c	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/exo/exo-string.c	2007-05-22 16:15:32 UTC (rev 25740)
@@ -1,6 +1,6 @@
 /* $Id$ */
 /*-
- * Copyright (c) 2004-2005 os-cillation e.K.
+ * Copyright (c) 2004-2007 os-cillation e.K.
  *
  * Written by Benedikt Meurer <benny at xfce.org>.
  *
@@ -175,6 +175,187 @@
 
 
 /**
+ * exo_strdup_strftime:
+ * @format : format string to pass to strftime(3). See the strftime(3) documentation
+ *           for details.
+ * @tm     : date/time, in struct tm format.
+ *
+ * Cover for standard date-and-time-formatting routine strftime that returns
+ * a newly-allocated string of the correct size. The caller is responsible
+ * to free the returned string using g_free() when no longer needed.
+ *
+ * Besides the buffer management, there are two differences between this
+ * and the library strftime:
+ *
+ * The modifiers "-" and "_" between a "%" and a numeric directive
+ * are defined as for the GNU version of strftime. "-" means "do not
+ * pad the field" and "_" means "pad with spaces instead of zeroes".
+ *
+ * Non-ANSI extensions to strftime are flagged at runtime with a
+ * warning, so it's easy to notice use of the extensions without
+ * testing with multiple versions of the library.
+ *
+ * Return value: a newly allocated string containing the formatted date/time.
+ *
+ * Since: 0.3.3
+ **/
+gchar*
+exo_strdup_strftime (const gchar     *format,
+                     const struct tm *tm)
+{
+  static const gchar C_STANDARD_STRFTIME_CHARACTERS[] = "aAbBcdHIjmMpSUwWxXyYZ";
+  static const gchar C_STANDARD_NUMERIC_STRFTIME_CHARACTERS[] = "dHIjmMSUwWyY";
+  static const gchar SUS_EXTENDED_STRFTIME_MODIFIERS[] = "EO";
+  const gchar       *remainder;
+  const gchar       *percent;
+  gboolean           strip_leading_zeros;
+  gboolean           turn_leading_zeros_to_spaces;
+  GString           *string;
+  gsize              string_length;
+  gchar              code[4];
+  gchar              buffer[512];
+  gchar             *piece;
+  gchar             *result;
+  gchar             *converted;
+  gchar              modifier;
+  gint               i;
+
+  /* Format could be translated, and contain UTF-8 chars,
+   * so convert to locale encoding which strftime uses.
+   */
+  converted = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
+  if (G_UNLIKELY (converted == NULL))
+    return NULL;
+  
+  /* start processing the format */
+  string = g_string_new ("");
+  remainder = converted;
+
+  /* walk from % character to % character */
+  for (;;)
+    {
+      /* look out for the next % character */
+      percent = strchr (remainder, '%');
+      if (percent == NULL)
+        {
+          /* we're done with the format */
+          g_string_append (string, remainder);
+          break;
+        }
+
+      /* append the characters in between */
+      g_string_append_len (string, remainder, percent - remainder);
+
+      /* handle the "%" character */
+      remainder = percent + 1;
+      switch (*remainder)
+        {
+        case '-':
+          strip_leading_zeros = TRUE;
+          turn_leading_zeros_to_spaces = FALSE;
+          remainder++;
+          break;
+
+        case '_':
+          strip_leading_zeros = FALSE;
+          turn_leading_zeros_to_spaces = TRUE;
+          remainder++;
+          break;
+
+        case '%':
+          g_string_append_c (string, '%');
+          remainder++;
+          continue;
+
+        case '\0':
+          g_warning ("Trailing %% passed to exo_strdup_strftime");
+          g_string_append_c (string, '%');
+          continue;
+
+        default:
+          strip_leading_zeros = FALSE;
+          turn_leading_zeros_to_spaces = FALSE;
+          break;
+        }
+
+      modifier = 0;
+      if (strchr (SUS_EXTENDED_STRFTIME_MODIFIERS, *remainder) != NULL)
+        {
+          modifier = *remainder++;
+          if (*remainder == 0)
+            {
+              g_warning ("Unfinished %%%c modifier passed to exo_strdup_strftime", modifier);
+              break;
+            }
+        } 
+      
+      if (strchr (C_STANDARD_STRFTIME_CHARACTERS, *remainder) == NULL)
+        g_warning ("exo_strdup_strftime does not support non-standard escape code %%%c", *remainder);
+
+      /* convert code to strftime format. We have a fixed
+       * limit here that each code can expand to a maximum
+       * of 512 bytes, which is probably OK. There's no
+       * limit on the total size of the result string.
+       */
+      i = 0;
+      code[i++] = '%';
+#ifdef HAVE_STRFTIME_EXTENSION
+      if (modifier != 0)
+        code[i++] = modifier;
+#endif
+      code[i++] = *remainder;
+      code[i++] = '\0';
+      string_length = strftime (buffer, sizeof (buffer), code, tm);
+      if (string_length == 0)
+        {
+          /* we could put a warning here, but there's no
+           * way to tell a successful conversion to
+           * empty string from a failure
+           */
+          buffer[0] = '\0';
+        }
+
+      /* strip leading zeros if requested */
+      piece = buffer;
+      if (strip_leading_zeros || turn_leading_zeros_to_spaces)
+        {
+          if (strchr (C_STANDARD_NUMERIC_STRFTIME_CHARACTERS, *remainder) == NULL)
+            g_warning ("exo_strdup_strftime does not support modifier for non-numeric escape code %%%c%c", remainder[-1], *remainder);
+
+          if (*piece == '0')
+            {
+              while (*++piece == '\0') ;
+              if (!g_ascii_isdigit (*piece))
+                --piece;
+            }
+
+          if (turn_leading_zeros_to_spaces)
+            {
+              memset (buffer, ' ', piece - buffer);
+              piece = buffer;
+            }
+        }
+
+      /* advance */
+      ++remainder;
+
+      /* add this piece */
+      g_string_append (string, piece);
+    }
+  
+  /* Convert the string back into UTF-8 */
+  result = g_locale_to_utf8 (string->str, -1, NULL, NULL, NULL);
+
+  /* cleanup */
+  g_string_free (string, TRUE);
+  g_free (converted);
+
+  return result;
+}
+
+
+
+/**
  * exo_strndupv:
  * @strv  : String vector to duplicate.
  * @num   : Number of strings in @strv to

Modified: libexo/trunk/exo/exo-string.h
===================================================================
--- libexo/trunk/exo/exo-string.h	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/exo/exo-string.h	2007-05-22 16:15:32 UTC (rev 25740)
@@ -1,6 +1,6 @@
 /* $Id$ */
 /*-
- * Copyright (c) 2004-2006 os-cillation e.K.
+ * Copyright (c) 2004-2007 os-cillation e.K.
  *
  * Written by Benedikt Meurer <benny at xfce.org>.
  *
@@ -31,21 +31,24 @@
 
 G_BEGIN_DECLS;
 
-gchar                *exo_str_elide_underscores  (const gchar *text) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+gchar                *exo_str_elide_underscores  (const gchar     *text) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
-gboolean              exo_str_is_equal           (const gchar *a,
-                                                  const gchar *b);
+gboolean              exo_str_is_equal           (const gchar     *a,
+                                                  const gchar     *b);
 
-gchar                *exo_str_replace            (const gchar *str,
-                                                  const gchar *pattern,
-                                                  const gchar *replacement) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+gchar                *exo_str_replace            (const gchar     *str,
+                                                  const gchar     *pattern,
+                                                  const gchar     *replacement) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
-gchar               **exo_strndupv               (gchar      **strv,
-                                                  gint         num) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+gchar                *exo_strdup_strftime        (const gchar     *format,
+                                                  const struct tm *tm) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
-G_CONST_RETURN gchar *exo_intern_string          (const gchar *string);
-G_CONST_RETURN gchar *exo_intern_static_string   (const gchar *string);
+gchar               **exo_strndupv               (gchar          **strv,
+                                                  gint             num) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
+G_CONST_RETURN gchar *exo_intern_string          (const gchar     *string);
+G_CONST_RETURN gchar *exo_intern_static_string   (const gchar     *string);
+
 #if GLIB_CHECK_VERSION(2,9,0)
 #define I_(string) (g_intern_static_string ((string)))
 #else

Modified: libexo/trunk/exo/exo.symbols
===================================================================
--- libexo/trunk/exo/exo.symbols	2007-05-22 15:25:15 UTC (rev 25739)
+++ libexo/trunk/exo/exo.symbols	2007-05-22 16:15:32 UTC (rev 25740)
@@ -291,10 +291,11 @@
 /* exo-string functions */
 #if IN_HEADER(__EXO_STRING_H__)
 #if IN_SOURCE(__EXO_STRING_C__)
-exo_str_elide_underscores
+exo_str_elide_underscores G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
 exo_str_is_equal
-exo_str_replace
-exo_strndupv
+exo_str_replace G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
+exo_strdup_strftime G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
+exo_strndupv G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
 exo_intern_string
 exo_intern_static_string
 #endif



More information about the Xfce4-commits mailing list