r22262 - gnucash/branches/2.4/src/gnome-utils - [22260]Rewrite the tip-of-the-day parsing logic

Geert Janssens gjanssens at code.gnucash.org
Sat Jul 7 12:10:55 EDT 2012


Author: gjanssens
Date: 2012-07-07 12:10:54 -0400 (Sat, 07 Jul 2012)
New Revision: 22262
Trac: http://svn.gnucash.org/trac/changeset/22262

Modified:
   gnucash/branches/2.4/src/gnome-utils/dialog-totd.c
Log:
[22260]Rewrite the tip-of-the-day parsing logic
Since commit 22246, an empty tip of the day appears in the list of tips.
This is due to the way the file is parsed.
This commit rewrites the parsing logic to avoid such problems. As an
added bonus, you can now add newlines in tips by using '\n' in the
string.

Modified: gnucash/branches/2.4/src/gnome-utils/dialog-totd.c
===================================================================
--- gnucash/branches/2.4/src/gnome-utils/dialog-totd.c	2012-07-07 16:06:36 UTC (rev 22261)
+++ gnucash/branches/2.4/src/gnome-utils/dialog-totd.c	2012-07-07 16:10:54 UTC (rev 22262)
@@ -173,7 +173,7 @@
 static gboolean
 gnc_totd_initialize (void)
 {
-    gchar *filename, *contents, *new, *found;
+    gchar *filename, *contents, *new;
     gsize length;
     GError *error;
 
@@ -190,44 +190,43 @@
         g_free(filename);
         return FALSE;
     }
+    g_free(filename);
 
-    /* Replace maximal substrings of more than two newlines by \n\n,
-     * remove leading and trailing \n\n */
-    while ((found = strstr(contents, "\n\n\n")) != NULL)
+    /* Split into multiple strings. Due to the nature of the
+     * tip list file, this can contain empty strings */
+    tip_list = g_strsplit(contents, "\n", 0);
+    g_free(contents);
+    contents = NULL;
+
+    /* Remove the empty strings */
+    for (tip_count = 0; tip_list[tip_count] != NULL; tip_count++)
     {
-        *found++ = '\0';
-        while (*found == '\n') found++;
-        if (*contents && *found)
+        if (*tip_list[tip_count]!='\0')
         {
-            /* put \n\n between the two nonempty parts */
-            new = g_strdup_printf("%s\n\n%s", contents, found);
-            g_free(contents);
-            contents = new;
+            g_strstrip(tip_list[tip_count]);
+            if (!contents)
+                contents = g_strdup (tip_list[tip_count]);
+            else
+            {
+                new = g_strjoin ("\n", contents, tip_list[tip_count], NULL);
+                g_free (contents);
+                contents = new;
+            }
         }
-        else if (*found)
-        {
-            /* remove leading newlines */
-            new = g_strdup(found);
-            g_free(contents);
-            contents = new;
-        }
     }
 
-    /* Split into multiple strings */
-    tip_list = g_strsplit(contents, "\n\n", 0);
+    /* Split cleaned up contents into multiple strings again */
+    g_strfreev (tip_list);
+    tip_list = g_strsplit(contents, "\n", 0);
 
     /* Convert any escaped characters while counting the strings */
     for (tip_count = 0; tip_list[tip_count] != NULL; tip_count++)
     {
-
-        g_strstrip(tip_list[tip_count]);
-        new = g_strcompress(g_strdelimit(tip_list[tip_count], "\n", ' '));
+        new = g_strcompress(tip_list[tip_count]);
         g_free(tip_list[tip_count]);
         tip_list[tip_count] = new;
     }
 
-    g_free(contents);
-    g_free(filename);
 
     /* Don't continue when no tips were found, to prevent
      * gnc_new_tip_number doesn't handle that case (it would try to



More information about the gnucash-changes mailing list