comparison src/util.c @ 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 b83905afbb55
children f1d87ab17e41
comparison
equal deleted inserted replaced
9232:7ffeabe6a09e 9233:0c352d0e4ddc
2331 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]); 2331 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]);
2332 } 2332 }
2333 } 2333 }
2334 2334
2335 char * 2335 char *
2336 gaim_str_seconds_to_string(guint sec) 2336 gaim_str_seconds_to_string(guint secs)
2337 { 2337 {
2338 guint daze, hrs, min; 2338 GString *gstr;
2339 char *ret = NULL; 2339 const char *prefix = "";
2340 2340 guint days, hrs, mins;
2341 daze = sec / (60 * 60 * 24); 2341
2342 hrs = (sec % (60 * 60 * 24)) / (60 * 60); 2342 days = secs / (60 * 60 * 24);
2343 min = (sec % (60 * 60)) / 60; 2343 secs = secs % (60 * 60 * 24);
2344 sec = min % 60; 2344 hrs = secs / (60 * 60);
2345 2345 secs = secs % (60 * 60);
2346 if (daze) { 2346 mins = secs / 60;
2347 if (hrs || min) { 2347 secs = secs % 60;
2348 if (hrs) { 2348
2349 if (min) { 2349 gstr = g_string_new("");
2350 ret = g_strdup_printf( 2350
2351 "%d %s, %d %s, %d %s.", 2351 if (days > 0)
2352 daze, ngettext("day","days",daze), 2352 {
2353 hrs, ngettext("hour","hours",hrs), min, ngettext("minute","minutes",min)); 2353 g_string_append_printf(gstr, "%d %s", days,
2354 } else { 2354 ngettext("day", "days", days));
2355 ret = g_strdup_printf( 2355
2356 "%d %s, %d %s.", 2356 prefix = ", ";
2357 daze, ngettext("day","days",daze), hrs, ngettext("hour","hours",hrs)); 2357 }
2358 } 2358
2359 } else { 2359 if (hrs > 0)
2360 ret = g_strdup_printf( 2360 {
2361 "%d %s, %d %s.", 2361 g_string_append_printf(gstr, "%s%d %s", prefix, hrs,
2362 daze, ngettext("day","days",daze), min, ngettext("minute","minutes",min)); 2362 ngettext("hour", "hours", hrs));
2363 } 2363
2364 } else 2364 prefix = ", ";
2365 ret = g_strdup_printf("%d %s.", daze, ngettext("day","days",daze)); 2365 }
2366 } else { 2366
2367 if (hrs) { 2367 if (mins > 0)
2368 if (min) { 2368 {
2369 ret = g_strdup_printf( 2369 g_string_append_printf(gstr, "%s%d %s", prefix, mins,
2370 "%d %s, %d %s.", 2370 ngettext("minute", "minutes", mins));
2371 hrs, ngettext("hour","hours",hrs), min, ngettext("minute","minutes",min)); 2371 }
2372 } else { 2372
2373 ret = g_strdup_printf("%d %s.", hrs, ngettext("hour","hours",hrs)); 2373 return g_string_free(gstr, FALSE);
2374 }
2375 } else {
2376 ret = g_strdup_printf("%d %s.", min, ngettext("minute","minutes",min));
2377 }
2378 }
2379
2380 return ret;
2381 } 2374 }
2382 2375
2383 /************************************************************************** 2376 /**************************************************************************
2384 * URI/URL Functions 2377 * URI/URL Functions
2385 **************************************************************************/ 2378 **************************************************************************/
2413 turl += 7; 2406 turl += 7;
2414 url = turl; 2407 url = turl;
2415 } 2408 }
2416 2409
2417 /* parse out authentication information if supplied */ 2410 /* parse out authentication information if supplied */
2418 if (at = strchr(url, '@')) { 2411 if ((at = strchr(url, '@')) != NULL) {
2419 g_snprintf(scan_info, sizeof(scan_info), 2412 g_snprintf(scan_info, sizeof(scan_info),
2420 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl); 2413 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl);
2421 f = sscanf(url, scan_info, user, passwd); 2414 f = sscanf(url, scan_info, user, passwd);
2422 2415
2423 if (f ==1 ) { 2416 if (f ==1 ) {