summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2008-10-23 12:03:35 +0100
committerRichard Hughes <richard@hughsie.com>2008-10-23 12:03:35 +0100
commit8d2bf6ad14ffff398d0ab4c9558bc6b208f0b322 (patch)
tree73419c4740cc184d47abae88160f0c42b3581673
parent44efaef725583a8592c3660fa4267ba00903d9e4 (diff)
trivial: egg updates
-rw-r--r--libdevkit-power/egg-obj-list.c221
-rw-r--r--libdevkit-power/egg-obj-list.h21
-rw-r--r--src/egg-debug.c101
-rw-r--r--src/egg-debug.h2
-rw-r--r--src/egg-string.c315
-rw-r--r--src/egg-string.h3
6 files changed, 601 insertions, 62 deletions
diff --git a/libdevkit-power/egg-obj-list.c b/libdevkit-power/egg-obj-list.c
index 6acceb3..e64115b 100644
--- a/libdevkit-power/egg-obj-list.c
+++ b/libdevkit-power/egg-obj-list.c
@@ -36,6 +36,7 @@ struct EggObjListPrivate
EggObjListNewFunc func_new;
EggObjListCopyFunc func_copy;
EggObjListFreeFunc func_free;
+ EggObjListCompareFunc func_compare;
EggObjListToStringFunc func_to_string;
EggObjListFromStringFunc func_from_string;
GPtrArray *array;
@@ -86,6 +87,20 @@ egg_obj_list_set_free (EggObjList *list, EggObjListFreeFunc func)
}
/**
+ * egg_obj_list_set_compare:
+ * @list: a valid #EggObjList instance
+ * @func: typedef'd function
+ *
+ * Adds a compare func
+ **/
+void
+egg_obj_list_set_compare (EggObjList *list, EggObjListCompareFunc func)
+{
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ list->priv->func_compare = func;
+}
+
+/**
* egg_obj_list_set_to_string:
* @list: a valid #EggObjList instance
* @func: typedef'd function
@@ -169,6 +184,41 @@ egg_obj_list_print (EggObjList *list)
}
/**
+ * egg_obj_list_to_string:
+ * @list: a valid #EggObjList instance
+ *
+ * Converts the list to a newline delimited string
+ **/
+gchar *
+egg_obj_list_to_string (EggObjList *list)
+{
+ guint i;
+ gpointer obj;
+ GPtrArray *array;
+ gchar *text;
+ EggObjListToStringFunc func_to_string;
+ GString *string;
+
+ g_return_val_if_fail (list->priv->func_to_string != NULL, NULL);
+ g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
+
+ array = list->priv->array;
+ func_to_string = list->priv->func_to_string;
+ string = g_string_new ("");
+ for (i=0; i<array->len; i++) {
+ obj = g_ptr_array_index (array, i);
+ text = func_to_string (obj);
+ g_string_append_printf (string, "%s\n", text);
+ g_free (text);
+ }
+ /* remove trailing newline */
+ if (string->len != 0)
+ g_string_set_size (string, string->len-1);
+
+ return g_string_free (string, FALSE);
+}
+
+/**
* egg_obj_list_add:
* @list: a valid #EggObjList instance
* @obj: a valid #gpointer object
@@ -176,7 +226,7 @@ egg_obj_list_print (EggObjList *list)
* Adds a copy of the object to the list
**/
void
-egg_obj_list_add (EggObjList *list, const gpointer obj)
+egg_obj_list_add (EggObjList *list, gconstpointer obj)
{
gpointer obj_new;
@@ -191,32 +241,167 @@ egg_obj_list_add (EggObjList *list, const gpointer obj)
}
/**
+ * egg_obj_list_add_list:
+ *
+ * Makes a deep copy of the list
+ **/
+void
+egg_obj_list_add_list (EggObjList *list, const EggObjList *data)
+{
+ guint i;
+ gconstpointer obj;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ g_return_if_fail (EGG_IS_OBJ_LIST (data));
+
+ /* add data items to list */
+ for (i=0; i < data->len; i++) {
+ obj = egg_obj_list_index (data, i);
+ egg_obj_list_add (list, obj);
+ }
+}
+
+/**
+ * egg_obj_list_remove_list:
+ *
+ * Makes a deep copy of the list
+ **/
+void
+egg_obj_list_remove_list (EggObjList *list, const EggObjList *data)
+{
+ guint i;
+ gconstpointer obj;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ g_return_if_fail (EGG_IS_OBJ_LIST (data));
+
+ /* remove data items from list */
+ for (i=0; i < data->len; i++) {
+ obj = egg_obj_list_index (data, i);
+ egg_obj_list_remove (list, obj);
+ }
+}
+
+/**
+ * egg_obj_list_find_obj:
+ * @list: a valid #EggObjList instance
+ * @obj: a valid #gpointer object
+ *
+ * Return value: the object
+ *
+ * Removes an item from a list
+ **/
+static gboolean
+egg_obj_list_obj_equal (EggObjList *list, gconstpointer obj1, gconstpointer obj2)
+{
+ EggObjListCompareFunc func_compare;
+
+ /* two less pointer deferences... */
+ func_compare = list->priv->func_compare;
+
+ /* trivial case */
+ if (func_compare == NULL)
+ return obj1 == obj2;
+
+ /* use helper function */
+ return func_compare (obj1, obj2) == 0;
+}
+
+/**
+ * egg_obj_list_remove_duplicate:
+ *
+ * Removes duplicate entries
+ **/
+void
+egg_obj_list_remove_duplicate (EggObjList *list)
+{
+ guint i, j;
+ gconstpointer obj1;
+ gconstpointer obj2;
+
+ for (i=0; i<list->len; i++) {
+ obj1 = egg_obj_list_index (list, i);
+ for (j=0; j<list->len; j++) {
+ if (i == j)
+ break;
+ obj2 = egg_obj_list_index (list, j);
+ if (egg_obj_list_obj_equal (list, obj1, obj2))
+ egg_obj_list_remove_index (list, i);
+ }
+ }
+}
+
+/**
+ * egg_obj_list_find_obj:
+ * @list: a valid #EggObjList instance
+ * @obj: a valid #gpointer object
+ *
+ * Return value: the object
+ *
+ * Removes an item from a list
+ **/
+static gpointer
+egg_obj_list_find_obj (EggObjList *list, gconstpointer obj)
+{
+ guint i;
+ gconstpointer obj_tmp;
+ EggObjListCompareFunc func_compare;
+
+ /* the pointers point to the same thing */
+ func_compare = list->priv->func_compare;
+ if (func_compare == NULL)
+ return (gpointer) obj;
+
+ /* remove data items from list */
+ for (i=0; i < list->len; i++) {
+ obj_tmp = egg_obj_list_index (list, i);
+ if (func_compare (obj_tmp, obj) == 0)
+ return (gpointer) obj_tmp;
+ }
+
+ /* nothing found */
+ return NULL;
+}
+
+/**
* egg_obj_list_remove:
* @list: a valid #EggObjList instance
* @obj: a valid #gpointer object
*
* Return value: TRUE is we removed something
*
- * Removes an item from a list
+ * Removes all the items from a list matching obj
**/
gboolean
-egg_obj_list_remove (EggObjList *list, const gpointer obj)
+egg_obj_list_remove (EggObjList *list, gconstpointer obj)
{
gboolean ret;
gpointer obj_new;
+ gboolean found = FALSE;
g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
g_return_val_if_fail (obj != NULL, FALSE);
g_return_val_if_fail (list->priv->func_free != NULL, FALSE);
- /* the pointers point to the same thing */
- obj_new = (gpointer) obj;
- ret = g_ptr_array_remove (list->priv->array, obj_new);
- if (!ret)
- return FALSE;
- list->priv->func_free (obj_new);
- list->len = list->priv->array->len;
- return TRUE;
+ do {
+ /* get the object */
+ obj_new = egg_obj_list_find_obj (list, obj);
+ if (obj_new == NULL)
+ break;
+
+ /* try to remove */
+ ret = g_ptr_array_remove (list->priv->array, obj_new);
+
+ /* no compare function, and pointer not found */
+ if (!ret)
+ break;
+
+ found = TRUE;
+ list->priv->func_free (obj_new);
+ list->len = list->priv->array->len;
+ } while (ret);
+
+ return found;
}
/**
@@ -256,7 +441,7 @@ gboolean
egg_obj_list_to_file (EggObjList *list, const gchar *filename)
{
guint i;
- gpointer obj;
+ gconstpointer obj;
gchar *part;
GString *string;
gboolean ret = TRUE;
@@ -378,15 +563,15 @@ out:
*
* Gets an object from the list
**/
-const gpointer
+gconstpointer
egg_obj_list_index (const EggObjList *list, guint index)
{
- gpointer obj;
+ gconstpointer obj;
g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
obj = g_ptr_array_index (list->priv->array, index);
- return (const gpointer) obj;
+ return obj;
}
/**
@@ -441,6 +626,7 @@ egg_obj_list_init (EggObjList *list)
list->priv->func_new = NULL;
list->priv->func_copy = NULL;
list->priv->func_free = NULL;
+ list->priv->func_compare = NULL;
list->priv->func_to_string = NULL;
list->priv->func_from_string = NULL;
list->priv->array = g_ptr_array_new ();
@@ -477,10 +663,7 @@ egg_obj_list_test (EggTest *test)
/************************************************************/
egg_test_title (test, "get an instance");
list = egg_obj_list_new ();
- if (list != NULL)
- egg_test_success (test, NULL);
- else
- egg_test_failed (test, NULL);
+ egg_test_assert (test, list != NULL);
g_object_unref (list);
diff --git a/libdevkit-power/egg-obj-list.h b/libdevkit-power/egg-obj-list.h
index 79e9b76..5237df7 100644
--- a/libdevkit-power/egg-obj-list.h
+++ b/libdevkit-power/egg-obj-list.h
@@ -48,10 +48,12 @@ typedef struct
} EggObjListClass;
typedef gpointer (*EggObjListNewFunc) (void);
-typedef gpointer (*EggObjListCopyFunc) (const gpointer data);
+typedef gpointer (*EggObjListCopyFunc) (gconstpointer data);
typedef void (*EggObjListFreeFunc) (gpointer data);
+typedef gint (*EggObjListCompareFunc) (gconstpointer data1,
+ gconstpointer data2);
typedef gpointer (*EggObjListFromStringFunc) (const gchar *data);
-typedef gchar *(*EggObjListToStringFunc) (gpointer data);
+typedef gchar *(*EggObjListToStringFunc) (gconstpointer data);
GType egg_obj_list_get_type (void) G_GNUC_CONST;
EggObjList *egg_obj_list_new (void);
@@ -62,23 +64,32 @@ void egg_obj_list_set_copy (EggObjList *list,
EggObjListCopyFunc func);
void egg_obj_list_set_free (EggObjList *list,
EggObjListFreeFunc func);
+void egg_obj_list_set_compare (EggObjList *list,
+ EggObjListCompareFunc func);
void egg_obj_list_set_to_string (EggObjList *list,
EggObjListToStringFunc func);
void egg_obj_list_set_from_string (EggObjList *list,
EggObjListFromStringFunc func);
void egg_obj_list_clear (EggObjList *list);
void egg_obj_list_print (EggObjList *list);
+gchar *egg_obj_list_to_string (EggObjList *list)
+ G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_obj_list_to_file (EggObjList *list,
const gchar *filename);
gboolean egg_obj_list_from_file (EggObjList *list,
const gchar *filename);
void egg_obj_list_add (EggObjList *list,
- const gpointer data);
+ gconstpointer data);
+void egg_obj_list_add_list (EggObjList *list,
+ const EggObjList *data);
+void egg_obj_list_remove_list (EggObjList *list,
+ const EggObjList *data);
+void egg_obj_list_remove_duplicate (EggObjList *list);
gboolean egg_obj_list_remove (EggObjList *list,
- const gpointer data);
+ gconstpointer data);
gboolean egg_obj_list_remove_index (EggObjList *list,
guint index);
-const gpointer egg_obj_list_index (const EggObjList *list,
+gconstpointer egg_obj_list_index (const EggObjList *list,
guint index);
G_END_DECLS
diff --git a/src/egg-debug.c b/src/egg-debug.c
index 2a37397..3920add 100644
--- a/src/egg-debug.c
+++ b/src/egg-debug.c
@@ -53,24 +53,9 @@
#define CONSOLE_CYAN 36
#define CONSOLE_WHITE 37
-static gboolean do_verbose = FALSE; /* if we should print out debugging */
-static gboolean do_logging = FALSE; /* if we should write to a file */
-static gboolean is_console = FALSE;
static gint fd = -1;
/**
- * egg_debug_set_logging:
- **/
-void
-egg_debug_set_logging (gboolean enabled)
-{
- do_logging = enabled;
- if (enabled) {
- egg_debug ("now logging to %s", EGG_LOG_FILE);
- }
-}
-
-/**
* pk_set_console_mode:
**/
static void
@@ -79,9 +64,9 @@ pk_set_console_mode (guint console_code)
gchar command[13];
/* don't put extra commands into logs */
- if (!is_console) {
+ if (!egg_debug_is_console ())
return;
- }
+
/* Command is the control command to the terminal */
g_snprintf (command, 13, "%c[%dm", 0x1B, console_code);
printf ("%s", command);
@@ -123,21 +108,18 @@ pk_log_line (const gchar *buffer)
if (fd == -1) {
/* ITS4: ignore, /var/log/foo is owned by root, and this is just debug text */
fd = open (EGG_LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0777);
- if (fd == -1) {
+ if (fd == -1)
g_error ("could not open log: '%s'", EGG_LOG_FILE);
- }
}
/* ITS4: ignore, debug text always NULL terminated */
count = write (fd, buffer, strlen (buffer));
- if (count == -1) {
+ if (count == -1)
g_warning ("could not write %s", buffer);
- }
/* newline */
count = write (fd, "\n", 1);
- if (count == -1) {
+ if (count == -1)
g_warning ("could not write newline");
- }
}
/**
@@ -170,7 +152,7 @@ pk_print_line (const gchar *func, const gchar *file, const int line, const gchar
pk_set_console_mode (CONSOLE_RESET);
/* log to a file */
- if (do_logging) {
+ if (egg_debug_is_logging ()) {
pk_log_line (header);
pk_log_line (buffer);
}
@@ -190,9 +172,8 @@ egg_debug_real (const gchar *func, const gchar *file, const int line, const gcha
va_list args;
gchar *buffer = NULL;
- if (do_verbose == FALSE) {
+ if (!egg_debug_enabled ())
return;
- }
va_start (args, format);
g_vasprintf (&buffer, format, args);
@@ -212,18 +193,16 @@ egg_warning_real (const gchar *func, const gchar *file, const int line, const gc
va_list args;
gchar *buffer = NULL;
- if (do_verbose == FALSE) {
+ if (!egg_debug_enabled ())
return;
- }
va_start (args, format);
g_vasprintf (&buffer, format, args);
va_end (args);
/* do extra stuff for a warning */
- if (!is_console) {
+ if (!egg_debug_is_console ())
printf ("*** WARNING ***\n");
- }
pk_print_line (func, file, line, buffer, CONSOLE_RED);
g_free(buffer);
@@ -243,9 +222,8 @@ egg_error_real (const gchar *func, const gchar *file, const int line, const gcha
va_end (args);
/* do extra stuff for a warning */
- if (!is_console) {
+ if (!egg_debug_is_console ())
printf ("*** ERROR ***\n");
- }
pk_print_line (func, file, line, buffer, CONSOLE_RED);
g_free(buffer);
@@ -263,7 +241,50 @@ egg_error_real (const gchar *func, const gchar *file, const int line, const gcha
gboolean
egg_debug_enabled (void)
{
- return do_verbose;
+ const gchar *env;
+ env = g_getenv (EGG_VERBOSE);
+ return (g_strcmp0 (env, "1") == 0);
+}
+
+/**
+ * egg_debug_is_logging:
+ *
+ * Returns: TRUE if we have logging enabled
+ **/
+gboolean
+egg_debug_is_logging (void)
+{
+ const gchar *env;
+ env = g_getenv (EGG_LOGGING);
+ return (g_strcmp0 (env, "1") == 0);
+}
+
+/**
+ * egg_debug_is_console:
+ *
+ * Returns: TRUE if we have debugging enabled
+ **/
+gboolean
+egg_debug_is_console (void)
+{
+ const gchar *env;
+ env = g_getenv (EGG_CONSOLE);
+ return (g_strcmp0 (env, "1") == 0);
+}
+
+/**
+ * egg_debug_set_logging:
+ **/
+void
+egg_debug_set_logging (gboolean enabled)
+{
+ if (enabled)
+ g_setenv (EGG_LOGGING, "1", FALSE);
+ else
+ g_setenv (EGG_LOGGING, "0", FALSE);
+
+ if (egg_debug_is_logging ())
+ egg_debug ("logging to %s", EGG_LOG_FILE);
}
/**
@@ -273,11 +294,15 @@ egg_debug_enabled (void)
void
egg_debug_init (gboolean debug)
{
- do_verbose = debug;
/* check if we are on console */
- if (isatty (fileno (stdout)) == 1) {
- is_console = TRUE;
- }
- egg_debug ("Verbose debugging %i (on console %i)", do_verbose, is_console);
+ if (isatty (fileno (stdout)) == 1)
+ g_setenv (EGG_CONSOLE, "1", FALSE);
+ else
+ g_setenv (EGG_CONSOLE, "0", FALSE);
+ if (debug)
+ g_setenv (EGG_VERBOSE, "1", FALSE);
+ else
+ g_setenv (EGG_VERBOSE, "0", FALSE);
+ egg_debug ("Verbose debugging %i (on console %i)%s", egg_debug_enabled (), egg_debug_is_console (), EGG_VERBOSE);
}
diff --git a/src/egg-debug.h b/src/egg-debug.h
index 9683846..e3df542 100644
--- a/src/egg-debug.h
+++ b/src/egg-debug.h
@@ -62,6 +62,8 @@ G_BEGIN_DECLS
void egg_debug_init (gboolean debug);
void egg_debug_set_logging (gboolean enabled);
gboolean egg_debug_enabled (void);
+gboolean egg_debug_is_logging (void);
+gboolean egg_debug_is_console (void);
void egg_debug_backtrace (void);
void egg_debug_real (const gchar *func,
const gchar *file,
diff --git a/src/egg-string.c b/src/egg-string.c
index 8ddb6af..33df11e 100644
--- a/src/egg-string.c
+++ b/src/egg-string.c
@@ -197,6 +197,45 @@ egg_strequal (const gchar *id1, const gchar *id2)
}
/**
+ * egg_strvequal:
+ * @id1: the first item of text to test
+ * @id2: the second item of text to test
+ *
+ * This function will check to see if the GStrv arrays are string equal
+ *
+ * Return value: %TRUE if the arrays are the same, or are both %NULL
+ **/
+gboolean
+egg_strvequal (gchar **id1, gchar **id2)
+{
+ guint i;
+ guint length1;
+ guint length2;
+
+ if (id1 == NULL && id2 == NULL)
+ return TRUE;
+
+ if (id1 == NULL || id2 == NULL) {
+ egg_debug ("GStrv compare invalid '%p' and '%p'", id1, id2);
+ return FALSE;
+ }
+
+ /* check different sizes */
+ length1 = g_strv_length (id1);
+ length2 = g_strv_length (id2);
+ if (length1 != length2)
+ return FALSE;
+
+ /* text equal each one */
+ for (i=0; i<length1; i++) {
+ if (!egg_strequal (id1[i], id2[i]))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/**
* egg_strreplace:
* @text: The input text to make safe
* @find: What to search for
@@ -225,3 +264,279 @@ egg_strreplace (const gchar *text, const gchar *find, const gchar *replace)
return retval;
}
+/***************************************************************************
+ *** MAKE CHECK TESTS ***
+ ***************************************************************************/
+#ifdef EGG_TEST
+#include "egg-test.h"
+
+void
+egg_string_test (EggTest *test)
+{
+ gboolean ret;
+ gchar *text_safe;
+ const gchar *temp;
+ guint length;
+ gint value;
+ guint uvalue;
+ gchar **id1;
+ gchar **id2;
+
+ if (!egg_test_start (test, "EggString"))
+ return;
+
+ /************************************************************
+ **************** String equal ******************
+ ************************************************************/
+ egg_test_title (test, "egg_strequal same argument");
+ temp = "dave";
+ if (egg_strequal (temp, temp))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "incorrect ret when both same");
+
+ /************************************************************/
+ egg_test_title (test, "egg_strequal both const");
+ if (egg_strequal ("dave", "dave"))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "incorrect ret when both same");
+
+ /************************************************************
+ **************** String array equal ******************
+ ************************************************************/
+ egg_test_title (test, "egg_strvequal same argument");
+ id1 = g_strsplit ("the quick brown fox", " ", 0);
+ if (egg_strvequal (id1, id1))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "incorrect ret when both same");
+ g_strfreev (id1);
+
+ /************************************************************/
+ egg_test_title (test, "egg_strvequal same");
+ id1 = g_strsplit ("the quick brown fox", " ", 0);
+ id2 = g_strsplit ("the quick brown fox", " ", 0);
+ if (egg_strvequal (id1, id2))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "incorrect ret when both same");
+ g_strfreev (id1);
+ g_strfreev (id2);
+
+ /************************************************************/
+ egg_test_title (test, "egg_strvequal different lengths");
+ id1 = g_strsplit ("the quick brown", " ", 0);
+ id2 = g_strsplit ("the quick brown fox", " ", 0);
+ if (!egg_strvequal (id1, id2))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "incorrect ret when both same");
+ g_strfreev (id1);
+ g_strfreev (id2);
+
+ /************************************************************/
+ egg_test_title (test, "egg_strvequal different");
+ id1 = g_strsplit ("the quick brown fox", " ", 0);
+ id2 = g_strsplit ("richard hughes maintainer dude", " ", 0);
+ if (!egg_strvequal (id1, id2))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "same when different");
+ g_strfreev (id1);
+ g_strfreev (id2);
+
+ /************************************************************
+ **************** Zero ******************
+ ************************************************************/
+ temp = NULL;
+ egg_test_title (test, "test strzero (null)");
+ ret = egg_strzero (NULL);
+ if (ret)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed null");
+
+ /************************************************************/
+ egg_test_title (test, "test strzero (null first char)");
+ ret = egg_strzero ("");
+ if (ret)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed null");
+
+ /************************************************************/
+ egg_test_title (test, "test strzero (long string)");
+ ret = egg_strzero ("Richard");
+ if (!ret)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "zero length word!");
+
+ /************************************************************/
+ egg_test_title (test, "id strcmp pass");
+ ret = egg_strequal ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora");
+ egg_test_assert (test, ret);
+
+ /************************************************************/
+ egg_test_title (test, "id strcmp fail");
+ ret = egg_strequal ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora");
+ egg_test_assert (test, !ret);
+
+ /************************************************************
+ **************** strlen ******************
+ ************************************************************/
+ egg_test_title (test, "strlen bigger");
+ length = egg_strlen ("123456789", 20);
+ if (length == 9)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the strlen %i", length);
+
+ /************************************************************/
+ egg_test_title (test, "strlen smaller");
+ length = egg_strlen ("123456789", 5);
+ if (length == 5)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the strlen %i", length);
+
+ /************************************************************/
+ egg_test_title (test, "strlen correct");
+ length = egg_strlen ("123456789", 9);
+ if (length == 9)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the strlen %i", length);
+
+ /************************************************************
+ **************** Replace ******************
+ ************************************************************/
+ egg_test_title (test, "replace start");
+ text_safe = egg_strreplace ("richard\nhughes", "r", "e");
+ if (egg_strequal (text_safe, "eichaed\nhughes"))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the replace '%s'", text_safe);
+ g_free (text_safe);
+
+ /************************************************************/
+ egg_test_title (test, "replace none");
+ text_safe = egg_strreplace ("richard\nhughes", "dave", "e");
+ if (egg_strequal (text_safe, "richard\nhughes"))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the replace '%s'", text_safe);
+ g_free (text_safe);
+
+ /************************************************************/
+ egg_test_title (test, "replace end");
+ text_safe = egg_strreplace ("richard\nhughes", "s", "e");
+ if (egg_strequal (text_safe, "richard\nhughee"))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the replace '%s'", text_safe);
+ g_free (text_safe);
+
+ /************************************************************/
+ egg_test_title (test, "replace unicode");
+ text_safe = egg_strreplace ("richard\n- hughes", "\n- ", "\n• ");
+ if (egg_strequal (text_safe, "richard\n• hughes"))
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "failed the replace '%s'", text_safe);
+ g_free (text_safe);
+
+ /************************************************************
+ ************** Check for numbers ****************
+ ************************************************************/
+ egg_test_title (test, "check number valid");
+ ret = egg_strnumber ("123");
+ egg_test_assert (test, ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number valid");
+ ret = egg_strnumber ("-123");
+ egg_test_assert (test, ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number zero");
+ ret = egg_strnumber ("0");
+ egg_test_assert (test, ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number oversize");
+ ret = egg_strnumber ("123456891234");
+ egg_test_assert (test, !ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number NULL");
+ ret = egg_strnumber (NULL);
+ egg_test_assert (test, !ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number blank");
+ ret = egg_strnumber ("");
+ egg_test_assert (test, !ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number not negative");
+ ret = egg_strnumber ("503-");
+ egg_test_assert (test, !ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number positive");
+ ret = egg_strnumber ("+503");
+ egg_test_assert (test, !ret);
+
+ /************************************************************/
+ egg_test_title (test, "check number random chars");
+ ret = egg_strnumber ("dave");
+ egg_test_assert (test, !ret);
+
+ /************************************************************
+ ************** Convert numbers ****************
+ ************************************************************/
+ egg_test_title (test, "convert valid number");
+ ret = egg_strtoint ("234", &value);
+ if (ret && value == 234)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", value);
+
+ /************************************************************/
+ egg_test_title (test, "convert negative valid number");
+ ret = egg_strtoint ("-234", &value);
+ if (ret && value == -234)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", value);
+
+ /************************************************************/
+ egg_test_title (test, "don't convert invalid number");
+ ret = egg_strtoint ("dave", &value);
+ if (ret == FALSE && value == 0)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", value);
+
+ /************************************************************/
+ egg_test_title (test, "convert valid uint number");
+ ret = egg_strtouint ("234", &uvalue);
+ if (ret && uvalue == 234)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", uvalue);
+
+ /************************************************************/
+ egg_test_title (test, "convert invalid uint number");
+ ret = egg_strtouint ("-234", &uvalue);
+ if (ret == FALSE && uvalue == 0)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", uvalue);
+
+ egg_test_end (test);
+}
+#endif
+
diff --git a/src/egg-string.h b/src/egg-string.h
index 5331b4c..5414907 100644
--- a/src/egg-string.h
+++ b/src/egg-string.h
@@ -34,6 +34,9 @@ gboolean egg_strzero (const gchar *text)
gboolean egg_strequal (const gchar *id1,
const gchar *id2)
G_GNUC_WARN_UNUSED_RESULT;
+gboolean egg_strvequal (gchar **id1,
+ gchar **id2)
+ G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_strnumber (const gchar *text)
G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_strtoint (const gchar *text,