[gnome-todo] utils: Introduce utilility file



commit 62d7411a76aedd71cfc41f51d91acb782d094aff
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Feb 2 12:35:53 2018 -0200

    utils: Introduce utilility file
    
    Let's open the champagne with gtd_str_replace()

 src/gnome-todo.h |  3 +-
 src/gtd-utils.c  | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gtd-utils.h  | 31 +++++++++++++++++++
 src/meson.build  |  2 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
---
diff --git a/src/gnome-todo.h b/src/gnome-todo.h
index 6deb707..3709737 100644
--- a/src/gnome-todo.h
+++ b/src/gnome-todo.h
@@ -29,7 +29,8 @@
 #include "gtd-task.h"
 #include "gtd-task-list.h"
 #include "gtd-task-list-view.h"
-#include "gtd-window.h"
 #include "gtd-types.h"
+#include "gtd-utils.h"
+#include "gtd-window.h"
 
 #endif /* GNOME_TODO_H */
diff --git a/src/gtd-utils.c b/src/gtd-utils.c
new file mode 100644
index 0000000..434306a
--- /dev/null
+++ b/src/gtd-utils.c
@@ -0,0 +1,90 @@
+/* gtd-utils.c
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "gtd-utils.h"
+
+gchar*
+gtd_str_replace (const gchar *source,
+                 const gchar *search,
+                 const gchar *replacement)
+{
+  gchar *new_string, *new_aux;
+  const gchar *source_aux;
+  const gchar *source_aux2;
+  gint64 n_ocurrences;
+  gint64 replacement_len;
+  gint64 search_len;
+  gint64 final_size;
+  gint64 diff;
+
+  g_assert_nonnull (source);
+  g_assert_nonnull (search);
+  g_assert_nonnull (replacement);
+
+  /* Count the number of ocurrences of "search" inside "source" */
+  search_len = strlen (search);
+  replacement_len = strlen (replacement);
+  n_ocurrences = 0;
+
+  for (source_aux = g_strstr_len (source, -1, search);
+       source_aux != NULL;
+       source_aux = g_strstr_len (source_aux + search_len, -1, search))
+    {
+       n_ocurrences++;
+    }
+
+  /* Calculate size of the new string */
+  diff = replacement_len - search_len;
+  final_size = strlen (source) + diff * n_ocurrences + 1;
+
+  /* Create the new string */
+  new_string = g_malloc0 (final_size);
+
+  /*
+   * And copy the contents of the source string into the new string,
+   * substituting the search by replacement
+   */
+  source_aux2 = source;
+  new_aux = new_string;
+
+  for (source_aux = g_strstr_len (source, -1, search);
+       source_aux != NULL;
+       source_aux = g_strstr_len (source_aux + search_len, -1, search))
+    {
+      diff = source_aux - source_aux2;
+
+      /* Copy the non-search part between the instances of "search" in "source" */
+      strncpy (new_aux, source_aux2, diff);
+
+      new_aux += diff;
+
+      /* Now copy the "replacement" where "search would be in source */
+      strncpy (new_aux, replacement, replacement_len);
+
+      source_aux2 = source_aux + search_len;
+      new_aux += replacement_len;
+    }
+
+  /* Copy the last chunk of string if any */
+  diff = strlen (source) - (source_aux2 - source);
+  strncpy (new_aux, source_aux2, diff);
+
+  return new_string;
+}
diff --git a/src/gtd-utils.h b/src/gtd-utils.h
new file mode 100644
index 0000000..56c1475
--- /dev/null
+++ b/src/gtd-utils.h
@@ -0,0 +1,31 @@
+/* gtd-utils.h
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gchar*               gtd_str_replace                             (const gchar        *source,
+                                                                  const gchar        *search,
+                                                                  const gchar        *replacement);
+
+G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index 9fd3641..943dcb0 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -27,6 +27,7 @@ headers = enum_headers + files(
   'gtd-task-list-view.h',
   'gtd-timer.h',
   'gtd-types.h',
+  'gtd-utils.h',
   'gtd-window.h',
   'gnome-todo.h'
 )
@@ -73,6 +74,7 @@ sources = files(
   'gtd-task-list-view.c',
   'gtd-task-row.c',
   'gtd-timer.c',
+  'gtd-utils.c',
   'gtd-window.c',
   'main.c'
 )


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]