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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* 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 3 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, see
* <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <glib-object.h>
#undef GIMP_DISABLE_DEPRECATED
#include "gimpmemsize.h"
#include "libgimp/libgimp-intl.h"
/**
* SECTION: gimpmemsize
* @title: gimpmemsize
* @short_description: Functions to (de)serialize a given memory size.
*
* Functions to (de)serialize a given memory size.
**/
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)
{
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 g_format_size().
*
* 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 is deprecated! Use g_format_size() instead.
*
* Return value: A newly allocated human-readable, translated string.
**/
gchar *
gimp_memsize_to_string (guint64 memsize)
{
return g_format_size (memsize);
}
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_value_set_uint64 (dest_value, memsize);
}
else
{
g_warning ("Can't convert string to GimpMemsize.");
}
}
/*
* GIMP_TYPE_PARAM_MEMSIZE
*/
static void gimp_param_memsize_class_init (GParamSpecClass *class);
/**
* gimp_param_memsize_get_type:
*
* Reveals the object type
*
* Returns: the #GType for a memsize object
*
* Since: GIMP 2.4
**/
GType
gimp_param_memsize_get_type (void)
{
static GType spec_type = 0;
if (! spec_type)
{
const GTypeInfo type_info =
{
sizeof (GParamSpecClass),
NULL, NULL,
(GClassInitFunc) gimp_param_memsize_class_init,
NULL, NULL,
sizeof (GParamSpecUInt64),
0, NULL, NULL
};
spec_type = g_type_register_static (G_TYPE_PARAM_UINT64,
"GimpParamMemsize",
&type_info, 0);
}
return spec_type;
}
static void
gimp_param_memsize_class_init (GParamSpecClass *class)
{
class->value_type = GIMP_TYPE_MEMSIZE;
}
/**
* gimp_param_spec_memsize:
* @name: Canonical name of the param
* @nick: Nickname of the param
* @blurb: Brief desciption of param.
* @minimum: Smallest allowed value of the parameter.
* @maximum: Largest allowed value of the parameter.
* @default_value: Value to use if none is assigned.
* @flags: a combination of #GParamFlags
*
* Creates a param spec to hold a memory size value.
* See g_param_spec_internal() for more information.
*
* Returns: a newly allocated #GParamSpec instance
*
* Since: GIMP 2.4
**/
GParamSpec *
gimp_param_spec_memsize (const gchar *name,
const gchar *nick,
const gchar *blurb,
guint64 minimum,
guint64 maximum,
guint64 default_value,
GParamFlags flags)
{
GParamSpecUInt64 *pspec;
pspec = g_param_spec_internal (GIMP_TYPE_PARAM_MEMSIZE,
name, nick, blurb, flags);
pspec->minimum = minimum;
pspec->maximum = maximum;
pspec->default_value = default_value;
return G_PARAM_SPEC (pspec);
}
|