2
0
Fork 0

Add l_g_string_from_int to utils - prints a gint64 to a GString

This commit is contained in:
Stefan Bühler 2009-03-16 22:24:14 +01:00
parent 669ff2e079
commit 9df3918e9b
2 changed files with 47 additions and 0 deletions

View File

@ -66,6 +66,8 @@ LI_API GString *l_g_string_assign_len(GString *string, const gchar *val, gssize
LI_API gboolean l_g_string_prefix(GString *str, const gchar *s, gsize len);
LI_API gboolean l_g_string_suffix(GString *str, const gchar *s, gsize len);
LI_API GString *l_g_string_from_int(GString *dest, gint64 val);
LI_API gsize dirent_buf_size(DIR * dirp);
#endif

View File

@ -580,6 +580,51 @@ GString *l_g_string_assign_len(GString *string, const gchar *val, gssize len) {
return string;
}
GString *l_g_string_from_int(GString *dest, gint64 v) {
gchar *buf, *end, swap;
guint len;
guint64 val;
if (!dest) {
dest = g_string_sized_new(21);
} else {
g_string_set_size(dest, 21);
}
len = 1;
buf = dest->str;
if (v < 0) {
len++;
*(buf++) = '-';
/* -v maybe < 0 for signed types, so just cast it to unsigned to get the correct value */
val = -v;
} else {
val = v;
}
end = buf;
while (val > 9) {
*(end++) = '0' + (val % 10);
val = val / 10;
}
*(end) = '0' + val;
*(end + 1) = '\0';
len += end - buf;
while (buf < end) {
swap = *end;
*end = *buf;
*buf = swap;
buf++;
end--;
}
dest->len = len;
return dest;
}
/* http://womble.decadentplace.org.uk/readdir_r-advisory.html */
gsize dirent_buf_size(DIR * dirp) {
glong name_max;