From c049d43462e76a1f96891819fdf8798ed52a513e Mon Sep 17 00:00:00 2001 From: Christopher James Lahey Date: Mon, 29 Apr 2002 21:53:41 +0000 Subject: Changed this to handle large numbers. (e_create_directory): Changed this 2002-04-29 Christopher James Lahey * gal/util/e-util.c (e_format_number_float): Changed this to handle large numbers. (e_create_directory): Changed this to be a wrapper around e_mkdir_hier. svn path=/trunk/; revision=16639 --- e-util/e-util.c | 176 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 111 insertions(+), 65 deletions(-) (limited to 'e-util/e-util.c') diff --git a/e-util/e-util.c b/e-util/e-util.c index 5d8e9b19db..7a10d71b34 100644 --- a/e-util/e-util.c +++ b/e-util/e-util.c @@ -36,6 +36,9 @@ #include #include #include +#include +#include +#include #if 0 #include @@ -248,13 +251,22 @@ e_mkdir_hier(const char *path, mode_t mode) { char *copy, *p; - p = copy = g_strdup (path); + if (path[0] == '/') { + p = copy = g_strdup (path); + } else { + gchar *current_dir = g_get_current_dir(); + p = copy = g_concat_dir_and_file (current_dir, path); + } + do { p = strchr (p + 1, '/'); if (p) *p = '\0'; - if (access (copy, F_OK) == -1) { - if (mkdir (copy, mode) == -1) { + if (mkdir (copy, mode) == -1) { + switch (errno) { + case EEXIST: + break; + default: g_free (copy); return -1; } @@ -1019,10 +1031,97 @@ e_format_number (gint number) } } +static gchar * +do_format_number_as_float (double number) +{ + GList *iterator, *list = NULL; + struct lconv *locality; + gint char_length = 0; + gint group_count = 0; + guchar *grouping; + int last_count = 3; + int divider; + char *value; + char *value_iterator; + double fractional; + + locality = localeconv(); + grouping = locality->grouping; + while (number >= 1.0) { + char *group; + switch (*grouping) { + default: + last_count = *grouping; + grouping++; + /* Fall through */ + case 0: + divider = epow10(last_count); + number /= divider; + fractional = modf (number, &number); + fractional *= divider; + fractional = floor (fractional); + + if (number >= 1.0) { + group = g_strdup_printf("%0*d", last_count, (int) fractional); + } else { + group = g_strdup_printf("%d", (int) fractional); + } + break; + case CHAR_MAX: + divider = epow10(last_count); + number /= divider; + fractional = modf (number, &number); + fractional *= divider; + fractional = floor (fractional); + + while (number >= 1.0) { + group = g_strdup_printf("%0*d", last_count, (int) fractional); + + char_length += strlen(group); + list = g_list_prepend(list, group); + group_count ++; + + divider = epow10(last_count); + number /= divider; + fractional = modf (number, &number); + fractional *= divider; + fractional = floor (fractional); + } + + group = g_strdup_printf("%d", (int) fractional); + break; + } + char_length += strlen(group); + list = g_list_prepend(list, group); + group_count ++; + } + + if (list) { + value = g_new(char, 1 + char_length + (group_count - 1) * strlen(locality->thousands_sep)); + + iterator = list; + value_iterator = value; + + strcpy(value_iterator, iterator->data); + value_iterator += strlen(iterator->data); + for (iterator = iterator->next; iterator; iterator = iterator->next) { + strcpy(value_iterator, locality->thousands_sep); + value_iterator += strlen(locality->thousands_sep); + + strcpy(value_iterator, iterator->data); + value_iterator += strlen(iterator->data); + } + e_free_string_list (list); + return value; + } else { + return g_strdup("0"); + } +} + gchar * e_format_number_float (gfloat number) { - gint int_part; + gfloat int_part; gint fraction; struct lconv *locality; gchar *str_intpart; @@ -1032,8 +1131,8 @@ e_format_number_float (gfloat number) locality = localeconv(); - int_part = (int) number; - str_intpart = e_format_number (int_part); + int_part = floor (number); + str_intpart = do_format_number_as_float ((double) int_part); if (!strcmp(locality->mon_decimal_point, "")) { decimal_point = "."; @@ -1046,8 +1145,7 @@ e_format_number_float (gfloat number) if (fraction == 0) { str_fraction = g_strdup ("00"); - } - else { + } else { str_fraction = g_strdup_printf ("%02d", fraction); } @@ -1062,63 +1160,11 @@ e_format_number_float (gfloat number) gboolean e_create_directory (gchar *directory) { - gchar *full_name; - gchar *position; - gchar *current_dir = g_get_current_dir(); - struct stat info; - gboolean return_value = TRUE; - - if (directory[0] == '/') { - full_name = g_malloc0 (strlen (directory) + 1); - strcpy (full_name, directory); - } else { - full_name = g_malloc0 (strlen (directory) + strlen (current_dir) + 2); - sprintf (full_name, "%s/%s", current_dir, directory); - } - - if ((position = strrchr (full_name, '/')) == full_name) { - if (stat (full_name, &info)) { - switch (errno) { - case ENOENT: - if (mkdir (full_name, 0777)) { - switch (errno) { - default: - return_value = FALSE; - break; - } - } - break; - default: - return_value = FALSE; - break; - } - } - } else { - *position = 0; - e_create_directory (full_name); - *position = '/'; - if (stat (full_name, &info)) { - switch (errno) { - case ENOENT: - if (mkdir (full_name, 0777)) { - switch (errno) { - default: - return_value = FALSE; - break; - } - } - break; - default: - return_value = FALSE; - break; - } - } - } - - g_free (current_dir); - g_free (full_name); - - return (return_value); + gint ret_val = e_mkdir_hier (directory, 0777); + if (ret_val == -1) + return FALSE; + else + return TRUE; } -- cgit