2002-03-28 07:15:00 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpunit.c
|
|
|
|
* Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
|
|
|
#include "gimp.h"
|
2003-09-16 01:26:28 +08:00
|
|
|
#include "gimp-units.h"
|
2002-03-28 07:15:00 +08:00
|
|
|
#include "gimpunit.h"
|
|
|
|
|
2003-11-05 05:58:39 +08:00
|
|
|
#include "config/gimpconfig-error.h"
|
2005-01-25 06:41:24 +08:00
|
|
|
#include "config/gimpconfig-file.h"
|
2003-03-10 23:48:16 +08:00
|
|
|
#include "config/gimpconfigwriter.h"
|
2002-03-28 07:15:00 +08:00
|
|
|
#include "config/gimpscanner.h"
|
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
|
2003-09-16 01:26:28 +08:00
|
|
|
/*
|
2002-03-28 07:15:00 +08:00
|
|
|
* All deserialize functions return G_TOKEN_LEFT_PAREN on success,
|
|
|
|
* or the GTokenType they would have expected but didn't get.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static GTokenType gimp_unitrc_unit_info_deserialize (GScanner *scanner,
|
|
|
|
Gimp *gimp);
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_units_init (Gimp *gimp)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
|
|
|
gimp->user_units = NULL;
|
|
|
|
gimp->n_user_units = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_units_exit (Gimp *gimp)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
2005-01-13 21:08:00 +08:00
|
|
|
gimp_user_units_free (gimp);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* unitrc functions **********/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
UNIT_INFO = 1,
|
|
|
|
UNIT_FACTOR,
|
|
|
|
UNIT_DIGITS,
|
|
|
|
UNIT_SYMBOL,
|
|
|
|
UNIT_ABBREV,
|
|
|
|
UNIT_SINGULAR,
|
|
|
|
UNIT_PLURAL
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_unitrc_load (Gimp *gimp)
|
|
|
|
{
|
|
|
|
gchar *filename;
|
|
|
|
GScanner *scanner;
|
|
|
|
GTokenType token;
|
2002-05-31 21:58:18 +08:00
|
|
|
GError *error = NULL;
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
|
|
|
filename = gimp_personal_rc_file ("unitrc");
|
2003-06-24 06:02:56 +08:00
|
|
|
scanner = gimp_scanner_new_file (filename, &error);
|
2003-11-05 05:58:39 +08:00
|
|
|
|
|
|
|
if (! scanner && error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT)
|
|
|
|
{
|
|
|
|
g_clear_error (&error);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
filename = g_build_filename (gimp_sysconf_directory (), "unitrc", NULL);
|
|
|
|
scanner = gimp_scanner_new_file (filename, NULL);
|
|
|
|
}
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
if (! scanner)
|
2003-11-05 05:58:39 +08:00
|
|
|
{
|
|
|
|
g_clear_error (&error);
|
|
|
|
g_free (filename);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-28 07:15:00 +08:00
|
|
|
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, 0,
|
2002-03-28 07:15:00 +08:00
|
|
|
"unit-info", GINT_TO_POINTER (UNIT_INFO));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"factor", GINT_TO_POINTER (UNIT_FACTOR));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"digits", GINT_TO_POINTER (UNIT_DIGITS));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"symbol", GINT_TO_POINTER (UNIT_SYMBOL));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"abbreviation", GINT_TO_POINTER (UNIT_ABBREV));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"singular", GINT_TO_POINTER (UNIT_SINGULAR));
|
2003-10-02 01:32:14 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, UNIT_INFO,
|
2002-03-28 07:15:00 +08:00
|
|
|
"plural", GINT_TO_POINTER (UNIT_PLURAL));
|
|
|
|
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
while (g_scanner_peek_next_token (scanner) == token)
|
2002-03-28 07:15:00 +08:00
|
|
|
{
|
|
|
|
token = g_scanner_get_next_token (scanner);
|
|
|
|
|
|
|
|
switch (token)
|
|
|
|
{
|
|
|
|
case G_TOKEN_LEFT_PAREN:
|
|
|
|
token = G_TOKEN_SYMBOL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_SYMBOL:
|
|
|
|
if (scanner->value.v_symbol == GINT_TO_POINTER (UNIT_INFO))
|
|
|
|
{
|
|
|
|
g_scanner_set_scope (scanner, UNIT_INFO);
|
|
|
|
token = gimp_unitrc_unit_info_deserialize (scanner, gimp);
|
2002-05-27 22:04:21 +08:00
|
|
|
|
|
|
|
if (token == G_TOKEN_RIGHT_PAREN)
|
2002-05-15 21:48:47 +08:00
|
|
|
g_scanner_set_scope (scanner, 0);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_RIGHT_PAREN:
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token != G_TOKEN_LEFT_PAREN)
|
|
|
|
{
|
|
|
|
g_scanner_get_next_token (scanner);
|
2002-06-18 03:36:33 +08:00
|
|
|
g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
|
2002-03-28 07:15:00 +08:00
|
|
|
_("fatal parse error"), TRUE);
|
2002-05-31 21:58:18 +08:00
|
|
|
|
|
|
|
g_message (error->message);
|
|
|
|
g_clear_error (&error);
|
2003-11-05 05:58:39 +08:00
|
|
|
|
|
|
|
gimp_config_file_backup_on_error (filename, "unitrc", NULL);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gimp_scanner_destroy (scanner);
|
2003-11-05 05:58:39 +08:00
|
|
|
g_free (filename);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_unitrc_save (Gimp *gimp)
|
|
|
|
{
|
2003-03-10 23:48:16 +08:00
|
|
|
GimpConfigWriter *writer;
|
|
|
|
gchar *filename;
|
|
|
|
gint i;
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
|
|
|
filename = gimp_personal_rc_file ("unitrc");
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
writer =
|
|
|
|
gimp_config_writer_new_file (filename,
|
|
|
|
TRUE,
|
|
|
|
"GIMP units\n\n"
|
|
|
|
"This file contains the user unit database. "
|
|
|
|
"You can edit this list with the unit "
|
|
|
|
"editor. You are not supposed to edit it "
|
|
|
|
"manually, but of course you can do.\n"
|
|
|
|
"This file will be entirely rewritten every "
|
|
|
|
"time you quit the gimp.",
|
|
|
|
NULL);
|
2003-03-10 23:48:16 +08:00
|
|
|
|
2002-03-28 07:15:00 +08:00
|
|
|
g_free (filename);
|
|
|
|
|
2003-03-10 23:48:16 +08:00
|
|
|
if (!writer)
|
|
|
|
return;
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
/* save user defined units */
|
2003-10-02 01:32:14 +08:00
|
|
|
for (i = _gimp_unit_get_number_of_built_in_units (gimp);
|
|
|
|
i < _gimp_unit_get_number_of_units (gimp);
|
2002-03-28 07:15:00 +08:00
|
|
|
i++)
|
|
|
|
{
|
2003-10-02 01:32:14 +08:00
|
|
|
if (_gimp_unit_get_deletion_flag (gimp, i) == FALSE)
|
2002-03-28 07:15:00 +08:00
|
|
|
{
|
2002-04-15 05:26:42 +08:00
|
|
|
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_open (writer, "unit-info");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_string (writer,
|
|
|
|
_gimp_unit_get_identifier (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
|
|
|
|
gimp_config_writer_open (writer, "factor");
|
|
|
|
gimp_config_writer_print (writer,
|
|
|
|
g_ascii_formatd (buf, sizeof (buf), "%f",
|
2003-10-02 01:32:14 +08:00
|
|
|
_gimp_unit_get_factor (gimp, i)),
|
2003-03-10 23:48:16 +08:00
|
|
|
-1);
|
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
|
|
|
|
gimp_config_writer_open (writer, "digits");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_printf (writer,
|
|
|
|
"%d", _gimp_unit_get_digits (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
|
|
|
|
gimp_config_writer_open (writer, "symbol");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_string (writer,
|
|
|
|
_gimp_unit_get_symbol (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_close (writer);
|
2003-10-02 01:32:14 +08:00
|
|
|
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_open (writer, "abbreviation");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_string (writer,
|
|
|
|
_gimp_unit_get_abbreviation (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
|
|
|
|
gimp_config_writer_open (writer, "singular");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_string (writer,
|
|
|
|
_gimp_unit_get_singular (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_close (writer);
|
2003-10-02 01:32:14 +08:00
|
|
|
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_open (writer, "plural");
|
2003-10-02 01:32:14 +08:00
|
|
|
gimp_config_writer_string (writer,
|
|
|
|
_gimp_unit_get_plural (gimp, i));
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
|
|
|
|
gimp_config_writer_close (writer);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-10 23:48:16 +08:00
|
|
|
gimp_config_writer_finish (writer, "end of units", NULL);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
|
|
|
static GTokenType
|
|
|
|
gimp_unitrc_unit_info_deserialize (GScanner *scanner,
|
|
|
|
Gimp *gimp)
|
|
|
|
{
|
|
|
|
gchar *identifier = NULL;
|
|
|
|
gdouble factor = 1.0;
|
|
|
|
gint digits = 2.0;
|
|
|
|
gchar *symbol = NULL;
|
|
|
|
gchar *abbreviation = NULL;
|
|
|
|
gchar *singular = NULL;
|
|
|
|
gchar *plural = NULL;
|
|
|
|
GTokenType token;
|
|
|
|
|
|
|
|
if (! gimp_scanner_parse_string (scanner, &identifier))
|
|
|
|
return G_TOKEN_STRING;
|
|
|
|
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
while (g_scanner_peek_next_token (scanner) == token)
|
2002-03-28 07:15:00 +08:00
|
|
|
{
|
|
|
|
token = g_scanner_get_next_token (scanner);
|
|
|
|
|
|
|
|
switch (token)
|
|
|
|
{
|
|
|
|
case G_TOKEN_LEFT_PAREN:
|
|
|
|
token = G_TOKEN_SYMBOL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_SYMBOL:
|
|
|
|
switch (GPOINTER_TO_INT (scanner->value.v_symbol))
|
|
|
|
{
|
|
|
|
case UNIT_FACTOR:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_FLOAT;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_float (scanner, &factor))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT_DIGITS:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_INT;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_int (scanner, &digits))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT_SYMBOL:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_STRING;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_string (scanner, &symbol))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT_ABBREV:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_STRING;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_string (scanner, &abbreviation))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT_SINGULAR:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_STRING;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_string (scanner, &singular))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT_PLURAL:
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_STRING;
|
2002-03-28 07:15:00 +08:00
|
|
|
if (! gimp_scanner_parse_string (scanner, &plural))
|
2002-05-27 22:04:21 +08:00
|
|
|
goto cleanup;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2002-05-27 22:04:21 +08:00
|
|
|
token = G_TOKEN_RIGHT_PAREN;
|
2002-03-28 07:15:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_RIGHT_PAREN:
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token == G_TOKEN_LEFT_PAREN)
|
|
|
|
{
|
|
|
|
token = G_TOKEN_RIGHT_PAREN;
|
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
if (g_scanner_peek_next_token (scanner) == token)
|
2002-03-28 07:15:00 +08:00
|
|
|
{
|
2005-01-13 21:08:00 +08:00
|
|
|
GimpUnit unit = _gimp_unit_new (gimp,
|
|
|
|
identifier, factor, digits,
|
|
|
|
symbol, abbreviation,
|
|
|
|
singular, plural);
|
2002-03-28 07:15:00 +08:00
|
|
|
|
|
|
|
/* make the unit definition persistent */
|
2003-10-02 01:32:14 +08:00
|
|
|
_gimp_unit_set_deletion_flag (gimp, unit, FALSE);
|
2002-03-28 07:15:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
cleanup:
|
|
|
|
|
2002-03-28 07:15:00 +08:00
|
|
|
g_free (identifier);
|
|
|
|
g_free (symbol);
|
|
|
|
g_free (abbreviation);
|
|
|
|
g_free (singular);
|
|
|
|
g_free (plural);
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|