SF.net SVN: geany:[3267] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Fri Nov 21 17:40:04 UTC 2008


Revision: 3267
          http://geany.svn.sourceforge.net/geany/?rev=3267&view=rev
Author:   eht16
Date:     2008-11-21 17:40:04 +0000 (Fri, 21 Nov 2008)

Log Message:
-----------
Attempt to make utils_get_date_time() UTF-8 safe and add it to the plugin API.
Fix misnamed str_casecmp() function in the plugin API, sorry.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/plugins/export.c
    trunk/plugins/saveactions.c
    trunk/src/callbacks.c
    trunk/src/plugindata.h
    trunk/src/plugins.c
    trunk/src/utils.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/ChangeLog	2008-11-21 17:40:04 UTC (rev 3267)
@@ -3,6 +3,11 @@
  * src/main.c:
    Try to fix some problems when opening files with non-Ascii characters
    on Windows from the command line.
+ * plugins/export.c, plugins/saveactions.c, src/callbacks.c,
+   src/plugindata.h, src/plugins.c, src/utils.c:
+   Attempt to make utils_get_date_time() UTF-8 safe and add it to the
+   plugin API.
+   Fix misnamed str_casecmp() function in the plugin API, sorry.
 
 
 2008-11-21  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>

Modified: trunk/plugins/export.c
===================================================================
--- trunk/plugins/export.c	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/plugins/export.c	2008-11-21 17:40:04 UTC (rev 3267)
@@ -281,20 +281,12 @@
 }
 
 
-static const gchar *get_date(gint type)
+static gchar *get_date(gint type)
 {
-	static gchar str[128];
 	gchar *format;
-	time_t t;
-	struct tm *tmp;
 
-	t = time(NULL);
-	tmp = localtime(&t);
-	if (tmp == NULL)
-		return "";
-
 	if (type == DATE_TYPE_HTML)
-/** needs testing */
+/* needs testing */
 #ifdef _GNU_SOURCE
 		format = "%Y-%m-%dT%H:%M:%S%z";
 #else
@@ -303,9 +295,7 @@
 	else
 		format = "%c";
 
-	strftime(str, sizeof str, format, tmp);
-
-	return str;
+	return p_utils->get_date_time(format, NULL);
 }
 
 
@@ -350,7 +340,7 @@
 {
 	GeanyEditor *editor = doc->editor;
 	gint i, style = -1, old_style = 0, column = 0;
-	gchar c, c_next, *tmp;
+	gchar c, c_next, *tmp, *date;
 	/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
 	gint styles[STYLE_MAX + 1][MAX_TYPES];
 	gboolean block_open = FALSE;
@@ -533,11 +523,12 @@
 		}
 	}
 
+	date = get_date(DATE_TYPE_DEFAULT);
 	/* write all */
 	latex = g_string_new(TEMPLATE_LATEX);
 	p_utils->string_replace_all(latex, "{export_content}", body->str);
 	p_utils->string_replace_all(latex, "{export_styles}", cmds->str);
-	p_utils->string_replace_all(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT));
+	p_utils->string_replace_all(latex, "{export_date}", date);
 	if (doc->file_name == NULL)
 		p_utils->string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED);
 	else
@@ -548,6 +539,7 @@
 	g_string_free(body, TRUE);
 	g_string_free(cmds, TRUE);
 	g_string_free(latex, TRUE);
+	g_free(date);
 }
 
 
@@ -555,7 +547,7 @@
 {
 	GeanyEditor *editor = doc->editor;
 	gint i, style = -1, old_style = 0, column = 0;
-	gchar c, c_next;
+	gchar c, c_next, *date;
 	/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
 	gint styles[STYLE_MAX + 1][MAX_TYPES];
 	gboolean span_open = FALSE;
@@ -686,9 +678,10 @@
 		}
 	}
 
+	date = get_date(DATE_TYPE_HTML);
 	/* write all */
 	html = g_string_new(TEMPLATE_HTML);
-	p_utils->string_replace_all(html, "{export_date}", get_date(DATE_TYPE_HTML));
+	p_utils->string_replace_all(html, "{export_date}", date);
 	p_utils->string_replace_all(html, "{export_content}", body->str);
 	p_utils->string_replace_all(html, "{export_styles}", css->str);
 	if (doc->file_name == NULL)
@@ -702,6 +695,7 @@
 	g_string_free(body, TRUE);
 	g_string_free(css, TRUE);
 	g_string_free(html, TRUE);
+	g_free(date);
 }
 
 

Modified: trunk/plugins/saveactions.c
===================================================================
--- trunk/plugins/saveactions.c	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/plugins/saveactions.c	2008-11-21 17:40:04 UTC (rev 3267)
@@ -196,14 +196,11 @@
 	gchar *locale_filename_dst;
 	gchar *basename_src;
 	gchar *dir_parts_src;
-	gchar stamp[512];
-	time_t t = time(NULL);
-	struct tm *now;
+	gchar *stamp;
 
 	if (! enable_backupcopy)
 		return;
 
-	now = localtime(&t);
 	locale_filename_src = p_utils->get_locale_from_utf8(doc->file_name);
 
 	if ((src = g_fopen(locale_filename_src, "r")) == NULL)
@@ -215,7 +212,7 @@
 		return;
 	}
 
-	strftime(stamp, sizeof(stamp), backupcopy_time_fmt, now);
+	stamp = p_utils->get_date_time(backupcopy_time_fmt, NULL);
 	basename_src = g_path_get_basename(locale_filename_src);
 	dir_parts_src = backupcopy_create_dir_parts(locale_filename_src);
 	locale_filename_dst = g_strconcat(
@@ -231,6 +228,7 @@
 			g_strerror(errno));
 		g_free(locale_filename_src);
 		g_free(locale_filename_dst);
+		g_free(stamp);
 		fclose(src);
 		return;
 	}
@@ -244,6 +242,7 @@
 	fclose(dst);
 	g_free(locale_filename_src);
 	g_free(locale_filename_dst);
+	g_free(stamp);
 }
 
 

Modified: trunk/src/callbacks.c
===================================================================
--- trunk/src/callbacks.c	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/src/callbacks.c	2008-11-21 17:40:04 UTC (rev 3267)
@@ -1418,10 +1418,7 @@
 {
 	GeanyDocument *doc = document_get_current();
 	gchar *format;
-	gchar time_str[300]; /* the entered format string can be maximal 256 chars long, so we have
-						  * 44 additional characters for strtime's conversion */
-	time_t t;
-	struct tm *tm;
+	gchar *time_str;
 
 	if (doc == NULL) return;
 
@@ -1454,15 +1451,14 @@
 		return;
 	}
 
-	/* get the current time */
-	t = time(NULL);
-	tm = localtime(&t);
-	if (strftime(time_str, sizeof time_str, format, tm) != 0)
+	time_str = utils_get_date_time(format, NULL);
+	if (time_str != NULL)
 	{
 		verify_click_pos(doc); /* make sure that the click_pos is valid */
 
 		sci_insert_text(doc->editor->sci, editor_info.click_pos, time_str);
 		sci_goto_pos(doc->editor->sci, editor_info.click_pos + strlen(time_str), FALSE);
+		g_free(time_str);
 	}
 	else
 	{

Modified: trunk/src/plugindata.h
===================================================================
--- trunk/src/plugindata.h	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/src/plugindata.h	2008-11-21 17:40:04 UTC (rev 3267)
@@ -45,7 +45,7 @@
 enum {
 	/** The Application Programming Interface (API) version, incremented
 	 * whenever any plugin data types are modified or appended to. */
-	GEANY_API_VERSION = 110,
+	GEANY_API_VERSION = 111,
 
 	/** The Application Binary Interface (ABI) version, incremented whenever
 	 * existing fields in the plugin data types have to be changed or reordered. */
@@ -330,7 +330,8 @@
 	gboolean	(*spawn_async) (const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
 				 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
 				 GError **error);
-	gint		(*utils_str_casecmp) (const gchar *s1, const gchar *s2);
+	gint		(*str_casecmp) (const gchar *s1, const gchar *s2);
+	gchar*		(*get_date_time) (const gchar *format, time_t *time_to_use);
 }
 UtilsFuncs;
 

Modified: trunk/src/plugins.c
===================================================================
--- trunk/src/plugins.c	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/src/plugins.c	2008-11-21 17:40:04 UTC (rev 3267)
@@ -196,7 +196,8 @@
 	&utils_get_setting_string,
 	&utils_spawn_sync,
 	&utils_spawn_async,
-	&utils_str_casecmp
+	&utils_str_casecmp,
+	&utils_get_date_time
 };
 
 static UIUtilsFuncs uiutils_funcs = {

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2008-11-21 17:33:35 UTC (rev 3266)
+++ trunk/src/utils.c	2008-11-21 17:40:04 UTC (rev 3267)
@@ -554,24 +554,47 @@
 }
 
 
+/**
+ *  This is a convenience function to retrieve a formatted date/time string from strftime().
+ *  This function should be preferred to directly calling strftime() since this function
+ *  works on UTF-8 encoded strings.
+ *
+ *  @param format The format string to pass to strftime(3). See the strftime(3)
+ *                documentation for details, in UTF-8 encoding.
+ *  @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
+ *
+ *  @return A newly-allocated string, should be freed when no longer needed.
+ **/
 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
 {
-	time_t tp;
 	const struct tm *tm;
-	gchar *date;
+	static gchar date[1024];
+	gchar *locale_format;
+	gsize len;
 
-	if (format == NULL)
+	g_return_val_if_fail(format != NULL, NULL);
+
+	locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
+	if (locale_format == NULL)
 		return NULL;
 
 	if (time_to_use != NULL)
-		tp = *time_to_use;
+		tm = localtime(time_to_use);
 	else
-		tp = time(NULL);
+	{
+		time_t tp = time(NULL);
+		tm = localtime(&tp);
+	}
 
-	tm = localtime(&tp);
-	date = g_malloc0(256);
-	strftime(date, 256, format, tm);
-	return date;
+	len = strftime(date, 1024, locale_format, tm);
+	g_free(locale_format);
+	if (len == 0)
+		return NULL;
+
+	if (! g_utf8_validate(date, len, NULL))
+		return g_locale_to_utf8(date, len, NULL, NULL, NULL);
+	else
+		return g_strdup(date);
 }
 
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list