Revision: 4522 http://geany.svn.sourceforge.net/geany/?rev=4522&view=rev Author: eht16 Date: 2010-01-12 21:10:46 +0000 (Tue, 12 Jan 2010)
Log Message: ----------- Refactor templates_replace_all() into templates_replace_valist() to save some code duplication.
Modified Paths: -------------- trunk/ChangeLog trunk/src/editor.c trunk/src/templates.c trunk/src/templates.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2010-01-12 18:44:38 UTC (rev 4521) +++ trunk/ChangeLog 2010-01-12 21:10:46 UTC (rev 4522) @@ -1,3 +1,10 @@ +2010-01-12 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de> + + * src/editor.c, src/templates.c, src/templates.h: + Refactor templates_replace_all() into templates_replace_valist() + to save some code duplication. + + 2010-01-11 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
* plugins/geanyfunctions.h, src/editor.c, src/editor.h,
Modified: trunk/src/editor.c =================================================================== --- trunk/src/editor.c 2010-01-12 18:44:38 UTC (rev 4521) +++ trunk/src/editor.c 2010-01-12 21:10:46 UTC (rev 4522) @@ -2135,15 +2135,12 @@
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));
- templates_replace_all(text, year, date, datetime); - utils_string_replace_all(text, "{filename}", basename); + templates_replace_default_dates(text); + templates_replace_valist(text, "{filename}", basename, NULL);
- utils_free_pointers(4, year, date, datetime, basename, NULL); + g_free(basename); }
Modified: trunk/src/templates.c =================================================================== --- trunk/src/templates.c 2010-01-12 18:44:38 UTC (rev 4521) +++ trunk/src/templates.c 2010-01-12 21:10:46 UTC (rev 4522) @@ -167,7 +167,11 @@ str = g_string_new(text);
g_free(text); - templates_replace_all(str, year, date, datetime); + templates_replace_valist(str, + "{year}", year, + "{date}", date, + "{datetime}", datetime, + NULL);
return g_string_free(str, FALSE); } @@ -288,18 +292,17 @@ 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 = g_string_new(content);
file_header = templates_get_template_fileheader(FILETYPE_ID(ft), doc_filename); - templates_replace_all(template, year, date, datetime); - utils_string_replace_all(template, "{filename}", doc_filename); - utils_string_replace_all(template, "{fileheader}", file_header); + templates_replace_valist(template, + "{filename}", doc_filename, + "{fileheader}", file_header, + NULL); + templates_replace_default_dates(template);
- utils_free_pointers(5, year, date, datetime, file_header, content, NULL); + utils_free_pointers(2, file_header, content, NULL); return g_string_free(template, FALSE); } return NULL; @@ -660,12 +663,8 @@ }
-void templates_replace_all(GString *text, const gchar *year, const gchar *date, - const gchar *datetime) +static void replace_static_values(GString *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); @@ -675,3 +674,52 @@ utils_string_replace_all(text, "{geanyversion}", "Geany " VERSION); }
+ +/* Replaces all static template wildcards (version, mail, company, name, ...) + * plus those wildcard, value pairs which are passed, e.g. + * + * templates_replace_valist(text, "{some_wildcard}", "some value", + * "{another_wildcard}", "another value", NULL); + * + * The argument list must be terminated with NULL. */ +void templates_replace_valist(GString *text, const gchar *first_wildcard, ...) +{ + va_list args; + const gchar *key, *value; + + g_return_if_fail(text != NULL); + + va_start(args, first_wildcard); + + key = first_wildcard; + value = va_arg(args, gchar*); + + while (key != NULL) + { + utils_string_replace_all(text, key, value); + + key = va_arg(args, gchar*); + if (text == NULL) + break; + value = va_arg(args, gchar*); + } + va_end(args); + + replace_static_values(text); +} + + +void templates_replace_default_dates(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); + + templates_replace_valist(text, + "{year}", year, + "{date}", date, + "{datetime}", datetime, + NULL); + + utils_free_pointers(3, year, date, datetime, NULL); +}
Modified: trunk/src/templates.h =================================================================== --- trunk/src/templates.h 2010-01-12 18:44:38 UTC (rev 4521) +++ trunk/src/templates.h 2010-01-12 21:10:46 UTC (rev 4522) @@ -74,9 +74,10 @@
gchar *templates_get_template_licence(gint filetype_idx, gint licence_type);
-void templates_replace_all(GString *source, const gchar *year, const gchar *date, - const gchar *datetime); +void templates_replace_default_dates(GString *text);
+void templates_replace_valist(GString *text, const gchar *first_wildcard, ...) G_GNUC_NULL_TERMINATED; + void templates_free_templates(void);
#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.