[evolution-patches] Unnecessary g_strdup/g_free pair in e-d-s/.../e-weather-source-ccf.c



Seems like somebody misunderstood how strtok_r() works. The third
char** argument needs not point to a char* that points to
anything. strtok_r will overwrite it with another char* anyway.

Patch also adds a strtok_r() implementation if !HAVE_STRTOK_R.

--tml
Index: calendar/ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/ChangeLog,v
retrieving revision 1.498
diff -p -u -2 -r1.498 ChangeLog
--- calendar/ChangeLog	10 Aug 2005 21:29:25 -0000	1.498
+++ calendar/ChangeLog	10 Aug 2005 21:51:37 -0000
@@ -15,4 +15,10 @@
 	of <pthread.h>, these files do not use any pthread API.
 
+	* backends/weather/e-weather-source-ccf.c (strtok_r): Implement
+	here if not present in the C library.
+	(tokenize): Drop unnecessary buffer allocation/deallocation.
+	(date2tm): No localtime_r() in Microsoft's C library, but its
+	localtime() is MT-safe.
+
 2005-08-06  P. S. Chakravarthi	<pchakravarthi novell com>
 
Index: calendar/backends/weather/e-weather-source-ccf.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/backends/weather/e-weather-source-ccf.c,v
retrieving revision 1.2
diff -p -u -2 -r1.2 e-weather-source-ccf.c
--- calendar/backends/weather/e-weather-source-ccf.c	2 Feb 2005 18:44:31 -0000	1.2
+++ calendar/backends/weather/e-weather-source-ccf.c	10 Aug 2005 21:51:37 -0000
@@ -33,4 +33,41 @@
 #define DATA_SIZE 5000
 
+#ifndef HAVE_STRTOK_R
+
+/* strtok_r lifted from glibc. */
+
+static char *
+strtok_r (char *s, const char *delim, char **save_ptr)
+{
+  char *token;
+
+  if (s == NULL)
+    s = *save_ptr;
+
+  /* Scan leading delimiters.  */
+  s += strspn (s, delim);
+  if (*s == '\0')
+    {
+      *save_ptr = s;
+      return NULL;
+    }
+
+  /* Find the end of the token.  */
+  token = s;
+  s = strpbrk (token, delim);
+  if (s == NULL)
+    /* This token finishes the string.  */
+    *save_ptr = strchr (token, '\0');
+  else
+    {
+      /* Terminate the token and make *SAVE_PTR point past it.  */
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  return token;
+}
+
+#endif
+
 static gchar *
 parse_for_url (char *code, char *name, xmlNode *parent)
@@ -107,6 +144,5 @@ tokenize (char *buffer)
 {
 	char *token;
-	char *tokbuf = g_strdup (buffer);
-	char *buffer2 = tokbuf;
+	char *tokbuf;
 	GSList *ret;
 
@@ -115,5 +151,4 @@ tokenize (char *buffer)
 	while ((token = strtok_r (NULL, " \n/", &tokbuf)))
 		ret = g_slist_append (ret, g_strdup (token));
-	g_free (buffer2);
 	return ret;
 }
@@ -126,5 +161,10 @@ date2tm (char *date, struct tm *times)
 	tmp[2] = '\0';
 
+#ifndef G_OS_WIN32
 	localtime_r (&curtime, times);
+#else
+	/* The localtime() in Microsoft's C library is MT-safe */
+	memcpy (times, localtime (&curtime), sizeof (struct tm));
+#endif
 
 	tmp[0] = date[0]; tmp[1] = date[1];


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