diff options
author | Richard Hughes <richard@hughsie.com> | 2008-10-23 12:03:35 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2008-10-23 12:03:35 +0100 |
commit | 8d2bf6ad14ffff398d0ab4c9558bc6b208f0b322 (patch) | |
tree | 73419c4740cc184d47abae88160f0c42b3581673 /src | |
parent | 44efaef725583a8592c3660fa4267ba00903d9e4 (diff) |
trivial: egg updates
Diffstat (limited to 'src')
-rw-r--r-- | src/egg-debug.c | 101 | ||||
-rw-r--r-- | src/egg-debug.h | 2 | ||||
-rw-r--r-- | src/egg-string.c | 315 | ||||
-rw-r--r-- | src/egg-string.h | 3 |
4 files changed, 383 insertions, 38 deletions
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, |