237 lines
5.3 KiB
C
237 lines
5.3 KiB
C
/* LIBPIKA - The PIKA Library
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
*
|
|
* pikaunitcache.c
|
|
* Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
|
|
*
|
|
* 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
|
|
* <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include "libpikabase/pikabase.h"
|
|
|
|
#include "pikaunitcache.h"
|
|
#include "pikaunit_pdb.h"
|
|
|
|
#include "libpika-intl.h"
|
|
|
|
/* internal structures */
|
|
|
|
typedef struct
|
|
{
|
|
gdouble factor;
|
|
gint digits;
|
|
const gchar *identifier;
|
|
const gchar *symbol;
|
|
const gchar *abbreviation;
|
|
const gchar *singular;
|
|
const gchar *plural;
|
|
} PikaUnitDef;
|
|
|
|
|
|
static PikaUnitDef * pika_unit_defs = NULL;
|
|
static PikaUnit pika_units_initialized = 0;
|
|
|
|
/* not a unit at all but kept here to have the strings in one place
|
|
*/
|
|
static const PikaUnitDef pika_unit_percent =
|
|
{
|
|
0.0, 0, "percent", "%", "%", N_("percent"), N_("percent")
|
|
};
|
|
|
|
|
|
static void pika_unit_def_init (PikaUnitDef *unit_def,
|
|
PikaUnit unit);
|
|
|
|
|
|
static gboolean
|
|
pika_unit_init (PikaUnit unit)
|
|
{
|
|
gint i, n;
|
|
|
|
if (unit < pika_units_initialized)
|
|
return TRUE;
|
|
|
|
n = _pika_unit_get_number_of_units ();
|
|
|
|
if (unit >= n)
|
|
return FALSE;
|
|
|
|
pika_unit_defs = g_renew (PikaUnitDef, pika_unit_defs, n);
|
|
|
|
for (i = pika_units_initialized; i < n; i++)
|
|
{
|
|
pika_unit_def_init (&pika_unit_defs[i], i);
|
|
}
|
|
|
|
pika_units_initialized = n;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static void
|
|
pika_unit_def_init (PikaUnitDef *unit_def,
|
|
PikaUnit unit)
|
|
{
|
|
unit_def->factor = _pika_unit_get_factor (unit);
|
|
unit_def->digits = _pika_unit_get_digits (unit);
|
|
unit_def->identifier = _pika_unit_get_identifier (unit);
|
|
unit_def->symbol = _pika_unit_get_symbol (unit);
|
|
unit_def->abbreviation = _pika_unit_get_abbreviation (unit);
|
|
unit_def->singular = _pika_unit_get_singular (unit);
|
|
unit_def->plural = _pika_unit_get_plural (unit);
|
|
}
|
|
|
|
gint
|
|
_pika_unit_cache_get_number_of_units (void)
|
|
{
|
|
return _pika_unit_get_number_of_units ();
|
|
}
|
|
|
|
gint
|
|
_pika_unit_cache_get_number_of_built_in_units (void)
|
|
{
|
|
return PIKA_UNIT_END;
|
|
}
|
|
|
|
PikaUnit
|
|
_pika_unit_cache_new (gchar *identifier,
|
|
gdouble factor,
|
|
gint digits,
|
|
gchar *symbol,
|
|
gchar *abbreviation,
|
|
gchar *singular,
|
|
gchar *plural)
|
|
{
|
|
return _pika_unit_new (identifier,
|
|
factor,
|
|
digits,
|
|
symbol,
|
|
abbreviation,
|
|
singular,
|
|
plural);
|
|
}
|
|
|
|
gboolean
|
|
_pika_unit_cache_get_deletion_flag (PikaUnit unit)
|
|
{
|
|
if (unit < PIKA_UNIT_END)
|
|
return FALSE;
|
|
|
|
return _pika_unit_get_deletion_flag (unit);
|
|
}
|
|
|
|
void
|
|
_pika_unit_cache_set_deletion_flag (PikaUnit unit,
|
|
gboolean deletion_flag)
|
|
{
|
|
if (unit < PIKA_UNIT_END)
|
|
return;
|
|
|
|
_pika_unit_set_deletion_flag (unit,
|
|
deletion_flag);
|
|
}
|
|
|
|
gdouble
|
|
_pika_unit_cache_get_factor (PikaUnit unit)
|
|
{
|
|
g_return_val_if_fail (unit >= PIKA_UNIT_INCH, 1.0);
|
|
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return pika_unit_percent.factor;
|
|
|
|
if (!pika_unit_init (unit))
|
|
return 1.0;
|
|
|
|
return pika_unit_defs[unit].factor;
|
|
}
|
|
|
|
gint
|
|
_pika_unit_cache_get_digits (PikaUnit unit)
|
|
{
|
|
g_return_val_if_fail (unit >= PIKA_UNIT_INCH, 0);
|
|
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return pika_unit_percent.digits;
|
|
|
|
if (!pika_unit_init (unit))
|
|
return 0;
|
|
|
|
return pika_unit_defs[unit].digits;
|
|
}
|
|
|
|
const gchar *
|
|
_pika_unit_cache_get_identifier (PikaUnit unit)
|
|
{
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return pika_unit_percent.identifier;
|
|
|
|
if (!pika_unit_init (unit))
|
|
return NULL;
|
|
|
|
return pika_unit_defs[unit].identifier;
|
|
}
|
|
|
|
const gchar *
|
|
_pika_unit_cache_get_symbol (PikaUnit unit)
|
|
{
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return pika_unit_percent.symbol;
|
|
|
|
if (!pika_unit_init (unit))
|
|
return NULL;
|
|
|
|
return pika_unit_defs[unit].symbol;
|
|
}
|
|
|
|
const gchar *
|
|
_pika_unit_cache_get_abbreviation (PikaUnit unit)
|
|
{
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return pika_unit_percent.abbreviation;
|
|
|
|
if (!pika_unit_init (unit))
|
|
return NULL;
|
|
|
|
return pika_unit_defs[unit].abbreviation;
|
|
}
|
|
|
|
const gchar *
|
|
_pika_unit_cache_get_singular (PikaUnit unit)
|
|
{
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return gettext (pika_unit_percent.singular);
|
|
|
|
if (!pika_unit_init (unit))
|
|
return NULL;
|
|
|
|
return gettext (pika_unit_defs[unit].singular);
|
|
}
|
|
|
|
const gchar *
|
|
_pika_unit_cache_get_plural (PikaUnit unit)
|
|
{
|
|
if (unit == PIKA_UNIT_PERCENT)
|
|
return gettext (pika_unit_percent.plural);
|
|
|
|
if (!pika_unit_init (unit))
|
|
return NULL;
|
|
|
|
return gettext (pika_unit_defs[unit].plural);
|
|
}
|