SF.net SVN: geany:[3268] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Fri Nov 21 18:19:36 UTC 2008


Revision: 3268
          http://geany.svn.sourceforge.net/geany/?rev=3268&view=rev
Author:   eht16
Date:     2008-11-21 18:19:36 +0000 (Fri, 21 Nov 2008)

Log Message:
-----------
Make templates_replace_all() working on GStrings to fix various string replacement problems when using custom file templates.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/editor.c
    trunk/src/templates.c
    trunk/src/templates.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-21 17:40:04 UTC (rev 3267)
+++ trunk/ChangeLog	2008-11-21 18:19:36 UTC (rev 3268)
@@ -8,6 +8,9 @@
    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.
+ * src/editor.c, src/templates.c, src/templates.h:
+   Make templates_replace_all() working on GStrings to fix various
+   string replacement problems when using custom file templates.
 
 
 2008-11-21  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2008-11-21 17:40:04 UTC (rev 3267)
+++ trunk/src/editor.c	2008-11-21 18:19:36 UTC (rev 3268)
@@ -1658,7 +1658,7 @@
  * modified when replacing a completion but the foreach function still passes the old pointer
  * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
  * ac_complete_constructs. Any hints to improve this are welcome. */
-static gchar *snippets_global_pattern = NULL;
+static GString *snippets_global_pattern = NULL;
 
 void snippets_replace_specials(gpointer key, gpointer value, gpointer user_data)
 {
@@ -1669,24 +1669,22 @@
 
 	needle = g_strconcat("%", (gchar*) key, "%", NULL);
 
-	snippets_global_pattern = utils_str_replace(snippets_global_pattern, needle, (gchar*) value);
+	utils_string_replace_all(snippets_global_pattern, needle, (gchar*) value);
 	g_free(needle);
 }
 
 
-static gchar *snippets_replace_wildcards(GeanyEditor *editor, gchar *text)
+static void snippets_replace_wildcards(GeanyEditor *editor, GString *text)
 {
 	gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
 	gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
 	gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
 	gchar *basename = g_path_get_basename(DOC_FILENAME(editor->document));
 
-	text = templates_replace_all(text, year, date, datetime);
-	text = utils_str_replace(text, "{filename}", basename);
+	templates_replace_all(text, year, date, datetime);
+	utils_string_replace_all(text, "{filename}", basename);
 
 	utils_free_pointers(4, year, date, datetime, basename, NULL);
-
-	return text;
 }
 
 
@@ -1785,8 +1783,9 @@
 static gboolean snippets_complete_constructs(GeanyEditor *editor, gint pos, const gchar *word)
 {
 	gchar *str;
-	gchar *pattern;
-	gint step, str_len, cur_index;
+	GString *pattern;
+	gint step, str_len;
+	gsize cur_index;
 	gint ft_id = FILETYPE_ID(editor->document->file_type);
 	GHashTable *specials;
 	ScintillaObject *sci = editor->sci;
@@ -1794,10 +1793,11 @@
 	str = g_strdup(word);
 	g_strstrip(str);
 
-	pattern = snippets_find_completion_by_name(filetypes[ft_id]->name, str);
-	if (pattern == NULL || pattern[0] == '\0')
+	pattern = g_string_new(snippets_find_completion_by_name(filetypes[ft_id]->name, str));
+	if (pattern == NULL || pattern->len == 0)
 	{
-		utils_free_pointers(2, str, pattern, NULL); /* free pattern in case it is "" */
+		g_free(str);
+		g_string_free(pattern, TRUE);
 		return FALSE;
 	}
 
@@ -1817,27 +1817,28 @@
 		/* ugly hack using global_pattern */
 		snippets_global_pattern = pattern;
 		g_hash_table_foreach(specials, snippets_replace_specials, NULL);
-		pattern = snippets_global_pattern;
 	}
 
 	/* replace any %template% wildcards */
-	pattern = snippets_replace_wildcards(editor, pattern);
+	snippets_replace_wildcards(editor, pattern);
 
 	/* find the %cursor% pos (has to be done after all other operations) */
-	step = utils_strpos(pattern, "%cursor%");
+	step = utils_strpos(pattern->str, "%cursor%");
 	if (step != -1)
-		pattern = utils_str_replace(pattern, "%cursor%", "");
+		utils_string_replace_all(pattern, "%cursor%", "");
 
 	/* finally insert the text and set the cursor */
 	if (step != -1)
 		cur_index = step;
 	else
-		cur_index = strlen(pattern);
+		cur_index = pattern->len;
 
-	editor_insert_text_block(editor, pattern, pos, cur_index, -1);
+	editor_insert_text_block(editor, pattern->str, pos, cur_index, -1);
 	sci_scroll_caret(sci);
 
-	utils_free_pointers(2, pattern, str, NULL);
+	g_free(str);
+	g_string_free(pattern, TRUE);
+
  	return TRUE;
 }
 

Modified: trunk/src/templates.c
===================================================================
--- trunk/src/templates.c	2008-11-21 17:40:04 UTC (rev 3267)
+++ trunk/src/templates.c	2008-11-21 18:19:36 UTC (rev 3268)
@@ -255,6 +255,23 @@
 
 
 
+/* FIXME the callers should use GStrings instead of char arrays */
+static gchar *replace_all(gchar *text, const gchar *year, const gchar *date, const gchar *datetime)
+{
+	GString *str;
+
+	if (text == NULL)
+		return NULL;
+
+	str = g_string_new(text);
+
+	g_free(text);
+	templates_replace_all(str, year, date, datetime);
+
+	return g_string_free(str, FALSE);
+}
+
+
 static void init_general_templates(const gchar *year, const gchar *date, const gchar *datetime)
 {
 	gchar *template_filename_fileheader = TEMPLATES_GET_FILENAME("fileheader");
@@ -272,19 +289,19 @@
 
 	/* read the contents */
 	TEMPLATES_READ_FILE(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]);
-	templates[GEANY_TEMPLATE_FILEHEADER] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER], year, date, datetime);
+	templates[GEANY_TEMPLATE_FILEHEADER] = replace_all(templates[GEANY_TEMPLATE_FILEHEADER], year, date, datetime);
 
 	TEMPLATES_READ_FILE(template_filename_gpl, &templates[GEANY_TEMPLATE_GPL]);
-	templates[GEANY_TEMPLATE_GPL] = templates_replace_all(templates[GEANY_TEMPLATE_GPL], year, date, datetime);
+	templates[GEANY_TEMPLATE_GPL] = replace_all(templates[GEANY_TEMPLATE_GPL], year, date, datetime);
 
 	TEMPLATES_READ_FILE(template_filename_bsd, &templates[GEANY_TEMPLATE_BSD]);
-	templates[GEANY_TEMPLATE_BSD] = templates_replace_all(templates[GEANY_TEMPLATE_BSD], year, date, datetime);
+	templates[GEANY_TEMPLATE_BSD] = replace_all(templates[GEANY_TEMPLATE_BSD], year, date, datetime);
 
 	TEMPLATES_READ_FILE(template_filename_function, &templates[GEANY_TEMPLATE_FUNCTION]);
-	templates[GEANY_TEMPLATE_FUNCTION] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION], year, date, datetime);
+	templates[GEANY_TEMPLATE_FUNCTION] = replace_all(templates[GEANY_TEMPLATE_FUNCTION], year, date, datetime);
 
 	TEMPLATES_READ_FILE(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]);
-	templates[GEANY_TEMPLATE_CHANGELOG] = templates_replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date, datetime);
+	templates[GEANY_TEMPLATE_CHANGELOG] = replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date, datetime);
 
 	/* free the whole stuff */
 	g_free(template_filename_fileheader);
@@ -321,7 +338,7 @@
 			default: break;
 		}
 		TEMPLATES_READ_FILE(fname, &ft_templates[ft_id]);
-		ft_templates[ft_id] = templates_replace_all(ft_templates[ft_id], year, date, datetime);
+		ft_templates[ft_id] = replace_all(ft_templates[ft_id], year, date, datetime);
 
 		g_free(fname);
 		g_free(shortname);
@@ -372,27 +389,28 @@
 static gchar *get_template_from_file(const gchar *locale_fname, const gchar *doc_filename,
 									 GeanyFiletype *ft)
 {
-	GString template = { NULL, 0, 0 };
+	gchar *content;
+	GString *template = NULL;
 
-	g_file_get_contents(locale_fname, &template.str, &template.len, NULL);
+	g_file_get_contents(locale_fname, &content, NULL, NULL);
 
-	if (template.len > 0)
+	if (content != NULL)
 	{
 		gchar *file_header;
 		gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
 		gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
 		gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
 
-		template.allocated_len = template.len + 1;
+		template = g_string_new(content);
 
 		file_header = templates_get_template_fileheader(FILETYPE_ID(ft), doc_filename);
-		template.str = templates_replace_all(template.str, year, date, datetime);
-		utils_string_replace_all(&template, "{filename}", doc_filename);
-		utils_string_replace_all(&template, "{fileheader}", file_header);
+		templates_replace_all(template, year, date, datetime);
+		utils_string_replace_all(template, "{filename}", doc_filename);
+		utils_string_replace_all(template, "{fileheader}", file_header);
 
-		utils_free_pointers(4, year, date, datetime, file_header, NULL);
+		utils_free_pointers(5, year, date, datetime, file_header, content, NULL);
 	}
-	return template.str;
+	return g_string_free(template, FALSE);
 }
 
 
@@ -794,20 +812,18 @@
 }
 
 
-gchar *templates_replace_all(gchar *text, const gchar *year, const gchar *date,
-							 const gchar *datetime)
+void templates_replace_all(GString *text, const gchar *year, const gchar *date,
+						   const gchar *datetime)
 {
-	text = utils_str_replace(text, "{year}", year);
-	text = utils_str_replace(text, "{date}", date);
-	text = utils_str_replace(text, "{datetime}", datetime);
-	text = utils_str_replace(text, "{version}", template_prefs.version);
-	text = utils_str_replace(text, "{initial}", template_prefs.initials);
-	text = utils_str_replace(text, "{developer}", template_prefs.developer);
-	text = utils_str_replace(text, "{mail}", template_prefs.mail);
-	text = utils_str_replace(text, "{company}", template_prefs.company);
-	text = utils_str_replace(text, "{untitled}", GEANY_STRING_UNTITLED);
-	text = utils_str_replace(text, "{geanyversion}", "Geany " VERSION);
-
-	return text;
+	utils_string_replace_all(text, "{year}", year);
+	utils_string_replace_all(text, "{date}", date);
+	utils_string_replace_all(text, "{datetime}", datetime);
+	utils_string_replace_all(text, "{version}", template_prefs.version);
+	utils_string_replace_all(text, "{initial}", template_prefs.initials);
+	utils_string_replace_all(text, "{developer}", template_prefs.developer);
+	utils_string_replace_all(text, "{mail}", template_prefs.mail);
+	utils_string_replace_all(text, "{company}", template_prefs.company);
+	utils_string_replace_all(text, "{untitled}", GEANY_STRING_UNTITLED);
+	utils_string_replace_all(text, "{geanyversion}", "Geany " VERSION);
 }
 

Modified: trunk/src/templates.h
===================================================================
--- trunk/src/templates.h	2008-11-21 17:40:04 UTC (rev 3267)
+++ trunk/src/templates.h	2008-11-21 18:19:36 UTC (rev 3268)
@@ -67,8 +67,8 @@
 
 gchar *templates_get_template_licence(gint filetype_idx, gint licence_type);
 
-gchar *templates_replace_all(gchar *source, const gchar *year, const gchar *date,
-							 const gchar *datetime);
+void templates_replace_all(GString *source, const gchar *year, const gchar *date,
+						   const gchar *datetime);
 
 void templates_free_templates(void);
 


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