From 7d0d2d60bc46c7a60d60877432ccb15fe97f7c8a Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sat, 2 May 2009 21:38:43 +0200 Subject: [PATCH] Add sokoke_replace_variables --- midori/sokoke.c | 25 +++++++++++++++++++++++++ midori/sokoke.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/midori/sokoke.c b/midori/sokoke.c index 69f2d675..1af7289d 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1018,3 +1018,28 @@ sokoke_get_res_server (void) return res_server; } + +gchar* +sokoke_replace_variables (const gchar* template, + const gchar* variable_first, ...) +{ + gchar* result = g_strdup (template); + const gchar* variable; + + va_list args; + va_start (args, variable_first); + + for (variable = variable_first; variable; variable = va_arg (args, const gchar*)) + { + const gchar* value = va_arg (args, const gchar*); + GRegex* regex = g_regex_new (variable, 0, 0, NULL); + gchar* replaced = result; + result = g_regex_replace (regex, replaced, -1, 0, value, 0, NULL); + g_free (replaced); + g_regex_unref (regex); + } + + va_end (args); + + return result; +} diff --git a/midori/sokoke.h b/midori/sokoke.h index e960e087..56a76615 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -157,4 +157,8 @@ sokoke_remove_path (const gchar* path, SoupServer* sokoke_get_res_server (void); +gchar* +sokoke_replace_variables (const gchar* template, + const gchar* variable_first, ...); + #endif /* !__SOKOKE_H__ */