From 75c63ee334b64b4f5c6ac2f9059e54f19a51d38f Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Wed, 27 Mar 2002 23:15:00 +0000 Subject: [PATCH] app/config/Makefile.am new files containing a convenience constructor for 2002-03-28 Michael Natterer * app/config/Makefile.am * app/config/gimpscanner.[ch]: new files containing a convenience constructor for GScanner and some parse utility functions, mostly cut out of app/plug-in/plug-in-rc.c * app/plug-in/plug-in-rc.c: removed the stuff here, added scopes to the scanner symbols. * app/core/Makefile.am * app/core/gimpunits.[ch]: moved the user_unit list handling and unitrc stuff to this file. Parse unitrc here, using the new utility functions instead of using gimprc. * app/core/gimpunit.[ch]: removed here. * app/core/gimpdocuments.c: same here: added a scanner for the document history instead of using gimprc. * app/gimprc.c: removed unitrc and document history parsing stuff along with some old unused cruft. * app/app_procs.c * app/core/gimp.c * app/gui/user-install-dialog.c: #include "core/gimpunits.h". * app/core/gimpdrawable-bucket-fill.c: don't include "gimprc.h". --- ChangeLog | 31 ++- app/app_procs.c | 2 +- app/config/Makefile.am | 2 + app/config/gimpscanner.c | 154 +++++++++++++ app/config/gimpscanner.h | 42 ++++ app/core/Makefile.am | 2 + app/core/gimp-documents.c | 74 ++++++- app/core/gimp-units.c | 333 ++++++++++++++++++++++++++++ app/core/gimp-units.h | 30 +++ app/core/gimp.c | 2 +- app/core/gimpdocumentlist.c | 74 ++++++- app/core/gimpdocuments.c | 74 ++++++- app/core/gimpdrawable-bucket-fill.c | 1 - app/core/gimpunit.c | 98 -------- app/core/gimpunit.h | 6 - app/core/gimpunits.c | 333 ++++++++++++++++++++++++++++ app/core/gimpunits.h | 30 +++ app/dialogs/user-install-dialog.c | 2 +- app/gimprc.c | 215 +----------------- app/gui/user-install-dialog.c | 2 +- app/plug-in/plug-in-rc.c | 190 ++++------------ libgimpconfig/gimpscanner.c | 154 +++++++++++++ libgimpconfig/gimpscanner.h | 42 ++++ 23 files changed, 1420 insertions(+), 473 deletions(-) create mode 100644 app/config/gimpscanner.c create mode 100644 app/config/gimpscanner.h create mode 100644 app/core/gimp-units.c create mode 100644 app/core/gimp-units.h create mode 100644 app/core/gimpunits.c create mode 100644 app/core/gimpunits.h create mode 100644 libgimpconfig/gimpscanner.c create mode 100644 libgimpconfig/gimpscanner.h diff --git a/ChangeLog b/ChangeLog index 8ac61ec3db..9428aa885e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,32 @@ +2002-03-28 Michael Natterer + + * app/config/Makefile.am + * app/config/gimpscanner.[ch]: new files containing a convenience + constructor for GScanner and some parse utility functions, mostly + cut out of app/plug-in/plug-in-rc.c + + * app/plug-in/plug-in-rc.c: removed the stuff here, added scopes + to the scanner symbols. + + * app/core/Makefile.am + * app/core/gimpunits.[ch]: moved the user_unit list handling and + unitrc stuff to this file. Parse unitrc here, using the new + utility functions instead of using gimprc. + + * app/core/gimpunit.[ch]: removed here. + + * app/core/gimpdocuments.c: same here: added a scanner for the + document history instead of using gimprc. + + * app/gimprc.c: removed unitrc and document history parsing stuff + along with some old unused cruft. + + * app/app_procs.c + * app/core/gimp.c + * app/gui/user-install-dialog.c: #include "core/gimpunits.h". + + * app/core/gimpdrawable-bucket-fill.c: don't include "gimprc.h". + 2002-03-27 Sven Neumann * autogen.sh: bail out if one of the required tools is not found @@ -29,7 +58,7 @@ * app/widgets/gimppreview.c: gimp_preview_set_viewable: never unset the drag source if the viewable is set to NULL (fixes dock - tabs, thanks to sjburges), also check the passed viweable's type. + tabs, thanks to sjburges), also check the passed viewable's type. * themes/Default/gtkrc: set the paned handle_size to 6 pixels, so it has the same size as the dock_separator. diff --git a/app/app_procs.c b/app/app_procs.c index 96009f5b31..c52ab0df3c 100644 --- a/app/app_procs.c +++ b/app/app_procs.c @@ -40,7 +40,7 @@ #include "core/gimp.h" #include "core/gimpdatafactory.h" -#include "core/gimpunit.h" +#include "core/gimpunits.h" #include "plug-in/plug-ins.h" diff --git a/app/config/Makefile.am b/app/config/Makefile.am index 53d5a3a997..367b8a61e4 100644 --- a/app/config/Makefile.am +++ b/app/config/Makefile.am @@ -28,6 +28,8 @@ libappconfig_a_SOURCES = @STRIP_BEGIN@ \ gimpguiconfig.h \ gimprc.c \ gimprc.h \ + gimpscanner.c \ + gimpscanner.h \ @STRIP_END@ AM_CPPFLAGS = @STRIP_BEGIN@ \ diff --git a/app/config/gimpscanner.c b/app/config/gimpscanner.c new file mode 100644 index 0000000000..6caf5bc9b4 --- /dev/null +++ b/app/config/gimpscanner.c @@ -0,0 +1,154 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpscanner.c + * Copyright (C) 2002 Sven Neumann + * Michael Natterer + * + * 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 +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#include + +#include + +#ifdef G_OS_WIN32 +#include +#endif + +#include "gimpscanner.h" + +#include "libgimp/gimpintl.h" + + +GScanner * +gimp_scanner_new (const gchar *filename) +{ + gint fd; + GScanner *scanner; + + g_return_val_if_fail (filename != NULL, NULL); + + fd = open (filename, O_RDONLY); + + if (fd == -1) + return NULL; + + scanner = g_scanner_new (NULL); + + scanner->config->cset_identifier_first = ( G_CSET_a_2_z ); + scanner->config->cset_identifier_nth = ( G_CSET_a_2_z "-_" ); + + g_scanner_input_file (scanner, fd); + scanner->input_name = g_strdup (filename); + + return scanner; +} + +void +gimp_scanner_destroy (GScanner *scanner) +{ + g_return_if_fail (scanner != NULL); + + close (scanner->input_fd); + g_free ((gchar *) scanner->input_name); + g_scanner_destroy (scanner); +} + + +gboolean +gimp_scanner_parse_token (GScanner *scanner, + GTokenType token) +{ + if (g_scanner_peek_next_token (scanner) != token) + return FALSE; + + g_scanner_get_next_token (scanner); + + return TRUE; +} + +gboolean +gimp_scanner_parse_string (GScanner *scanner, + gchar **dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return FALSE; + + g_scanner_get_next_token (scanner); + + if (*scanner->value.v_string) + { + if (! g_utf8_validate (scanner->value.v_string, -1, NULL)) + { + g_scanner_warn (scanner, _("invalid UTF-8 string")); + return FALSE; + } + + *dest = g_strdup (scanner->value.v_string); + } + + return TRUE; +} + +gboolean +gimp_scanner_parse_string_no_validate (GScanner *scanner, + gchar **dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return FALSE; + + g_scanner_get_next_token (scanner); + + if (*scanner->value.v_string) + *dest = g_strdup (scanner->value.v_string); + + return TRUE; +} + +gboolean +gimp_scanner_parse_int (GScanner *scanner, + gint *dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_INT) + return FALSE; + + g_scanner_get_next_token (scanner); + + *dest = scanner->value.v_int; + + return TRUE; +} + +gboolean +gimp_scanner_parse_float (GScanner *scanner, + gdouble *dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_FLOAT) + return FALSE; + + g_scanner_get_next_token (scanner); + + *dest = scanner->value.v_float; + + return TRUE; +} diff --git a/app/config/gimpscanner.h b/app/config/gimpscanner.h new file mode 100644 index 0000000000..20e98e0fab --- /dev/null +++ b/app/config/gimpscanner.h @@ -0,0 +1,42 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpscanner.h + * Copyright (C) 2002 Sven Neumann + * Michael Natterer + * + * 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. + */ + +#ifndef __GIMP_SCANNER_H__ +#define __GIMP_SCANNER_H__ + + +GScanner * gimp_scanner_new (const gchar *filename); +void gimp_scanner_destroy (GScanner *scanner); + +gboolean gimp_scanner_parse_token (GScanner *scanner, + GTokenType token); +gboolean gimp_scanner_parse_string (GScanner *scanner, + gchar **dest); +gboolean gimp_scanner_parse_string_no_validate (GScanner *scanner, + gchar **dest); +gboolean gimp_scanner_parse_int (GScanner *scanner, + gint *dest); +gboolean gimp_scanner_parse_float (GScanner *scanner, + gdouble *dest); + + +#endif /* __GIMP_SCANNER_H__ */ diff --git a/app/core/Makefile.am b/app/core/Makefile.am index f378e7e7a0..fb8dc3adca 100644 --- a/app/core/Makefile.am +++ b/app/core/Makefile.am @@ -152,6 +152,8 @@ libappcore_a_sources = @STRIP_BEGIN@ \ gimptoolinfo.h \ gimpunit.c \ gimpunit.h \ + gimpunits.c \ + gimpunits.h \ gimpundo.c \ gimpundo.h \ gimpundostack.c \ diff --git a/app/core/gimp-documents.c b/app/core/gimp-documents.c index 7b561acf80..89d98aea96 100644 --- a/app/core/gimp-documents.c +++ b/app/core/gimp-documents.c @@ -29,12 +29,14 @@ #include "core-types.h" +#include "config/gimpscanner.h" + #include "gimp.h" #include "gimpdocuments.h" #include "gimpimagefile.h" #include "gimplist.h" -#include "gimprc.h" +#include "libgimp/gimpintl.h" void @@ -63,13 +65,79 @@ gimp_documents_exit (Gimp *gimp) void gimp_documents_load (Gimp *gimp) { - gchar *filename; + gchar *filename; + GScanner *scanner; + GTokenType token; g_return_if_fail (GIMP_IS_GIMP (gimp)); filename = gimp_personal_rc_file ("documents"); - gimprc_parse_file (filename); + scanner = gimp_scanner_new (filename); g_free (filename); + + if (! scanner) + return; + + g_scanner_scope_add_symbol (scanner, 0, + "document", GINT_TO_POINTER (1)); + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + token = g_scanner_get_next_token (scanner); + + switch (token) + { + case G_TOKEN_LEFT_PAREN: + token = G_TOKEN_SYMBOL; + break; + + case G_TOKEN_SYMBOL: + token = G_TOKEN_RIGHT_PAREN; + if (scanner->value.v_symbol == GINT_TO_POINTER (1)) + { + gchar *uri; + GimpImagefile *imagefile; + + if (! gimp_scanner_parse_string (scanner, &uri)) + { + token = G_TOKEN_STRING; + break; + } + + imagefile = gimp_imagefile_new (uri); + + g_free (uri); + + GIMP_LIST (gimp->documents)->list = + g_list_append (GIMP_LIST (gimp->documents)->list, imagefile); + + gimp->documents->num_children++; + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: /* do nothing */ + break; + } + } + while (token != G_TOKEN_EOF); + + if (token != G_TOKEN_LEFT_PAREN) + { + g_scanner_get_next_token (scanner); + g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, + _("fatal parse error"), TRUE); + } + + gimp_scanner_destroy (scanner); } static void diff --git a/app/core/gimp-units.c b/app/core/gimp-units.c new file mode 100644 index 0000000000..82d6a94c3f --- /dev/null +++ b/app/core/gimp-units.c @@ -0,0 +1,333 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis + * + * gimpunit.c + * Copyright (C) 1999-2000 Michael Natterer + * + * 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 + +#include + +#include "libgimpbase/gimpbase.h" + +#include "core-types.h" + +#include "gimp.h" +#include "gimpunit.h" +#include "gimpunits.h" + +#include "config/gimpscanner.h" + +#include "libgimp/gimpintl.h" + + +/* + * 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 != NULL); + 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 != NULL); + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + if (gimp->user_units) + { + g_list_foreach (gimp->user_units, (GFunc) g_free, NULL); + g_list_free (gimp->user_units); + + gimp->user_units = NULL; + gimp->n_user_units = 0; + } +} + + +/* 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; + + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + filename = gimp_personal_rc_file ("unitrc"); + scanner = gimp_scanner_new (filename); + g_free (filename); + + if (! scanner) + return; + + g_scanner_scope_add_symbol (scanner, 0, + "unit-info", GINT_TO_POINTER (UNIT_INFO)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "factor", GINT_TO_POINTER (UNIT_FACTOR)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "digits", GINT_TO_POINTER (UNIT_DIGITS)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "symbol", GINT_TO_POINTER (UNIT_SYMBOL)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "abbreviation", GINT_TO_POINTER (UNIT_ABBREV)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "singular", GINT_TO_POINTER (UNIT_SINGULAR)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "plural", GINT_TO_POINTER (UNIT_PLURAL)); + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + 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); + g_scanner_set_scope (scanner, 0); + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: /* do nothing */ + break; + } + } + while (token != G_TOKEN_EOF); + + if (token != G_TOKEN_LEFT_PAREN) + { + g_scanner_get_next_token (scanner); + g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, + _("fatal parse error"), TRUE); + } + + gimp_scanner_destroy (scanner); +} + +void +gimp_unitrc_save (Gimp *gimp) +{ + gint i; + gchar *filename; + FILE *fp; + + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + filename = gimp_personal_rc_file ("unitrc"); + + fp = fopen (filename, "w"); + g_free (filename); + if (!fp) + return; + + fprintf (fp, + "# GIMP unitrc\n" + "# This file contains your user unit database. You can\n" + "# modify this list with the unit editor. You are not\n" + "# supposed to edit it manually, but of course you can do.\n" + "# This file will be entirely rewritten every time you\n" + "# quit the gimp.\n\n"); + + /* save user defined units */ + for (i = gimp_unit_get_number_of_built_in_units (); + i < gimp_unit_get_number_of_units (); + i++) + { + if (gimp_unit_get_deletion_flag (i) == FALSE) + { + fprintf (fp, + "(unit-info \"%s\"\n" + " (factor %f)\n" + " (digits %d)\n" + " (symbol \"%s\")\n" + " (abbreviation \"%s\")\n" + " (singular \"%s\")\n" + " (plural \"%s\"))\n\n", + gimp_unit_get_identifier (i), + gimp_unit_get_factor (i), + gimp_unit_get_digits (i), + gimp_unit_get_symbol (i), + gimp_unit_get_abbreviation (i), + gimp_unit_get_singular (i), + gimp_unit_get_plural (i)); + } + } + + fclose (fp); +} + + +/* 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; + GimpUnit unit; + + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return G_TOKEN_STRING; + + if (! gimp_scanner_parse_string (scanner, &identifier)) + return G_TOKEN_STRING; + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + token = g_scanner_get_next_token (scanner); + + switch (token) + { + case G_TOKEN_LEFT_PAREN: + token = G_TOKEN_SYMBOL; + break; + + case G_TOKEN_SYMBOL: + token = G_TOKEN_RIGHT_PAREN; + switch (GPOINTER_TO_INT (scanner->value.v_symbol)) + { + case UNIT_FACTOR: + if (! gimp_scanner_parse_float (scanner, &factor)) + return G_TOKEN_FLOAT; + break; + + case UNIT_DIGITS: + if (! gimp_scanner_parse_int (scanner, &digits)) + return G_TOKEN_INT; + break; + + case UNIT_SYMBOL: + if (! gimp_scanner_parse_string (scanner, &symbol)) + return G_TOKEN_STRING; + break; + + case UNIT_ABBREV: + if (! gimp_scanner_parse_string (scanner, &abbreviation)) + return G_TOKEN_STRING; + break; + + case UNIT_SINGULAR: + if (! gimp_scanner_parse_string (scanner, &singular)) + return G_TOKEN_STRING; + break; + + case UNIT_PLURAL: + if (! gimp_scanner_parse_string (scanner, &plural)) + return G_TOKEN_STRING; + break; + + default: + break; + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: + break; + } + } + while (token != G_TOKEN_EOF); + + if (token == G_TOKEN_LEFT_PAREN) + { + token = G_TOKEN_RIGHT_PAREN; + + if (gimp_scanner_parse_token (scanner, token)) + { + unit = _gimp_unit_new (gimp, identifier, factor, digits, + symbol, abbreviation, singular, plural); + + /* make the unit definition persistent */ + gimp_unit_set_deletion_flag (unit, FALSE); + + g_free (identifier); + g_free (symbol); + g_free (abbreviation); + g_free (singular); + g_free (plural); + + return G_TOKEN_LEFT_PAREN; + } + } + + g_free (identifier); + g_free (symbol); + g_free (abbreviation); + g_free (singular); + g_free (plural); + + return token; +} diff --git a/app/core/gimp-units.h b/app/core/gimp-units.h new file mode 100644 index 0000000000..ee2149a8ba --- /dev/null +++ b/app/core/gimp-units.h @@ -0,0 +1,30 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * 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. + */ + +#ifndef __GIMP_UNITS_H__ +#define __GIMP_UNITS_H__ + + +void gimp_units_init (Gimp *gimp); +void gimp_units_exit (Gimp *gimp); + +void gimp_unitrc_load (Gimp *gimp); +void gimp_unitrc_save (Gimp *gimp); + + +#endif /* __GIMP_UNITS_H__ */ diff --git a/app/core/gimp.c b/app/core/gimp.c index ed65a85330..3cdeb0905a 100644 --- a/app/core/gimp.c +++ b/app/core/gimp.c @@ -52,7 +52,7 @@ #include "gimppattern.h" #include "gimpparasite.h" #include "gimptoolinfo.h" -#include "gimpunit.h" +#include "gimpunits.h" #include "libgimp/gimpintl.h" diff --git a/app/core/gimpdocumentlist.c b/app/core/gimpdocumentlist.c index 7b561acf80..89d98aea96 100644 --- a/app/core/gimpdocumentlist.c +++ b/app/core/gimpdocumentlist.c @@ -29,12 +29,14 @@ #include "core-types.h" +#include "config/gimpscanner.h" + #include "gimp.h" #include "gimpdocuments.h" #include "gimpimagefile.h" #include "gimplist.h" -#include "gimprc.h" +#include "libgimp/gimpintl.h" void @@ -63,13 +65,79 @@ gimp_documents_exit (Gimp *gimp) void gimp_documents_load (Gimp *gimp) { - gchar *filename; + gchar *filename; + GScanner *scanner; + GTokenType token; g_return_if_fail (GIMP_IS_GIMP (gimp)); filename = gimp_personal_rc_file ("documents"); - gimprc_parse_file (filename); + scanner = gimp_scanner_new (filename); g_free (filename); + + if (! scanner) + return; + + g_scanner_scope_add_symbol (scanner, 0, + "document", GINT_TO_POINTER (1)); + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + token = g_scanner_get_next_token (scanner); + + switch (token) + { + case G_TOKEN_LEFT_PAREN: + token = G_TOKEN_SYMBOL; + break; + + case G_TOKEN_SYMBOL: + token = G_TOKEN_RIGHT_PAREN; + if (scanner->value.v_symbol == GINT_TO_POINTER (1)) + { + gchar *uri; + GimpImagefile *imagefile; + + if (! gimp_scanner_parse_string (scanner, &uri)) + { + token = G_TOKEN_STRING; + break; + } + + imagefile = gimp_imagefile_new (uri); + + g_free (uri); + + GIMP_LIST (gimp->documents)->list = + g_list_append (GIMP_LIST (gimp->documents)->list, imagefile); + + gimp->documents->num_children++; + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: /* do nothing */ + break; + } + } + while (token != G_TOKEN_EOF); + + if (token != G_TOKEN_LEFT_PAREN) + { + g_scanner_get_next_token (scanner); + g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, + _("fatal parse error"), TRUE); + } + + gimp_scanner_destroy (scanner); } static void diff --git a/app/core/gimpdocuments.c b/app/core/gimpdocuments.c index 7b561acf80..89d98aea96 100644 --- a/app/core/gimpdocuments.c +++ b/app/core/gimpdocuments.c @@ -29,12 +29,14 @@ #include "core-types.h" +#include "config/gimpscanner.h" + #include "gimp.h" #include "gimpdocuments.h" #include "gimpimagefile.h" #include "gimplist.h" -#include "gimprc.h" +#include "libgimp/gimpintl.h" void @@ -63,13 +65,79 @@ gimp_documents_exit (Gimp *gimp) void gimp_documents_load (Gimp *gimp) { - gchar *filename; + gchar *filename; + GScanner *scanner; + GTokenType token; g_return_if_fail (GIMP_IS_GIMP (gimp)); filename = gimp_personal_rc_file ("documents"); - gimprc_parse_file (filename); + scanner = gimp_scanner_new (filename); g_free (filename); + + if (! scanner) + return; + + g_scanner_scope_add_symbol (scanner, 0, + "document", GINT_TO_POINTER (1)); + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + token = g_scanner_get_next_token (scanner); + + switch (token) + { + case G_TOKEN_LEFT_PAREN: + token = G_TOKEN_SYMBOL; + break; + + case G_TOKEN_SYMBOL: + token = G_TOKEN_RIGHT_PAREN; + if (scanner->value.v_symbol == GINT_TO_POINTER (1)) + { + gchar *uri; + GimpImagefile *imagefile; + + if (! gimp_scanner_parse_string (scanner, &uri)) + { + token = G_TOKEN_STRING; + break; + } + + imagefile = gimp_imagefile_new (uri); + + g_free (uri); + + GIMP_LIST (gimp->documents)->list = + g_list_append (GIMP_LIST (gimp->documents)->list, imagefile); + + gimp->documents->num_children++; + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: /* do nothing */ + break; + } + } + while (token != G_TOKEN_EOF); + + if (token != G_TOKEN_LEFT_PAREN) + { + g_scanner_get_next_token (scanner); + g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, + _("fatal parse error"), TRUE); + } + + gimp_scanner_destroy (scanner); } static void diff --git a/app/core/gimpdrawable-bucket-fill.c b/app/core/gimpdrawable-bucket-fill.c index e52ee93456..630263f078 100644 --- a/app/core/gimpdrawable-bucket-fill.c +++ b/app/core/gimpdrawable-bucket-fill.c @@ -40,7 +40,6 @@ #include "gimpimage-mask.h" #include "gimppattern.h" -#include "gimprc.h" #include "undo.h" #include "libgimp/gimpintl.h" diff --git a/app/core/gimpunit.c b/app/core/gimpunit.c index f71e2f0d2c..e438cfc299 100644 --- a/app/core/gimpunit.c +++ b/app/core/gimpunit.c @@ -21,9 +21,6 @@ #include "config.h" -#include -#include - #include #include "libgimpbase/gimpbase.h" @@ -33,8 +30,6 @@ #include "gimp.h" #include "gimpunit.h" -#include "gimprc.h" - #include "libgimp/gimpintl.h" @@ -271,96 +266,3 @@ _gimp_unit_get_plural (Gimp *gimp, return gettext (_gimp_unit_get_user_unit (gimp, unit)->plural); } - - -/* unitrc functions **********/ - -void -gimp_units_init (Gimp *gimp) -{ - g_return_if_fail (gimp != NULL); - 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 != NULL); - g_return_if_fail (GIMP_IS_GIMP (gimp)); - - if (gimp->user_units) - { - g_list_foreach (gimp->user_units, (GFunc) g_free, NULL); - g_list_free (gimp->user_units); - - gimp->user_units = NULL; - gimp->n_user_units = 0; - } -} - -void -gimp_unitrc_load (Gimp *gimp) -{ - gchar *filename; - - g_return_if_fail (gimp != NULL); - g_return_if_fail (GIMP_IS_GIMP (gimp)); - - filename = gimp_personal_rc_file ("unitrc"); - gimprc_parse_file (filename); - g_free (filename); -} - -void -gimp_unitrc_save (Gimp *gimp) -{ - gint i; - gchar *filename; - FILE *fp; - - g_return_if_fail (gimp != NULL); - g_return_if_fail (GIMP_IS_GIMP (gimp)); - - filename = gimp_personal_rc_file ("unitrc"); - - fp = fopen (filename, "w"); - g_free (filename); - if (!fp) - return; - - fprintf (fp, - "# GIMP unitrc\n" - "# This file contains your user unit database. You can\n" - "# modify this list with the unit editor. You are not\n" - "# supposed to edit it manually, but of course you can do.\n" - "# This file will be entirely rewritten every time you\n" - "# quit the gimp.\n\n"); - - /* save user defined units */ - for (i = gimp_unit_get_number_of_built_in_units (); - i < gimp_unit_get_number_of_units (); - i++) - if (gimp_unit_get_deletion_flag (i) == FALSE) - { - fprintf (fp, - "(unit-info \"%s\"\n" - " (factor %f)\n" - " (digits %d)\n" - " (symbol \"%s\")\n" - " (abbreviation \"%s\")\n" - " (singular \"%s\")\n" - " (plural \"%s\"))\n\n", - gimp_unit_get_identifier (i), - gimp_unit_get_factor (i), - gimp_unit_get_digits (i), - gimp_unit_get_symbol (i), - gimp_unit_get_abbreviation (i), - gimp_unit_get_singular (i), - gimp_unit_get_plural (i)); - } - - fclose (fp); -} diff --git a/app/core/gimpunit.h b/app/core/gimpunit.h index 2bec25bdc0..464d2c47ee 100644 --- a/app/core/gimpunit.h +++ b/app/core/gimpunit.h @@ -20,12 +20,6 @@ #define __APP_GIMP_UNIT_H__ -void gimp_units_init (Gimp *gimp); -void gimp_units_exit (Gimp *gimp); - -void gimp_unitrc_load (Gimp *gimp); -void gimp_unitrc_save (Gimp *gimp); - gint _gimp_unit_get_number_of_units (Gimp *gimp); gint _gimp_unit_get_number_of_built_in_units (Gimp *gimp) G_GNUC_CONST; diff --git a/app/core/gimpunits.c b/app/core/gimpunits.c new file mode 100644 index 0000000000..82d6a94c3f --- /dev/null +++ b/app/core/gimpunits.c @@ -0,0 +1,333 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis + * + * gimpunit.c + * Copyright (C) 1999-2000 Michael Natterer + * + * 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 + +#include + +#include "libgimpbase/gimpbase.h" + +#include "core-types.h" + +#include "gimp.h" +#include "gimpunit.h" +#include "gimpunits.h" + +#include "config/gimpscanner.h" + +#include "libgimp/gimpintl.h" + + +/* + * 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 != NULL); + 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 != NULL); + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + if (gimp->user_units) + { + g_list_foreach (gimp->user_units, (GFunc) g_free, NULL); + g_list_free (gimp->user_units); + + gimp->user_units = NULL; + gimp->n_user_units = 0; + } +} + + +/* 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; + + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + filename = gimp_personal_rc_file ("unitrc"); + scanner = gimp_scanner_new (filename); + g_free (filename); + + if (! scanner) + return; + + g_scanner_scope_add_symbol (scanner, 0, + "unit-info", GINT_TO_POINTER (UNIT_INFO)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "factor", GINT_TO_POINTER (UNIT_FACTOR)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "digits", GINT_TO_POINTER (UNIT_DIGITS)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "symbol", GINT_TO_POINTER (UNIT_SYMBOL)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "abbreviation", GINT_TO_POINTER (UNIT_ABBREV)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "singular", GINT_TO_POINTER (UNIT_SINGULAR)); + g_scanner_scope_add_symbol (scanner, UNIT_INFO, + "plural", GINT_TO_POINTER (UNIT_PLURAL)); + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + 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); + g_scanner_set_scope (scanner, 0); + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: /* do nothing */ + break; + } + } + while (token != G_TOKEN_EOF); + + if (token != G_TOKEN_LEFT_PAREN) + { + g_scanner_get_next_token (scanner); + g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, + _("fatal parse error"), TRUE); + } + + gimp_scanner_destroy (scanner); +} + +void +gimp_unitrc_save (Gimp *gimp) +{ + gint i; + gchar *filename; + FILE *fp; + + g_return_if_fail (GIMP_IS_GIMP (gimp)); + + filename = gimp_personal_rc_file ("unitrc"); + + fp = fopen (filename, "w"); + g_free (filename); + if (!fp) + return; + + fprintf (fp, + "# GIMP unitrc\n" + "# This file contains your user unit database. You can\n" + "# modify this list with the unit editor. You are not\n" + "# supposed to edit it manually, but of course you can do.\n" + "# This file will be entirely rewritten every time you\n" + "# quit the gimp.\n\n"); + + /* save user defined units */ + for (i = gimp_unit_get_number_of_built_in_units (); + i < gimp_unit_get_number_of_units (); + i++) + { + if (gimp_unit_get_deletion_flag (i) == FALSE) + { + fprintf (fp, + "(unit-info \"%s\"\n" + " (factor %f)\n" + " (digits %d)\n" + " (symbol \"%s\")\n" + " (abbreviation \"%s\")\n" + " (singular \"%s\")\n" + " (plural \"%s\"))\n\n", + gimp_unit_get_identifier (i), + gimp_unit_get_factor (i), + gimp_unit_get_digits (i), + gimp_unit_get_symbol (i), + gimp_unit_get_abbreviation (i), + gimp_unit_get_singular (i), + gimp_unit_get_plural (i)); + } + } + + fclose (fp); +} + + +/* 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; + GimpUnit unit; + + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return G_TOKEN_STRING; + + if (! gimp_scanner_parse_string (scanner, &identifier)) + return G_TOKEN_STRING; + + token = G_TOKEN_LEFT_PAREN; + + do + { + if (g_scanner_peek_next_token (scanner) != token) + break; + + token = g_scanner_get_next_token (scanner); + + switch (token) + { + case G_TOKEN_LEFT_PAREN: + token = G_TOKEN_SYMBOL; + break; + + case G_TOKEN_SYMBOL: + token = G_TOKEN_RIGHT_PAREN; + switch (GPOINTER_TO_INT (scanner->value.v_symbol)) + { + case UNIT_FACTOR: + if (! gimp_scanner_parse_float (scanner, &factor)) + return G_TOKEN_FLOAT; + break; + + case UNIT_DIGITS: + if (! gimp_scanner_parse_int (scanner, &digits)) + return G_TOKEN_INT; + break; + + case UNIT_SYMBOL: + if (! gimp_scanner_parse_string (scanner, &symbol)) + return G_TOKEN_STRING; + break; + + case UNIT_ABBREV: + if (! gimp_scanner_parse_string (scanner, &abbreviation)) + return G_TOKEN_STRING; + break; + + case UNIT_SINGULAR: + if (! gimp_scanner_parse_string (scanner, &singular)) + return G_TOKEN_STRING; + break; + + case UNIT_PLURAL: + if (! gimp_scanner_parse_string (scanner, &plural)) + return G_TOKEN_STRING; + break; + + default: + break; + } + break; + + case G_TOKEN_RIGHT_PAREN: + token = G_TOKEN_LEFT_PAREN; + break; + + default: + break; + } + } + while (token != G_TOKEN_EOF); + + if (token == G_TOKEN_LEFT_PAREN) + { + token = G_TOKEN_RIGHT_PAREN; + + if (gimp_scanner_parse_token (scanner, token)) + { + unit = _gimp_unit_new (gimp, identifier, factor, digits, + symbol, abbreviation, singular, plural); + + /* make the unit definition persistent */ + gimp_unit_set_deletion_flag (unit, FALSE); + + g_free (identifier); + g_free (symbol); + g_free (abbreviation); + g_free (singular); + g_free (plural); + + return G_TOKEN_LEFT_PAREN; + } + } + + g_free (identifier); + g_free (symbol); + g_free (abbreviation); + g_free (singular); + g_free (plural); + + return token; +} diff --git a/app/core/gimpunits.h b/app/core/gimpunits.h new file mode 100644 index 0000000000..ee2149a8ba --- /dev/null +++ b/app/core/gimpunits.h @@ -0,0 +1,30 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * 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. + */ + +#ifndef __GIMP_UNITS_H__ +#define __GIMP_UNITS_H__ + + +void gimp_units_init (Gimp *gimp); +void gimp_units_exit (Gimp *gimp); + +void gimp_unitrc_load (Gimp *gimp); +void gimp_unitrc_save (Gimp *gimp); + + +#endif /* __GIMP_UNITS_H__ */ diff --git a/app/dialogs/user-install-dialog.c b/app/dialogs/user-install-dialog.c index 946cbb34be..9ffed70194 100644 --- a/app/dialogs/user-install-dialog.c +++ b/app/dialogs/user-install-dialog.c @@ -38,7 +38,7 @@ #include "base/base-config.h" -#include "core/gimpunit.h" +#include "core/gimpunits.h" #include "gui.h" #include "resolution-calibrate-dialog.h" diff --git a/app/gimprc.c b/app/gimprc.c index d35c8fe36d..706221d3ee 100644 --- a/app/gimprc.c +++ b/app/gimprc.c @@ -42,8 +42,6 @@ #include "core/gimp.h" #include "core/gimpcoreconfig.h" -#include "core/gimpimagefile.h" -#include "core/gimplist.h" #include "core/gimpparasite.h" #include "core/gimptoolinfo.h" @@ -87,17 +85,14 @@ typedef enum TT_INTERP, TT_XPREVSIZE, TT_XUNIT, - TT_XMENUPATH, TT_XDEVICE, TT_XSESSIONINFO, TT_XCOLORHISTORY, - TT_XUNITINFO, TT_XPARASITE, TT_XNAVPREVSIZE, TT_XHELPBROWSER, TT_XCURSORMODE, - TT_XCOMMENT, - TT_XDOCUMENT + TT_XCOMMENT } TokenType; @@ -138,14 +133,11 @@ static gint parse_preview_size (gpointer val1p, gpointer val2p) static gint parse_nav_preview_size (gpointer val1p, gpointer val2p); static gint parse_units (gpointer val1p, gpointer val2p); static gint parse_device (gpointer val1p, gpointer val2p); -static gint parse_menu_path (gpointer val1p, gpointer val2p); static gint parse_session_info (gpointer val1p, gpointer val2p); -static gint parse_unit_info (gpointer val1p, gpointer val2p); static gint parse_parasite (gpointer val1p, gpointer val2p); static gint parse_help_browser (gpointer val1p, gpointer val2p); static gint parse_cursor_mode (gpointer val1p, gpointer val2p); static gint parse_color_history (gpointer val1p, gpointer val2p); -static gint parse_document (gpointer val1p, gpointer val2p); static gint parse_color (GimpRGB *color); static gint parse_unknown (gchar *token_sym); @@ -289,12 +281,9 @@ static ParseFunc funcs[] = { "theme", TT_STRING, &gimprc.theme, NULL }, { "parasite", TT_XPARASITE, NULL, NULL }, - { "menu-path", TT_XMENUPATH, NULL, NULL }, { "device", TT_XDEVICE, NULL, NULL }, { "session-info", TT_XSESSIONINFO, NULL, NULL }, - { "color-history", TT_XCOLORHISTORY, NULL, NULL }, - { "unit-info", TT_XUNITINFO, NULL, NULL }, - { "document", TT_XDOCUMENT, NULL, NULL } + { "color-history", TT_XCOLORHISTORY, NULL, NULL } }; @@ -907,16 +896,12 @@ parse_statement (void) return parse_nav_preview_size (func->val1p, func->val2p); case TT_XUNIT: return parse_units (func->val1p, func->val2p); - case TT_XMENUPATH: - return parse_menu_path (func->val1p, func->val2p); case TT_XDEVICE: return parse_device (func->val1p, func->val2p); case TT_XSESSIONINFO: return parse_session_info (func->val1p, func->val2p); case TT_XCOLORHISTORY: return parse_color_history (func->val1p, func->val2p); - case TT_XUNITINFO: - return parse_unit_info (func->val1p, func->val2p); case TT_XPARASITE: return parse_parasite (func->val1p, func->val2p); case TT_XHELPBROWSER: @@ -925,8 +910,6 @@ parse_statement (void) return parse_cursor_mode (func->val1p, func->val2p); case TT_XCOMMENT: return parse_string (func->val1p, func->val2p); - case TT_XDOCUMENT: - return parse_document (func->val1p, func->val2p); } } @@ -1468,42 +1451,6 @@ parse_color (GimpRGB *color) return OK; } -static gint -parse_menu_path (gpointer val1p, - gpointer val2p) -{ - gchar *menu_path = NULL; - gchar *accelerator = NULL; - gint token; - - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - - menu_path = g_strdup (token_str); - - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - - accelerator = g_strdup (token_str); - - token = peek_next_token (); - if (!token || (token != TOKEN_RIGHT_PAREN)) - goto error; - token = get_next_token (); - - return OK; - - error: - g_free (menu_path); - g_free (accelerator); - - return ERROR; -} - static gchar * transform_path (gchar *path, gboolean destroy) @@ -2123,124 +2070,6 @@ parse_color_history (gpointer val1p, return OK; } -static gint -parse_unit_info (gpointer val1p, - gpointer val2p) -{ - gint token; - GimpUnit unit; - - gchar *identifier = NULL; - gdouble factor = 1.0; - gint digits = 2.0; - gchar *symbol = NULL; - gchar *abbreviation = NULL; - gchar *singular = NULL; - gchar *plural = NULL; - - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - return ERROR; - token = get_next_token (); - - identifier = g_strdup (token_str); - - /* Parse options for unit info */ - - while (peek_next_token () == TOKEN_LEFT_PAREN) - { - token = get_next_token (); - - token = peek_next_token (); - if (!token || (token != TOKEN_SYMBOL)) - goto error; - token = get_next_token (); - - if (!strcmp ("factor", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_NUMBER)) - goto error; - token = get_next_token (); - factor = token_num; - } - else if (!strcmp ("digits", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_NUMBER)) - goto error; - token = get_next_token (); - digits = token_int; - } - else if (!strcmp ("symbol", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - symbol = g_strdup (token_str); - } - else if (!strcmp ("abbreviation", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - abbreviation = g_strdup (token_str); - } - else if (!strcmp ("singular", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - singular = g_strdup (token_str); - } - else if (!strcmp ("plural", token_sym)) - { - token = peek_next_token (); - if (!token || (token != TOKEN_STRING)) - goto error; - token = get_next_token (); - plural = g_strdup (token_str); - } - else - goto error; - - token = peek_next_token (); - if (!token || (token != TOKEN_RIGHT_PAREN)) - goto error; - token = get_next_token (); - } - - if (!token || (token != TOKEN_RIGHT_PAREN)) - goto error; - token = get_next_token (); - - unit = gimp_unit_new (identifier, factor, digits, - symbol, abbreviation, singular, plural); - - /* make the unit definition persistent */ - gimp_unit_set_deletion_flag (unit, FALSE); - - g_free (identifier); - g_free (symbol); - g_free (abbreviation); - g_free (singular); - g_free (plural); - - return OK; - - error: - g_free (identifier); - g_free (symbol); - g_free (abbreviation); - g_free (singular); - g_free (plural); - - return ERROR; -} - static gint parse_parasite (gpointer val1p, gpointer val2p) @@ -2333,43 +2162,6 @@ parse_cursor_mode (gpointer val1p, return OK; } -static gint -parse_document (gpointer val1p, - gpointer val2p) -{ - gint token = 0; - gchar *filename = NULL; - GimpImagefile *imagefile; - - token = peek_next_token (); - if (!token || token != TOKEN_STRING) - goto error; - - token = get_next_token (); - filename = g_strdup (token_str); - - token = peek_next_token (); - if (!token || (token != TOKEN_RIGHT_PAREN)) - goto error; - token = get_next_token (); - - imagefile = gimp_imagefile_new (filename); - - g_free (filename); - - GIMP_LIST (the_gimp->documents)->list = - g_list_append (GIMP_LIST (the_gimp->documents)->list, imagefile); - - the_gimp->documents->num_children++; - - return OK; - - error: - g_free (filename); - - return ERROR; -} - static gint parse_unknown (gchar *token_sym) { @@ -2466,13 +2258,10 @@ gimprc_value_to_str (const gchar *name) return cursor_mode_to_str (func->val1p, func->val2p); case TT_XCOMMENT: return comment_to_str (func->val1p, func->val2p); - case TT_XMENUPATH: case TT_XDEVICE: case TT_XSESSIONINFO: case TT_XCOLORHISTORY: - case TT_XUNITINFO: case TT_XPARASITE: - case TT_XDOCUMENT: return NULL; } } diff --git a/app/gui/user-install-dialog.c b/app/gui/user-install-dialog.c index 946cbb34be..9ffed70194 100644 --- a/app/gui/user-install-dialog.c +++ b/app/gui/user-install-dialog.c @@ -38,7 +38,7 @@ #include "base/base-config.h" -#include "core/gimpunit.h" +#include "core/gimpunits.h" #include "gui.h" #include "resolution-calibrate-dialog.h" diff --git a/app/plug-in/plug-in-rc.c b/app/plug-in/plug-in-rc.c index 0154550a8b..2387ffb2ac 100644 --- a/app/plug-in/plug-in-rc.c +++ b/app/plug-in/plug-in-rc.c @@ -21,22 +21,14 @@ #include "config.h" -#include -#include #include -#ifdef HAVE_UNISTD_H -#include -#endif -#include #include -#ifdef G_OS_WIN32 -#include -#endif - #include "plug-in-types.h" +#include "config/gimpscanner.h" + #include "plug-in.h" #include "plug-ins.h" #include "plug-in-def.h" @@ -47,7 +39,7 @@ /* - * All functions return G_TOKEN_LEFT_PAREN on success, + * All deserialize functions return G_TOKEN_LEFT_PAREN on success, * or the GTokenType they would have expected but didn't get. */ @@ -63,19 +55,10 @@ static GTokenType plug_in_help_def_deserialize (GScanner *scanner, static GTokenType plug_in_has_init_deserialize (GScanner *scanner, PlugInDef *plug_in_def); -static inline gboolean parse_token (GScanner *scanner, - GTokenType token); -static inline gboolean parse_string (GScanner *scanner, - gchar **dest); -static inline gboolean parse_string_no_validate (GScanner *scanner, - gchar **dest); -static inline gboolean parse_int (GScanner *scanner, - gint *dest); - enum { - PLUG_IN_DEF, + PLUG_IN_DEF = 1, PROC_DEF, LOCALE_DEF, HELP_DEF, @@ -87,36 +70,27 @@ enum gboolean plug_in_rc_parse (const gchar *filename) { - gint fd; GScanner *scanner; GTokenType token; g_return_val_if_fail (filename != NULL, FALSE); - fd = open (filename, O_RDONLY); + scanner = gimp_scanner_new (filename); - if (fd == -1) + if (! scanner) return TRUE; - scanner = g_scanner_new (NULL); - - scanner->config->cset_identifier_first = ( G_CSET_a_2_z ); - scanner->config->cset_identifier_nth = ( G_CSET_a_2_z "-_" ); - - g_scanner_input_file (scanner, fd); - scanner->input_name = filename; - - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, 0, "plug-in-def", GINT_TO_POINTER (PLUG_IN_DEF)); - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "proc-def", GINT_TO_POINTER (PROC_DEF)); - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "locale-def", GINT_TO_POINTER (LOCALE_DEF)); - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "help-def", GINT_TO_POINTER (HELP_DEF)); - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "has-init", GINT_TO_POINTER (HAS_INIT)); - g_scanner_scope_add_symbol (scanner, 0, + g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "proc-arg", GINT_TO_POINTER (PROC_ARG)); token = G_TOKEN_LEFT_PAREN; @@ -135,8 +109,12 @@ plug_in_rc_parse (const gchar *filename) break; case G_TOKEN_SYMBOL: - if (scanner->value.v_symbol == PLUG_IN_DEF) - token = plug_in_def_deserialize (scanner); + if (scanner->value.v_symbol == GINT_TO_POINTER (PLUG_IN_DEF)) + { + g_scanner_set_scope (scanner, PLUG_IN_DEF); + token = plug_in_def_deserialize (scanner); + g_scanner_set_scope (scanner, 0); + } break; case G_TOKEN_RIGHT_PAREN: @@ -156,8 +134,7 @@ plug_in_rc_parse (const gchar *filename) _("fatal parse error"), TRUE); } - g_scanner_destroy (scanner); - close (fd); + gimp_scanner_destroy (scanner); return (token != G_TOKEN_EOF); } @@ -170,13 +147,13 @@ plug_in_def_deserialize (GScanner *scanner) PlugInProcDef *proc_def; GTokenType token; - if (!parse_string (scanner, &name)) + if (! gimp_scanner_parse_string (scanner, &name)) return G_TOKEN_STRING; plug_in_def = plug_in_def_new (name); g_free (name); - if (!parse_int (scanner, (gint *) &plug_in_def->mtime)) + if (! gimp_scanner_parse_int (scanner, (gint *) &plug_in_def->mtime)) { plug_in_def_free (plug_in_def, TRUE); return G_TOKEN_INT; @@ -245,7 +222,7 @@ plug_in_def_deserialize (GScanner *scanner) { token = G_TOKEN_RIGHT_PAREN; - if (parse_token (scanner, token)) + if (gimp_scanner_parse_token (scanner, token)) { plug_ins_def_add (plug_in_def); return G_TOKEN_LEFT_PAREN; @@ -264,37 +241,37 @@ plug_in_proc_def_deserialize (GScanner *scanner, GTokenType token; gint i; - if (!parse_string (scanner, &proc_def->db_info.name)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.name)) return G_TOKEN_STRING; - if (!parse_int (scanner, (gint *) &proc_def->db_info.proc_type)) + if (! gimp_scanner_parse_int (scanner, (gint *) &proc_def->db_info.proc_type)) return G_TOKEN_INT; - if (!parse_string (scanner, &proc_def->db_info.blurb)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.blurb)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->db_info.help)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.help)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->db_info.author)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.author)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->db_info.copyright)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.copyright)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->db_info.date)) + if (! gimp_scanner_parse_string (scanner, &proc_def->db_info.date)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->menu_path)) + if (! gimp_scanner_parse_string (scanner, &proc_def->menu_path)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->extensions)) + if (! gimp_scanner_parse_string (scanner, &proc_def->extensions)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->prefixes)) + if (! gimp_scanner_parse_string (scanner, &proc_def->prefixes)) return G_TOKEN_STRING; - if (!parse_string_no_validate (scanner, &proc_def->magics)) + if (! gimp_scanner_parse_string_no_validate (scanner, &proc_def->magics)) return G_TOKEN_STRING; - if (!parse_string (scanner, &proc_def->image_types)) + if (! gimp_scanner_parse_string (scanner, &proc_def->image_types)) return G_TOKEN_STRING; proc_def->image_types_val = plug_ins_image_types_parse (proc_def->image_types); - if (!parse_int (scanner, (gint *) &proc_def->db_info.num_args)) + if (! gimp_scanner_parse_int (scanner, (gint *) &proc_def->db_info.num_args)) return G_TOKEN_INT; - if (!parse_int (scanner, (gint *) &proc_def->db_info.num_values)) + if (! gimp_scanner_parse_int (scanner, (gint *) &proc_def->db_info.num_values)) return G_TOKEN_INT; if (proc_def->db_info.num_args > 0) @@ -319,7 +296,7 @@ plug_in_proc_def_deserialize (GScanner *scanner, return token; } - if (!parse_token (scanner, G_TOKEN_RIGHT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; @@ -329,21 +306,21 @@ static GTokenType plug_in_proc_arg_deserialize (GScanner *scanner, ProcArg *arg) { - if (!parse_token (scanner, G_TOKEN_LEFT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_LEFT_PAREN)) return G_TOKEN_LEFT_PAREN; - if (!parse_token (scanner, G_TOKEN_SYMBOL) || + if (! gimp_scanner_parse_token (scanner, G_TOKEN_SYMBOL) || GPOINTER_TO_INT (scanner->value.v_symbol) != PROC_ARG) return G_TOKEN_SYMBOL; - if (!parse_int (scanner, (gint *) &arg->arg_type)) + if (! gimp_scanner_parse_int (scanner, (gint *) &arg->arg_type)) return G_TOKEN_INT; - if (!parse_string (scanner, &arg->name)) + if (! gimp_scanner_parse_string (scanner, &arg->name)) return G_TOKEN_STRING; - if (!parse_string (scanner, &arg->description)) + if (! gimp_scanner_parse_string (scanner, &arg->description)) return G_TOKEN_STRING; - if (!parse_token (scanner, G_TOKEN_RIGHT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; @@ -353,12 +330,12 @@ static GTokenType plug_in_locale_def_deserialize (GScanner *scanner, PlugInDef *plug_in_def) { - if (!parse_string (scanner, &plug_in_def->locale_domain)) + if (! gimp_scanner_parse_string (scanner, &plug_in_def->locale_domain)) return G_TOKEN_STRING; - parse_string (scanner, &plug_in_def->locale_path); + gimp_scanner_parse_string (scanner, &plug_in_def->locale_path); - if (!parse_token (scanner, G_TOKEN_RIGHT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; @@ -368,10 +345,10 @@ static GTokenType plug_in_help_def_deserialize (GScanner *scanner, PlugInDef *plug_in_def) { - if (!parse_string (scanner, &plug_in_def->help_path)) + if (! gimp_scanner_parse_string (scanner, &plug_in_def->help_path)) return G_TOKEN_STRING; - if (!parse_token (scanner, G_TOKEN_RIGHT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; @@ -383,81 +360,13 @@ plug_in_has_init_deserialize (GScanner *scanner, { plug_in_def->has_init = TRUE; - if (!parse_token (scanner, G_TOKEN_RIGHT_PAREN)) + if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; } -/* helper functions */ - - -static inline gboolean -parse_token (GScanner *scanner, - GTokenType token) -{ - if (g_scanner_peek_next_token (scanner) != token) - return FALSE; - - g_scanner_get_next_token (scanner); - - return TRUE; -} - -static inline gboolean -parse_string (GScanner *scanner, - gchar **dest) -{ - if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) - return FALSE; - - g_scanner_get_next_token (scanner); - - if (*scanner->value.v_string) - { - if (!g_utf8_validate (scanner->value.v_string, -1, NULL)) - { - g_scanner_warn (scanner, _("invalid UTF-8 string")); - return FALSE; - } - - *dest = g_strdup (scanner->value.v_string); - } - - return TRUE; -} - -static inline gboolean -parse_string_no_validate (GScanner *scanner, - gchar **dest) -{ - if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) - return FALSE; - - g_scanner_get_next_token (scanner); - - if (*scanner->value.v_string) - *dest = g_strdup (scanner->value.v_string); - - return TRUE; -} - -static inline gboolean -parse_int (GScanner *scanner, - gint *dest) -{ - if (g_scanner_peek_next_token (scanner) != G_TOKEN_INT) - return FALSE; - - g_scanner_get_next_token (scanner); - - *dest = scanner->value.v_int; - - return TRUE; -} - - /* serialize functions */ static void @@ -618,4 +527,3 @@ plug_in_rc_write (GSList *plug_in_defs, return TRUE; } - diff --git a/libgimpconfig/gimpscanner.c b/libgimpconfig/gimpscanner.c new file mode 100644 index 0000000000..6caf5bc9b4 --- /dev/null +++ b/libgimpconfig/gimpscanner.c @@ -0,0 +1,154 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpscanner.c + * Copyright (C) 2002 Sven Neumann + * Michael Natterer + * + * 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 +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#include + +#include + +#ifdef G_OS_WIN32 +#include +#endif + +#include "gimpscanner.h" + +#include "libgimp/gimpintl.h" + + +GScanner * +gimp_scanner_new (const gchar *filename) +{ + gint fd; + GScanner *scanner; + + g_return_val_if_fail (filename != NULL, NULL); + + fd = open (filename, O_RDONLY); + + if (fd == -1) + return NULL; + + scanner = g_scanner_new (NULL); + + scanner->config->cset_identifier_first = ( G_CSET_a_2_z ); + scanner->config->cset_identifier_nth = ( G_CSET_a_2_z "-_" ); + + g_scanner_input_file (scanner, fd); + scanner->input_name = g_strdup (filename); + + return scanner; +} + +void +gimp_scanner_destroy (GScanner *scanner) +{ + g_return_if_fail (scanner != NULL); + + close (scanner->input_fd); + g_free ((gchar *) scanner->input_name); + g_scanner_destroy (scanner); +} + + +gboolean +gimp_scanner_parse_token (GScanner *scanner, + GTokenType token) +{ + if (g_scanner_peek_next_token (scanner) != token) + return FALSE; + + g_scanner_get_next_token (scanner); + + return TRUE; +} + +gboolean +gimp_scanner_parse_string (GScanner *scanner, + gchar **dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return FALSE; + + g_scanner_get_next_token (scanner); + + if (*scanner->value.v_string) + { + if (! g_utf8_validate (scanner->value.v_string, -1, NULL)) + { + g_scanner_warn (scanner, _("invalid UTF-8 string")); + return FALSE; + } + + *dest = g_strdup (scanner->value.v_string); + } + + return TRUE; +} + +gboolean +gimp_scanner_parse_string_no_validate (GScanner *scanner, + gchar **dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_STRING) + return FALSE; + + g_scanner_get_next_token (scanner); + + if (*scanner->value.v_string) + *dest = g_strdup (scanner->value.v_string); + + return TRUE; +} + +gboolean +gimp_scanner_parse_int (GScanner *scanner, + gint *dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_INT) + return FALSE; + + g_scanner_get_next_token (scanner); + + *dest = scanner->value.v_int; + + return TRUE; +} + +gboolean +gimp_scanner_parse_float (GScanner *scanner, + gdouble *dest) +{ + if (g_scanner_peek_next_token (scanner) != G_TOKEN_FLOAT) + return FALSE; + + g_scanner_get_next_token (scanner); + + *dest = scanner->value.v_float; + + return TRUE; +} diff --git a/libgimpconfig/gimpscanner.h b/libgimpconfig/gimpscanner.h new file mode 100644 index 0000000000..20e98e0fab --- /dev/null +++ b/libgimpconfig/gimpscanner.h @@ -0,0 +1,42 @@ +/* The GIMP -- an image manipulation program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpscanner.h + * Copyright (C) 2002 Sven Neumann + * Michael Natterer + * + * 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. + */ + +#ifndef __GIMP_SCANNER_H__ +#define __GIMP_SCANNER_H__ + + +GScanner * gimp_scanner_new (const gchar *filename); +void gimp_scanner_destroy (GScanner *scanner); + +gboolean gimp_scanner_parse_token (GScanner *scanner, + GTokenType token); +gboolean gimp_scanner_parse_string (GScanner *scanner, + gchar **dest); +gboolean gimp_scanner_parse_string_no_validate (GScanner *scanner, + gchar **dest); +gboolean gimp_scanner_parse_int (GScanner *scanner, + gint *dest); +gboolean gimp_scanner_parse_float (GScanner *scanner, + gdouble *dest); + + +#endif /* __GIMP_SCANNER_H__ */