diff options
author | Christopher James Lahey <clahey@ximian.com> | 2001-02-07 11:28:54 +0800 |
---|---|---|
committer | Chris Lahey <clahey@src.gnome.org> | 2001-02-07 11:28:54 +0800 |
commit | 758b87ad3702f4c0b55c0b3174b521f365ff5896 (patch) | |
tree | dd377d7e7de58e38dfc6026df854918a36e73603 /e-util/e-util.c | |
parent | 71c965734eb20a39456ecc7c6f87052059cd23d4 (diff) | |
download | gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar.gz gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar.bz2 gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar.lz gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar.xz gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.tar.zst gsoc2013-evolution-758b87ad3702f4c0b55c0b3174b521f365ff5896.zip |
Added e_format_number and e_create_directory.
2001-02-06 Christopher James Lahey <clahey@ximian.com>
* gal/util/e-util.c, gal/util/e-util.h (e_format_number,
e_create_directory): Added e_format_number and e_create_directory.
svn path=/trunk/; revision=8042
Diffstat (limited to 'e-util/e-util.c')
-rw-r--r-- | e-util/e-util.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/e-util/e-util.c b/e-util/e-util.c index 5bffc9e4a9..4840bf0ffd 100644 --- a/e-util/e-util.c +++ b/e-util/e-util.c @@ -28,6 +28,8 @@ #include <ctype.h> #include <sys/stat.h> #include <string.h> +#include <locale.h> +#include <stdio.h> #include "e-util.h" #if 0 @@ -536,3 +538,133 @@ e_filename_make_safe (gchar *string) *p = '_'; } } + +static gint +epow10 (gint number) { + gint value; + + for (value = 1; number > 0; number --) { + value *= 10; + } + return value; +} + +gchar * +e_format_number (gint 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; + + locality = localeconv(); + grouping = locality->grouping; + while (number) { + char *group; + switch (*grouping) { + default: + last_count = *grouping; + grouping++; + case 0: + divider = epow10(last_count); + group = g_strdup_printf("%d", number % divider); + number /= divider; + break; + case CHAR_MAX: + group = g_strdup_printf("%d", number); + number = 0; + break; + } + char_length += strlen(group); + list = g_list_prepend(list, group); + group_count ++; + } + + if (list) { + value = g_new(char, 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"); + } +} + +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); +} |