[evolution/wip/webkit-composer: 341/372] Move replace_string from Backup-Restore module to e-misc-utils and make it public as e_str_replace_s
- From: Tomas Popela <tpopela src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution/wip/webkit-composer: 341/372] Move replace_string from Backup-Restore module to e-misc-utils and make it public as e_str_replace_s
- Date: Thu, 6 Feb 2014 11:52:10 +0000 (UTC)
commit a784b0d2e092682a780d4532bf53b86e832daa6c
Author: Tomas Popela <tpopela redhat com>
Date: Thu Jan 23 15:41:43 2014 +0100
Move replace_string from Backup-Restore module to e-misc-utils and make it public as e_str_replace_string
e-util/e-misc-utils.c | 44 ++++++++++++++++++++++++
e-util/e-misc-utils.h | 3 ++
modules/backup-restore/evolution-backup-tool.c | 44 +++--------------------
3 files changed, 53 insertions(+), 38 deletions(-)
---
diff --git a/e-util/e-misc-utils.c b/e-util/e-misc-utils.c
index 7aec593..1e2d5c5 100644
--- a/e-util/e-misc-utils.c
+++ b/e-util/e-misc-utils.c
@@ -1085,6 +1085,50 @@ e_str_without_underscores (const gchar *string)
return new_string;
}
+/**
+ * e_str_replace_string
+ * @text: the string to replace
+ * @before: the string to be replaced
+ * @after: the string to replaced with
+ *
+ * Replaces every occurrence of the string @before with the string @after in
+ * the string @text and returns a #GString with result that should be freed
+ * with g_string_free().
+ *
+ * Returns: a newly-allocated #GString
+ */
+GString *
+e_str_replace_string (const gchar *text,
+ const gchar *before,
+ const gchar *after)
+{
+ const gchar *p, *next;
+ GString *str;
+ gint find_len;
+
+ g_return_val_if_fail (text != NULL, NULL);
+ g_return_val_if_fail (before != NULL, NULL);
+ g_return_val_if_fail (*before, NULL);
+
+ find_len = strlen (before);
+ str = g_string_new ("");
+
+ p = text;
+ while (next = strstr (p, before), next) {
+ if (p < next)
+ g_string_append_len (str, p, next - p);
+
+ if (after && *after)
+ g_string_append (str, after);
+
+ p = next + find_len;
+ }
+
+ g_string_append (str, p);
+
+ return str;
+}
+
gint
e_str_compare (gconstpointer x,
gconstpointer y)
diff --git a/e-util/e-misc-utils.h b/e-util/e-misc-utils.h
index 54c8d15..3129a28 100644
--- a/e-util/e-misc-utils.h
+++ b/e-util/e-misc-utils.h
@@ -99,6 +99,9 @@ gchar * e_ascii_dtostr (gchar *buffer,
gdouble d);
gchar * e_str_without_underscores (const gchar *string);
+GString * e_str_replace_string (const gchar *text,
+ const gchar *find,
+ const gchar *replace);
gint e_str_compare (gconstpointer x,
gconstpointer y);
gint e_str_case_compare (gconstpointer x,
diff --git a/modules/backup-restore/evolution-backup-tool.c b/modules/backup-restore/evolution-backup-tool.c
index e94a3ff..8a239bf 100644
--- a/modules/backup-restore/evolution-backup-tool.c
+++ b/modules/backup-restore/evolution-backup-tool.c
@@ -101,38 +101,6 @@ static GOptionEntry options[] = {
static gboolean check (const gchar *filename, gboolean *is_new_format);
-static GString *
-replace_string (const gchar *text,
- const gchar *find,
- const gchar *replace)
-{
- const gchar *p, *next;
- GString *str;
- gint find_len;
-
- g_return_val_if_fail (text != NULL, NULL);
- g_return_val_if_fail (find != NULL, NULL);
- g_return_val_if_fail (*find, NULL);
-
- find_len = strlen (find);
- str = g_string_new ("");
-
- p = text;
- while (next = strstr (p, find), next) {
- if (p < next)
- g_string_append_len (str, p, next - p);
-
- if (replace && *replace)
- g_string_append (str, replace);
-
- p = next + find_len;
- }
-
- g_string_append (str, p);
-
- return str;
-}
-
static const gchar *
strip_home_dir (const gchar *dir)
{
@@ -166,11 +134,11 @@ replace_variables (const gchar *str,
strip_datadir = strip_home_dir (e_get_user_data_dir ());
strip_configdir = strip_home_dir (e_get_user_config_dir ());
- #define repl(_find, _replace) \
- use = replace_string (res ? res->str : str, _find, _replace); \
- g_return_val_if_fail (use != NULL, NULL); \
- if (res) \
- g_string_free (res, TRUE); \
+ #define repl(_find, _replace) \
+ use = e_str_replace_string (res ? res->str : str, _find, _replace); \
+ g_return_val_if_fail (use != NULL, NULL); \
+ if (res) \
+ g_string_free (res, TRUE); \
res = use;
repl ("$HOME", g_get_home_dir ());
@@ -223,7 +191,7 @@ replace_in_file (const gchar *filename,
}
if (g_file_get_contents (filename, &content, NULL, &error)) {
- GString *str = replace_string (content, find, replace);
+ GString *str = e_str_replace_string (content, find, replace);
if (str) {
if (!g_file_set_contents (filename, str->str, -1, &error) && error) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]