changeset 9233:0c352d0e4ddc

[gaim-migrate @ 10029] gaim_str_seconds_to_string() was overly complex, was painful to read, and had far too many nested if statements. I've replaced it with a very simple, easy to read function. Also, fixed a warning with rizzo's patch. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Mon, 07 Jun 2004 10:09:29 +0000
parents 7ffeabe6a09e
children f18eb3f22733
files src/util.c
diffstat 1 files changed, 36 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/util.c	Mon Jun 07 09:58:54 2004 +0000
+++ b/src/util.c	Mon Jun 07 10:09:29 2004 +0000
@@ -2333,51 +2333,44 @@
 }
 
 char *
-gaim_str_seconds_to_string(guint sec)
+gaim_str_seconds_to_string(guint secs)
 {
-	guint daze, hrs, min;
-	char *ret = NULL;
-
-	daze = sec / (60 * 60 * 24);
-	hrs = (sec % (60 * 60 * 24)) / (60 * 60);
-	min = (sec % (60 * 60)) / 60;
-	sec = min % 60;
-
-	if (daze) {
-		if (hrs || min) {
-			if (hrs) {
-				if (min) {
-					ret = g_strdup_printf(
-						   "%d %s, %d %s, %d %s.",
-						   daze, ngettext("day","days",daze),
-						   hrs, ngettext("hour","hours",hrs), min, ngettext("minute","minutes",min));
-				} else {
-					ret = g_strdup_printf(
-						   "%d %s, %d %s.",
-						   daze, ngettext("day","days",daze), hrs, ngettext("hour","hours",hrs));
-				}
-			} else {
-				ret = g_strdup_printf(
-					   "%d %s, %d %s.",
-					   daze, ngettext("day","days",daze), min, ngettext("minute","minutes",min));
-			}
-		} else
-			ret = g_strdup_printf("%d %s.", daze, ngettext("day","days",daze));
-	} else {
-		if (hrs) {
-			if (min) {
-				ret = g_strdup_printf(
-					   "%d %s, %d %s.",
-					   hrs, ngettext("hour","hours",hrs), min, ngettext("minute","minutes",min));
-			} else {
-				ret = g_strdup_printf("%d %s.", hrs, ngettext("hour","hours",hrs));
-			}
-		} else {
-			ret = g_strdup_printf("%d %s.", min, ngettext("minute","minutes",min));
-		}
+	GString *gstr;
+	const char *prefix = "";
+	guint days, hrs, mins;
+
+	days = secs / (60 * 60 * 24);
+	secs = secs % (60 * 60 * 24);
+	hrs  = secs / (60 * 60);
+	secs = secs % (60 * 60);
+	mins = secs / 60;
+	secs = secs % 60;
+
+	gstr = g_string_new("");
+
+	if (days > 0)
+	{
+		g_string_append_printf(gstr, "%d %s", days,
+							   ngettext("day", "days", days));
+
+		prefix = ", ";
 	}
 
-	return ret;
+	if (hrs > 0)
+	{
+		g_string_append_printf(gstr, "%s%d %s", prefix, hrs,
+							   ngettext("hour", "hours", hrs));
+
+		prefix = ", ";
+	}
+
+	if (mins > 0)
+	{
+		g_string_append_printf(gstr, "%s%d %s", prefix, mins,
+							   ngettext("minute", "minutes", mins));
+	}
+
+	return g_string_free(gstr, FALSE);
 }
 
 /**************************************************************************
@@ -2415,7 +2408,7 @@
 	}
 
 	/* parse out authentication information if supplied */
-	if (at = strchr(url, '@')) {
+	if ((at = strchr(url, '@')) != NULL) {
 		g_snprintf(scan_info, sizeof(scan_info),
 					"%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl);
 		f = sscanf(url, scan_info, user, passwd);