[aravis] str: implement an illegal characters stripping function.



commit a151e42ac7e151c665e291084d71d2b148ab9d93
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Sun Sep 8 22:41:24 2013 +0200

    str: implement an illegal characters stripping function.

 docs/reference/aravis/aravis-sections.txt |    3 +
 src/arvcamera.h                           |    5 ++
 src/arvstr.c                              |   59 ++++++++++++++++++++++++++++-
 src/arvstr.h                              |    2 +
 tests/misc.c                              |   43 +++++++++++++++++++++
 5 files changed, 111 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/aravis/aravis-sections.txt b/docs/reference/aravis/aravis-sections.txt
index fc2d343..eec42ab 100644
--- a/docs/reference/aravis/aravis-sections.txt
+++ b/docs/reference/aravis/aravis-sections.txt
@@ -66,6 +66,8 @@ ARV_CAMERA_GET_CLASS
 <SUBSECTION Private>
 ArvCameraPrivate
 ArvCameraClass
+ARV_CAMERA_NAME_ILLEGAL_CHARACTERS
+ARV_CAMERA_NAME_REPLACEMENT_CHARACTER
 </SECTION>
 
 <SECTION>
@@ -1010,6 +1012,7 @@ arv_statistic_reset
 arv_statistic_fill
 arv_statistic_set_name
 arv_statistic_to_string
+arv_str_strip
 arv_str_is_uri
 arv_str_to_uri
 arv_str_parse_double
diff --git a/src/arvcamera.h b/src/arvcamera.h
index f918217..7672d65 100644
--- a/src/arvcamera.h
+++ b/src/arvcamera.h
@@ -28,6 +28,11 @@
 
 G_BEGIN_DECLS
 
+#define ARV_CAMERA_NAME_ILLEGAL_CHARACTERS     
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
+                                               
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
+                                               " _-"
+#define ARV_CAMERA_NAME_REPLACEMENT_CHARACTER  '_'
+
 #define ARV_TYPE_CAMERA             (arv_camera_get_type ())
 #define ARV_CAMERA(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), ARV_TYPE_CAMERA, ArvCamera))
 #define ARV_CAMERA_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), ARV_TYPE_CAMERA, ArvCameraClass))
diff --git a/src/arvstr.c b/src/arvstr.c
index 767bfde..c363954 100644
--- a/src/arvstr.c
+++ b/src/arvstr.c
@@ -27,10 +27,67 @@
 #include <string.h>
 #include <math.h>
 
+/**
+ * arv_str_strip:
+ * @str: (allow-none): a string
+ * @illegal_chars: illegal characters
+ * @replacement_char: replacement character
+ *
+ * Remove any @illegal_chars from @str, and replace them by @replacement_char if they are not at the end or 
at the beginning of @str.
+ * Several consecutive @illegal_chars are replaced by only one @replacement_char. @illegal_chars at the 
beginnig or at the end of @str
+ * are simply removed.
+ *
+ * Returns: @str
+ *
+ * Since: 0.3.0
+ */
+
+char *
+arv_str_strip (char *str, const char *illegal_chars, char replacement_char)
+{
+       char *last_char = NULL;
+       char *ptr = str;
+       char *out = str;
+       unsigned int n_illegal_chars;
+       unsigned int i;
+       
+       if (str == NULL || illegal_chars == NULL)
+               return str;
+
+       n_illegal_chars = strlen (illegal_chars);
+       if (n_illegal_chars == 0)
+               return str;
+
+       while (*ptr != '\0') {
+               gboolean found = FALSE;
+               for (i = 0; i < n_illegal_chars && !found; i++)
+                       found = illegal_chars[i] == *ptr;
+               
+               if (found) {
+                       if (last_char == out && replacement_char != '\0') {
+                               *out = replacement_char;
+                               out++;
+                       }
+               } else {
+                       *out = *ptr;
+                       out++;
+                       last_char = out;
+               }
+               ptr++;
+       }
+
+       if (last_char != NULL)
+               *last_char = '\0';
+       else
+               *str = '\0';
+
+       return str;
+}
+
 /* http://www.ietf.org/rfc/rfc2396.txt - Implementation comes from librsvg (rsvg-base.c). */
 
 gboolean
-arv_str_is_uri  (const char *str)
+arv_str_is_uri (const char *str)
 {
        char const *p;
 
diff --git a/src/arvstr.h b/src/arvstr.h
index 2a0006e..e67404a 100644
--- a/src/arvstr.h
+++ b/src/arvstr.h
@@ -28,6 +28,8 @@
 
 G_BEGIN_DECLS
 
+char *                 arv_str_strip           (char *str, const char *illegal_chars, char replacement_char);
+
 gboolean       arv_str_is_uri          (const char *str);
 char *         arv_str_to_uri          (const char *str);
 
diff --git a/tests/misc.c b/tests/misc.c
index bff9dc4..56c14a2 100644
--- a/tests/misc.c
+++ b/tests/misc.c
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <arv.h>
+#include <arvstr.h>
 #include <string.h>
 
 static void
@@ -34,6 +35,47 @@ unaligned_from_le_ptr_test (void)
        g_assert_cmpuint (v_uint16, ==, 0x5544);
 }
 
+struct {
+       char *before;
+       char *after;
+} strip_strings[] = {
+       {"\n\tHello\r\nworld!\n\t",                                             "Hello_world!"},
+       {"\n\tHello",                                                           "Hello"},
+       {"Hello\r\t",                                                           "Hello"},
+       {"Hello\rworld!",                                                       "Hello_world!"},
+       {"Hello\r \rworld!",                                                    "Hello_world!"},
+       {"",                                                                    ""},
+       {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",        ""},
+       {"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",        ""},
+       {"\r",                                                                  ""},
+       {"\n\n",                                                                ""},
+       {"Hétéroclite",                                                         "Hétéroclite"}
+};
+
+static void
+arv_str_strip_test (void)
+{
+       unsigned i;
+       char *string;
+
+       for (i = 0; i < G_N_ELEMENTS (strip_strings); i++) {
+               string = g_strdup (strip_strings[i].before);
+               arv_str_strip (string, ARV_CAMERA_NAME_ILLEGAL_CHARACTERS, '_');
+
+               g_assert_cmpstr (string, ==, strip_strings[i].after);
+
+               g_free (string);
+       }
+
+       string = g_strdup ("Hello\r\n world");
+       arv_str_strip (string, ARV_CAMERA_NAME_ILLEGAL_CHARACTERS, '\0');
+       g_assert_cmpstr (string, ==, "Helloworld");
+       g_free (string);
+
+       g_assert (arv_str_strip (NULL, ARV_CAMERA_NAME_ILLEGAL_CHARACTERS, '_') == NULL);
+       g_assert (arv_str_strip (NULL, NULL, '_') == NULL);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -45,6 +87,7 @@ main (int argc, char *argv[])
        arv_g_type_init ();
 
        g_test_add_func ("/buffer/unaligned-from-le", unaligned_from_le_ptr_test);
+       g_test_add_func ("/str/arv-str-strip", arv_str_strip_test);
 
        result = g_test_run();
 


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