2002-02-25 10:52:33 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* tips-parser.c -- Parse the gimp-tips.xml file.
|
|
|
|
* Copyright (C) 2002 Sven Neumann <sven@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 <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2003-09-15 23:24:52 +08:00
|
|
|
#include "config/config-types.h"
|
|
|
|
#include "config/gimpxmlparser.h"
|
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
#include "tips-parser.h"
|
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TIPS_START,
|
|
|
|
TIPS_IN_TIPS,
|
|
|
|
TIPS_IN_TIP,
|
|
|
|
TIPS_IN_WELCOME,
|
|
|
|
TIPS_IN_THETIP,
|
2002-02-27 00:55:09 +08:00
|
|
|
TIPS_IN_UNKNOWN
|
2002-02-25 10:52:33 +08:00
|
|
|
} TipsParserState;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2002-02-26 00:48:30 +08:00
|
|
|
TIPS_LOCALE_NONE,
|
|
|
|
TIPS_LOCALE_MATCH,
|
|
|
|
TIPS_LOCALE_MISMATCH,
|
|
|
|
} TipsParserLocaleState;
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
typedef struct _TipsParser TipsParser;
|
|
|
|
struct _TipsParser
|
|
|
|
{
|
2002-02-26 00:48:30 +08:00
|
|
|
TipsParserState state;
|
|
|
|
TipsParserState last_known_state;
|
|
|
|
const gchar *locale;
|
|
|
|
TipsParserLocaleState locale_state;
|
2002-02-27 00:55:09 +08:00
|
|
|
gint markup_depth;
|
2002-02-26 00:48:30 +08:00
|
|
|
gint unknown_depth;
|
|
|
|
GString *value;
|
|
|
|
|
|
|
|
GimpTip *current_tip;
|
|
|
|
GList *tips;
|
2002-02-25 10:52:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void tips_parser_start_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
const gchar **attribute_names,
|
|
|
|
const gchar **attribute_values,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
|
|
|
static void tips_parser_end_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
|
|
|
static void tips_parser_characters (GMarkupParseContext *context,
|
|
|
|
const gchar *text,
|
|
|
|
gsize text_len,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
|
|
|
|
2002-02-27 00:55:09 +08:00
|
|
|
static void tips_parser_start_markup (TipsParser *parser,
|
|
|
|
const gchar *markup_name);
|
|
|
|
static void tips_parser_end_markup (TipsParser *parser,
|
|
|
|
const gchar *markup_name);
|
2002-02-26 00:48:30 +08:00
|
|
|
static void tips_parser_start_unknown (TipsParser *parser);
|
|
|
|
static void tips_parser_end_unknown (TipsParser *parser);
|
|
|
|
static void tips_parser_parse_locale (TipsParser *parser,
|
|
|
|
const gchar **names,
|
|
|
|
const gchar **values);
|
|
|
|
static void tips_parser_set_by_locale (TipsParser *parser,
|
|
|
|
gchar **dest);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
static const GMarkupParser markup_parser =
|
|
|
|
{
|
|
|
|
tips_parser_start_element,
|
|
|
|
tips_parser_end_element,
|
|
|
|
tips_parser_characters,
|
|
|
|
NULL, /* passthrough */
|
2003-09-15 23:24:52 +08:00
|
|
|
NULL /* error */
|
2002-02-25 10:52:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
GimpTip *
|
|
|
|
gimp_tip_new (const gchar *welcome,
|
|
|
|
const gchar *thetip)
|
|
|
|
{
|
|
|
|
GimpTip *tip = g_new (GimpTip, 1);
|
|
|
|
|
|
|
|
tip->welcome = welcome ? g_strdup (welcome) : NULL;
|
|
|
|
tip->thetip = thetip ? g_strdup (thetip) : NULL;
|
|
|
|
|
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_tip_free (GimpTip *tip)
|
|
|
|
{
|
|
|
|
if (!tip)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (tip->welcome);
|
|
|
|
g_free (tip->thetip);
|
|
|
|
g_free (tip);
|
|
|
|
}
|
|
|
|
|
2002-02-26 09:06:26 +08:00
|
|
|
/**
|
|
|
|
* gimp_tips_from_file:
|
|
|
|
* @filename: the name of the tips file to parse
|
|
|
|
* @error: return location for a #GError
|
2003-09-15 23:24:52 +08:00
|
|
|
*
|
2002-02-26 09:06:26 +08:00
|
|
|
* Reads a gimp-tips XML file, creates a new #GimpTip for
|
|
|
|
* each tip entry and returns a #GList of them. If a parser
|
|
|
|
* error occurs at some point, the uncompleted list is
|
|
|
|
* returned and @error is set (unless @error is %NULL).
|
|
|
|
* The message set in @error contains a detailed description
|
|
|
|
* of the problem.
|
2003-09-15 23:24:52 +08:00
|
|
|
*
|
2002-02-26 09:06:26 +08:00
|
|
|
* Return value: a #Glist of #GimpTips.
|
|
|
|
**/
|
2002-02-25 10:52:33 +08:00
|
|
|
GList *
|
|
|
|
gimp_tips_from_file (const gchar *filename,
|
|
|
|
GError **error)
|
|
|
|
{
|
2003-09-15 23:24:52 +08:00
|
|
|
GimpXmlParser *xml_parser;
|
|
|
|
TipsParser *parser;
|
|
|
|
const gchar *tips_locale;
|
|
|
|
GList *tips = NULL;
|
2002-02-26 05:33:50 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
parser = g_new0 (TipsParser, 1);
|
2003-09-15 23:24:52 +08:00
|
|
|
parser->value = g_string_new (NULL);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
2002-02-26 02:36:03 +08:00
|
|
|
/* This is a special string to specify the language identifier to
|
|
|
|
look for in the gimp-tips.xml file. Please translate the C in it
|
|
|
|
according to the name of the po file used for gimp-tips.xml.
|
|
|
|
E.g. for the german translation, that would be "tips-locale:de".
|
|
|
|
*/
|
|
|
|
tips_locale = _("tips-locale:C");
|
|
|
|
|
|
|
|
if (strncmp (tips_locale, "tips-locale:", 12) == 0)
|
|
|
|
{
|
|
|
|
tips_locale += 12;
|
|
|
|
if (*tips_locale && *tips_locale != 'C')
|
|
|
|
parser->locale = tips_locale;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_warning ("Wrong translation for 'tips-locale:', fix the translation!");
|
|
|
|
|
2003-09-15 23:24:52 +08:00
|
|
|
xml_parser = gimp_xml_parser_new (&markup_parser, parser);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
2003-09-15 23:24:52 +08:00
|
|
|
gimp_xml_parser_parse_file (xml_parser, filename, error);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
2003-09-15 23:24:52 +08:00
|
|
|
gimp_xml_parser_free (xml_parser);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
tips = g_list_reverse (parser->tips);
|
|
|
|
|
|
|
|
gimp_tip_free (parser->current_tip);
|
|
|
|
g_string_free (parser->value, TRUE);
|
|
|
|
g_free (parser);
|
|
|
|
|
|
|
|
return tips;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_tips_free (GList *tips)
|
|
|
|
{
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
for (list = tips; list; list = list->next)
|
|
|
|
gimp_tip_free (list->data);
|
|
|
|
|
|
|
|
g_list_free (tips);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tips_parser_start_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
const gchar **attribute_names,
|
|
|
|
const gchar **attribute_values,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
TipsParser *parser = (TipsParser *) user_data;
|
|
|
|
|
|
|
|
switch (parser->state)
|
|
|
|
{
|
|
|
|
case TIPS_START:
|
|
|
|
if (strcmp (element_name, "gimp-tips") == 0)
|
|
|
|
parser->state = TIPS_IN_TIPS;
|
|
|
|
else
|
|
|
|
tips_parser_start_unknown (parser);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIPS_IN_TIPS:
|
|
|
|
if (strcmp (element_name, "tip") == 0)
|
|
|
|
{
|
|
|
|
parser->state = TIPS_IN_TIP;
|
|
|
|
parser->current_tip = g_new0 (GimpTip, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tips_parser_start_unknown (parser);
|
|
|
|
break;
|
2003-09-15 23:24:52 +08:00
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
case TIPS_IN_TIP:
|
|
|
|
if (strcmp (element_name, "welcome") == 0)
|
|
|
|
{
|
|
|
|
parser->state = TIPS_IN_WELCOME;
|
2002-02-26 00:48:30 +08:00
|
|
|
tips_parser_parse_locale (parser, attribute_names, attribute_values);
|
2002-02-25 10:52:33 +08:00
|
|
|
}
|
|
|
|
else if (strcmp (element_name, "thetip") == 0)
|
|
|
|
{
|
|
|
|
parser->state = TIPS_IN_THETIP;
|
2002-02-26 00:48:30 +08:00
|
|
|
tips_parser_parse_locale (parser, attribute_names, attribute_values);
|
2002-02-25 10:52:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tips_parser_start_unknown (parser);
|
|
|
|
break;
|
2003-09-15 23:24:52 +08:00
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
case TIPS_IN_WELCOME:
|
|
|
|
case TIPS_IN_THETIP:
|
2003-09-15 23:24:52 +08:00
|
|
|
if (strcmp (element_name, "b" ) == 0 ||
|
2002-02-27 00:55:09 +08:00
|
|
|
strcmp (element_name, "big") == 0 ||
|
|
|
|
strcmp (element_name, "tt" ) == 0)
|
|
|
|
tips_parser_start_markup (parser, element_name);
|
|
|
|
else
|
|
|
|
tips_parser_start_unknown (parser);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIPS_IN_UNKNOWN:
|
2002-02-25 10:52:33 +08:00
|
|
|
tips_parser_start_unknown (parser);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tips_parser_end_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
TipsParser *parser = (TipsParser *) user_data;
|
|
|
|
|
|
|
|
switch (parser->state)
|
|
|
|
{
|
|
|
|
case TIPS_START:
|
|
|
|
g_warning ("tips_parser: This shouldn't happen.\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIPS_IN_TIPS:
|
|
|
|
parser->state = TIPS_START;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIPS_IN_TIP:
|
|
|
|
parser->tips = g_list_prepend (parser->tips, parser->current_tip);
|
|
|
|
parser->current_tip = NULL;
|
2002-02-27 00:55:09 +08:00
|
|
|
parser->state = TIPS_IN_TIPS;
|
2002-02-25 10:52:33 +08:00
|
|
|
break;
|
2003-09-15 23:24:52 +08:00
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
case TIPS_IN_WELCOME:
|
2002-02-27 00:55:09 +08:00
|
|
|
if (parser->markup_depth == 0)
|
|
|
|
{
|
|
|
|
tips_parser_set_by_locale (parser, &parser->current_tip->welcome);
|
2003-02-27 04:34:58 +08:00
|
|
|
g_string_truncate (parser->value, 0);
|
2002-02-27 00:55:09 +08:00
|
|
|
parser->state = TIPS_IN_TIP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tips_parser_end_markup (parser, element_name);
|
2002-02-25 10:52:33 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIPS_IN_THETIP:
|
2002-02-27 00:55:09 +08:00
|
|
|
if (parser->markup_depth == 0)
|
|
|
|
{
|
|
|
|
tips_parser_set_by_locale (parser, &parser->current_tip->thetip);
|
2003-02-27 04:34:58 +08:00
|
|
|
g_string_truncate (parser->value, 0);
|
2002-02-27 00:55:09 +08:00
|
|
|
parser->state = TIPS_IN_TIP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tips_parser_end_markup (parser, element_name);
|
2002-02-25 10:52:33 +08:00
|
|
|
break;
|
|
|
|
|
2002-02-27 00:55:09 +08:00
|
|
|
case TIPS_IN_UNKNOWN:
|
2002-02-25 10:52:33 +08:00
|
|
|
tips_parser_end_unknown (parser);
|
2002-02-27 00:55:09 +08:00
|
|
|
break;
|
2002-02-25 10:52:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tips_parser_characters (GMarkupParseContext *context,
|
|
|
|
const gchar *text,
|
|
|
|
gsize text_len,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
TipsParser *parser = (TipsParser *) user_data;
|
|
|
|
|
|
|
|
switch (parser->state)
|
|
|
|
{
|
|
|
|
case TIPS_IN_WELCOME:
|
|
|
|
case TIPS_IN_THETIP:
|
2002-02-26 00:48:30 +08:00
|
|
|
if (parser->locale_state != TIPS_LOCALE_MISMATCH)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
2003-09-15 23:24:52 +08:00
|
|
|
/* strip tabs, newlines and adjacent whitespace */
|
2002-02-26 00:48:30 +08:00
|
|
|
for (i = 0; i < text_len; i++)
|
|
|
|
{
|
2002-02-27 00:55:09 +08:00
|
|
|
if (text[i] != ' ' &&
|
|
|
|
text[i] != '\t' && text[i] != '\n' && text[i] != '\r')
|
2002-02-26 00:48:30 +08:00
|
|
|
g_string_append_c (parser->value, text[i]);
|
|
|
|
else if (parser->value->len > 0 &&
|
|
|
|
parser->value->str[parser->value->len - 1] != ' ')
|
|
|
|
g_string_append_c (parser->value, ' ');
|
|
|
|
}
|
|
|
|
}
|
2002-02-25 10:52:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-27 00:55:09 +08:00
|
|
|
static void
|
|
|
|
tips_parser_start_markup (TipsParser *parser,
|
|
|
|
const gchar *markup_name)
|
|
|
|
{
|
|
|
|
parser->markup_depth++;
|
|
|
|
g_string_append_printf (parser->value, "<%s>", markup_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tips_parser_end_markup (TipsParser *parser,
|
|
|
|
const gchar *markup_name)
|
|
|
|
{
|
|
|
|
g_assert (parser->markup_depth > 0);
|
|
|
|
|
|
|
|
parser->markup_depth--;
|
|
|
|
g_string_append_printf (parser->value, "</%s>", markup_name);
|
|
|
|
}
|
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
static void
|
|
|
|
tips_parser_start_unknown (TipsParser *parser)
|
|
|
|
{
|
|
|
|
if (parser->unknown_depth == 0)
|
|
|
|
parser->last_known_state = parser->state;
|
|
|
|
|
2002-02-27 00:55:09 +08:00
|
|
|
parser->state = TIPS_IN_UNKNOWN;
|
2002-02-25 10:52:33 +08:00
|
|
|
parser->unknown_depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tips_parser_end_unknown (TipsParser *parser)
|
|
|
|
{
|
2002-02-27 00:55:09 +08:00
|
|
|
g_assert (parser->unknown_depth > 0 && parser->state == TIPS_IN_UNKNOWN);
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
parser->unknown_depth--;
|
2003-09-15 23:24:52 +08:00
|
|
|
|
2002-02-25 10:52:33 +08:00
|
|
|
if (parser->unknown_depth == 0)
|
|
|
|
parser->state = parser->last_known_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-26 00:48:30 +08:00
|
|
|
tips_parser_parse_locale (TipsParser *parser,
|
|
|
|
const gchar **names,
|
|
|
|
const gchar **values)
|
2002-02-25 10:52:33 +08:00
|
|
|
{
|
2002-02-26 00:48:30 +08:00
|
|
|
parser->locale_state = TIPS_LOCALE_NONE;
|
2002-02-25 10:52:33 +08:00
|
|
|
|
|
|
|
while (*names && *values)
|
|
|
|
{
|
|
|
|
if (strcmp (*names, "xml:lang") == 0 && **values)
|
|
|
|
{
|
2002-02-26 02:36:03 +08:00
|
|
|
parser->locale_state = (parser->locale &&
|
2003-09-15 23:24:52 +08:00
|
|
|
strcmp (*values, parser->locale) == 0 ?
|
2002-02-26 02:36:03 +08:00
|
|
|
TIPS_LOCALE_MATCH : TIPS_LOCALE_MISMATCH);
|
2002-02-25 10:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
names++;
|
|
|
|
values++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-26 00:48:30 +08:00
|
|
|
tips_parser_set_by_locale (TipsParser *parser,
|
2003-09-15 23:24:52 +08:00
|
|
|
gchar **dest)
|
2002-02-25 10:52:33 +08:00
|
|
|
{
|
2002-02-26 00:48:30 +08:00
|
|
|
switch (parser->locale_state)
|
2002-02-25 10:52:33 +08:00
|
|
|
{
|
2002-02-26 00:48:30 +08:00
|
|
|
case TIPS_LOCALE_NONE:
|
|
|
|
if (!parser->locale)
|
2002-02-25 10:52:33 +08:00
|
|
|
{
|
|
|
|
g_free (*dest);
|
|
|
|
*dest = g_strdup (parser->value->str);
|
|
|
|
}
|
|
|
|
else if (*dest == NULL)
|
|
|
|
{
|
|
|
|
*dest = g_strdup (parser->value->str);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-02-26 00:48:30 +08:00
|
|
|
case TIPS_LOCALE_MATCH:
|
2002-02-25 10:52:33 +08:00
|
|
|
g_free (*dest);
|
|
|
|
*dest = g_strdup (parser->value->str);
|
|
|
|
break;
|
2003-09-15 23:24:52 +08:00
|
|
|
|
2002-02-26 00:48:30 +08:00
|
|
|
case TIPS_LOCALE_MISMATCH:
|
2002-02-25 10:52:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|