Let the size formatting code be re-used

This commit is contained in:
Pierre Pronchery 2015-10-04 15:10:17 +02:00
parent bc5dfecf55
commit a021628eae
2 changed files with 36 additions and 28 deletions

View File

@ -44,6 +44,7 @@ static char const _license[] =
#define COMMON_DND
#define COMMON_EXEC
#define COMMON_GET_ABSOLUTE_PATH
#define COMMON_SIZE
#include "common.c"
/* constants */
@ -1480,7 +1481,6 @@ static void _loop_insert(Browser * browser, GtkTreeIter * iter,
}
/* insert_all */
static char const * _insert_size(off_t size);
static char const * _insert_date(time_t date);
static void _insert_all(Browser * browser, struct stat * lst, struct stat * st,
@ -1502,7 +1502,7 @@ static void _insert_all(Browser * browser, struct stat * lst, struct stat * st,
*display = p; /* XXX memory leak */
*inode = lst->st_ino;
*size = lst->st_size;
*dsize = _insert_size(lst->st_size);
*dsize = _common_size(lst->st_size);
*pw = getpwuid(lst->st_uid);
*gr = getgrgid(lst->st_gid);
*ddate = _insert_date(lst->st_mtime);
@ -1532,32 +1532,6 @@ static void _insert_all(Browser * browser, struct stat * lst, struct stat * st,
}
}
static char const * _insert_size(off_t size)
{
static char buf[11];
double sz = size;
char * unit;
if(sz < 1024)
{
snprintf(buf, sizeof(buf), "%.0f %s", sz, _("bytes"));
return buf;
}
else if((sz /= 1024) < 1024)
unit = N_("kB");
else if((sz /= 1024) < 1024)
unit = N_("MB");
else if((sz /= 1024) < 1024)
unit = N_("GB");
else
{
sz /= 1024;
unit = N_("TB");
}
snprintf(buf, sizeof(buf), "%.1f %s", sz, _(unit));
return buf;
}
static char const * _insert_date(time_t date)
{
static char buf[16];

View File

@ -44,6 +44,10 @@ static int _common_exec(char const * program, char const * flags, GList * args);
static char * _common_get_absolute_path(char const * path);
#endif
#ifdef COMMON_SIZE
static char const * _common_size(off_t size);
#endif
#ifdef COMMON_SYMLINK
static int _common_symlink(GtkWidget * window, char const * cur);
#endif
@ -221,6 +225,36 @@ static char * _common_get_absolute_path(char const * path)
#endif /* COMMON_GET_ABSOLUTE_PATH */
#ifdef COMMON_SIZE
/* common_size */
static char const * _common_size(off_t size)
{
static char buf[16];
double sz = size;
char * unit;
if(sz < 1024)
{
snprintf(buf, sizeof(buf), "%.0f %s", sz, _("bytes"));
return buf;
}
else if((sz /= 1024) < 1024)
unit = N_("kB");
else if((sz /= 1024) < 1024)
unit = N_("MB");
else if((sz /= 1024) < 1024)
unit = N_("GB");
else
{
sz /= 1024;
unit = N_("TB");
}
snprintf(buf, sizeof(buf), "%.1f %s", sz, _(unit));
return buf;
}
#endif
#ifdef COMMON_SYMLINK
/* common_symlink */
static int _common_symlink(GtkWidget * window, char const * cur)