grim/gplate

Parents da03ce9fb23f
Children cc54f0826630
added a size1000 and size1024 to GPlateFileVariable that are human readable versions of the file size with base 1000 and base 1024 respectively
--- a/gplate/variables/gplate-file-variable.c Sat Jun 19 20:28:56 2010 -0500
+++ b/gplate/variables/gplate-file-variable.c Sat Jun 19 21:23:50 2010 -0500
@@ -49,6 +49,39 @@
static GObjectClass *parent_class = NULL;
/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static gchar *
+gplate_file_variable_humanize_size(off_t size, gint base) {
+ GString *str = NULL;
+ const gchar suffix[] = { '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
+ register gint b = base, m = 0, r = 0, s = (int)size;
+ gchar *ret = NULL;
+
+ while(s > b) {
+ m++;
+ r = s % b;
+ s /= b;
+ }
+
+ while(r > 10)
+ r = r / 10;
+
+ str = g_string_new("");
+ g_string_append_printf(str, "%d", s);
+
+ if(r > 0)
+ g_string_append_printf(str, ".%d", r);
+
+ g_string_append_c(str, suffix[m]);
+
+ ret = str->str;
+ g_string_free(str, FALSE);
+
+ return ret;
+}
+
+/******************************************************************************
* Private API
*****************************************************************************/
static void
@@ -75,6 +108,7 @@
GPlateCollection *collection = NULL;
GPlateVariable *filename_var = NULL;
const gchar *filename = NULL, *type = NULL;
+ gchar *size = NULL;
struct stat st;
collection = GPLATE_COLLECTION(file_variable);
@@ -128,6 +162,16 @@
type = "unknown";
gplate_collection_add_string(collection, "type", type);
+
+ /* figure out the base 1000 size */
+ size = gplate_file_variable_humanize_size(st.st_size, 1000);
+ gplate_collection_add_string(collection, "size1000", size);
+ g_free(size);
+
+ /* figure out the base 1024 size */
+ size = gplate_file_variable_humanize_size(st.st_size, 1024);
+ gplate_collection_add_string(collection, "size1024", size);
+ g_free(size);
}
/******************************************************************************