[gnome-calendar/calendar-editor] utils: add function to parse uris



commit f4af9ee8413a6eab56a71aca3c7b6785da2adbd7
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Mar 25 23:33:41 2015 -0300

    utils: add function to parse uris
    
    By using a custom regular expression,
    parse a uri or abort if it's invalid.

 src/gcal-utils.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcal-utils.h |    5 ++++
 2 files changed, 66 insertions(+), 0 deletions(-)
---
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index 8a63542..e420135 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -621,4 +621,65 @@ fix_popover_menu_icons (GtkPopover *popover)
   g_list_free (menu_section_box_children);
 }
 
+/**
+ * uri_get_fields:
+ *
+ * Split the given URI into the
+ * fields.
+ *
+ * Returns: #TRUE if @uri could be parsed, #FALSE otherwise
+ */
+gboolean
+uri_get_fields (const gchar  *uri,
+                gchar       **schema,
+                gchar       **host,
+                gchar       **path)
+{
+  GRegex *regex;
+  GMatchInfo *match;
+  gboolean valid;
+
+  g_return_val_if_fail (uri != NULL, FALSE);
+
+  match = NULL;
+  valid = FALSE;
+
+  regex = g_regex_new 
("([a-zA-Z0-9\\+\\.\\-]*):\\/\\/{0,1}([-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b)([-a-zA-Z0-9@:%_\\+.//=]*)",
+                       G_REGEX_CASELESS, 0, NULL);
+
+  /*
+   * Retrieved matching URI. The whole url is
+   * checked and the regex groups are:
+   * 1. schema
+   * 2. host
+   * 3. server path
+   */
+  if (g_regex_match (regex, uri, 0, &match))
+    {
+      valid = TRUE;
 
+      if (schema)
+        *schema = g_match_info_fetch (match, 1);
+
+      if (host)
+        *host = g_match_info_fetch (match, 2);
+
+      if (path)
+        *path = g_match_info_fetch (match, 3);
+    }
+  else
+    {
+      if (schema)
+        *schema = NULL;
+
+      if (host)
+        *host = NULL;
+
+      if (path)
+        *path = NULL;
+    }
+
+  g_match_info_free (match);
+  g_regex_unref (regex);
+  return valid;
+}
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index 08f58f3..3ba631b 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -100,4 +100,9 @@ gsize           e_utf8_strftime_fix_am_pm                       (gchar
 
 void            fix_popover_menu_icons                          (GtkPopover            *popover);
 
+gboolean        uri_get_fields                                  (const gchar           *uri,
+                                                                 gchar                **schema,
+                                                                 gchar                **host,
+                                                                 gchar                **path);
+
 #endif // __GCAL_UTILS_H__


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