1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <errno.h>
#include <glib-object.h>
#include "gimpmemsize.h"
#include "libgimp/libgimp-intl.h"
static void memsize_to_string (const GValue *src_value,
GValue *dest_value);
static void string_to_memsize (const GValue *src_value,
GValue *dest_value);
GType
gimp_memsize_get_type (void)
{
static GType memsize_type = 0;
if (!memsize_type)
{
static const GTypeInfo type_info = { 0, };
memsize_type = g_type_register_static (G_TYPE_UINT64, "GimpMemsize",
&type_info, 0);
g_value_register_transform_func (memsize_type, G_TYPE_STRING,
memsize_to_string);
g_value_register_transform_func (G_TYPE_STRING, memsize_type,
string_to_memsize);
}
return memsize_type;
}
/**
* gimp_memsize_serialize:
* @memsize: memory size in bytes
*
* Creates a string representation of a given memory size. This string
* can be parsed by gimp_memsize_deserialize() and can thus be used in
* config files. It should not be displayed to the user. If you need a
* nice human-readable string please use gimp_memsize_to_string().
*
* Return value: A newly allocated string representation of @memsize.
*
* Since: GIMP 2.2
**/
gchar *
gimp_memsize_serialize (guint64 memsize)
{
if (memsize > (1 << 30) && memsize % (1 << 30) == 0)
return g_strdup_printf ("%" G_GUINT64_FORMAT "G", memsize >> 30);
else if (memsize > (1 << 20) && memsize % (1 << 20) == 0)
return g_strdup_printf ("%" G_GUINT64_FORMAT "M", memsize >> 20);
else if (memsize > (1 << 10) && memsize % (1 << 10) == 0)
return g_strdup_printf ("%" G_GUINT64_FORMAT "k", memsize >> 10);
else
return g_strdup_printf ("%" G_GUINT64_FORMAT, memsize);
}
/**
* gimp_memsize_deserialize:
* @string: a string as returned by gimp_memsize_serialize()
* @memsize: return location for memory size in bytes
*
* Parses a string representation of a memory size as returned by
* gimp_memsize_serialize().
*
* Return value: %TRUE if the @string was successfully parsed and
* @memsize has been set, %FALSE otherwise.
*
* Since: GIMP 2.2
**/
gboolean
gimp_memsize_deserialize (const gchar *string,
guint64 *memsize)
{
gchar *end;
guint64 size;
g_return_val_if_fail (string != NULL, FALSE);
g_return_val_if_fail (memsize != NULL, FALSE);
size = g_ascii_strtoull (string, &end, 0);
if (size == G_MAXUINT64 && errno == ERANGE)
return FALSE;
if (end && *end)
{
guint shift;
switch (g_ascii_tolower (*end))
{
case 'b':
shift = 0;
break;
case 'k':
shift = 10;
break;
case 'm':
shift = 20;
break;
case 'g':
shift = 30;
break;
default:
return FALSE;
}
/* protect against overflow */
if (shift)
{
guint64 limit = G_MAXUINT64 >> shift;
if (size != (size & limit))
return FALSE;
size <<= shift;
}
}
*memsize = size;
return TRUE;
}
/**
* gimp_memsize_to_string:
* @memsize: A memory size in bytes.
*
* This function returns a human readable, translated representation
* of the passed @memsize. Large values are displayed using a
* reasonable memsize unit, e.g.: "345" becomes "345 Bytes", "4500"
* becomes "4.4 KB" and so on.
*
* Return value: A newly allocated human-readable, translated string.
**/
gchar *
gimp_memsize_to_string (guint64 memsize)
{
#if defined _MSC_VER && (_MSC_VER < 1300)
/* sorry, error C2520: conversion from unsigned __int64 to double not
* implemented, use signed __int64
*/
# define CAST_DOUBLE (gdouble)(gint64)
#else
# define CAST_DOUBLE (gdouble)
#endif
if (memsize < 1024)
{
return g_strdup_printf (_("%d Bytes"), (gint) memsize);
}
if (memsize < 1024 * 10)
{
return g_strdup_printf (_("%.2f KB"), CAST_DOUBLE memsize / 1024.0);
}
else if (memsize < 1024 * 100)
{
return g_strdup_printf (_("%.1f KB"), CAST_DOUBLE memsize / 1024.0);
}
else if (memsize < 1024 * 1024)
{
return g_strdup_printf (_("%d KB"), (gint) memsize / 1024);
}
memsize /= 1024;
if (memsize < 1024 * 10)
{
return g_strdup_printf (_("%.2f MB"), CAST_DOUBLE memsize / 1024.0);
}
else if (memsize < 1024 * 100)
{
return g_strdup_printf (_("%.1f MB"), CAST_DOUBLE memsize / 1024.0);
}
else if (memsize < 1024 * 1024)
{
return g_strdup_printf (_("%d MB"), (gint) memsize / 1024);
}
memsize /= 1024;
if (memsize < 1024 * 10)
{
return g_strdup_printf (_("%.2f GB"), CAST_DOUBLE memsize / 1024.0);
}
else if (memsize < 1024 * 100)
{
return g_strdup_printf (_("%.1f GB"), CAST_DOUBLE memsize / 1024.0);
}
else
{
return g_strdup_printf (_("%d GB"), (gint) memsize / 1024);
}
#undef CAST_DOUBLE
}
static void
memsize_to_string (const GValue *src_value,
GValue *dest_value)
{
g_value_take_string (dest_value,
gimp_memsize_serialize (g_value_get_uint64 (src_value)));
}
static void
string_to_memsize (const GValue *src_value,
GValue *dest_value)
{
const gchar *str;
guint64 memsize;
str = g_value_get_string (src_value);
if (! str || ! gimp_memsize_deserialize (str, &memsize))
g_warning ("Can't convert string to GimpMemsize.");
g_value_set_uint64 (dest_value, memsize);
}
|