2 * AnjutaPluginDescription - Plugin meta data
3 * anjuta-plugin-description.c Copyright (C) 2002 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:anjuta-plugin-description
23 * @title: AnjutaPluginDescription
24 * @short_description: Plugin description from .plugin file
25 * @see_also: #AnjutaPlugin, #AnjutaPluginHandle
26 * @stability: Unstable
27 * @include: libanjuta/anjuta-plugin-description.h
35 #include <libanjuta/anjuta-plugin-description.h>
37 typedef struct _AnjutaPluginDescriptionSection AnjutaPluginDescriptionSection
;
38 typedef struct _AnjutaPluginDescriptionLine AnjutaPluginDescriptionLine
;
39 typedef struct _AnjutaPluginDescriptionParser AnjutaPluginDescriptionParser
;
41 struct _AnjutaPluginDescriptionSection
{
42 GQuark section_name
; /* 0 means just a comment block (before any section) */
44 AnjutaPluginDescriptionLine
*lines
;
47 struct _AnjutaPluginDescriptionLine
{
48 GQuark key
; /* 0 means comment or blank line in value */
53 struct _AnjutaPluginDescription
{
55 AnjutaPluginDescriptionSection
*sections
;
56 char *current_locale
[2];
59 struct _AnjutaPluginDescriptionParser
{
60 AnjutaPluginDescription
*df
;
62 gint n_allocated_lines
;
63 gint n_allocated_sections
;
68 #define VALID_KEY_CHAR 1
69 #define VALID_LOCALE_CHAR 2
71 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
72 0x0 , 0x0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x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
83 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
84 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
85 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
86 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
89 static void report_error (AnjutaPluginDescriptionParser
*parser
,
91 AnjutaPluginDescriptionParseError error_code
,
94 static AnjutaPluginDescriptionSection
*lookup_section (AnjutaPluginDescription
*df
,
97 static AnjutaPluginDescriptionLine
*lookup_line (AnjutaPluginDescription
*df
,
98 AnjutaPluginDescriptionSection
*section
,
103 anjuta_plugin_description_parse_error_quark (void)
107 quark
= g_quark_from_static_string ("g_desktop_parse_error");
113 parser_free (AnjutaPluginDescriptionParser
*parser
)
115 anjuta_plugin_description_free (parser
->df
);
119 anjuta_plugin_description_line_free (AnjutaPluginDescriptionLine
*line
)
121 g_free (line
->locale
);
122 g_free (line
->value
);
126 anjuta_plugin_description_section_free (AnjutaPluginDescriptionSection
*section
)
130 for (i
= 0; i
< section
->n_lines
; i
++)
131 anjuta_plugin_description_line_free (§ion
->lines
[i
]);
133 g_free (section
->lines
);
137 * anjuta_plugin_description_free:
138 * @df: an #AnjutaPluginDescription object
140 * Frees the #AnjutaPluginDescription instance.
143 anjuta_plugin_description_free (AnjutaPluginDescription
*df
)
147 for (i
= 0; i
< df
->n_sections
; i
++)
148 anjuta_plugin_description_section_free (&df
->sections
[i
]);
149 g_free (df
->sections
);
150 g_free (df
->current_locale
[0]);
151 g_free (df
->current_locale
[1]);
157 grow_lines (AnjutaPluginDescriptionParser
*parser
)
160 AnjutaPluginDescriptionSection
*section
;
162 if (parser
->n_allocated_lines
== 0)
165 new_n_lines
= parser
->n_allocated_lines
*2;
167 section
= &parser
->df
->sections
[parser
->current_section
];
169 section
->lines
= g_realloc (section
->lines
,
170 sizeof (AnjutaPluginDescriptionLine
) * new_n_lines
);
171 parser
->n_allocated_lines
= new_n_lines
;
175 grow_sections (AnjutaPluginDescriptionParser
*parser
)
179 if (parser
->n_allocated_sections
== 0)
182 new_n_sections
= parser
->n_allocated_sections
*2;
184 parser
->df
->sections
= g_realloc (parser
->df
->sections
,
185 sizeof (AnjutaPluginDescriptionSection
) * new_n_sections
);
186 parser
->n_allocated_sections
= new_n_sections
;
190 unescape_string (gchar
*str
, gint len
)
196 /* len + 1 is enough, because unescaping never makes the
198 res
= g_new (gchar
, len
+ 1);
207 /* Found an embedded null */
216 /* Escape at end of string */
239 /* Invalid escape code */
254 escape_string (const gchar
*str
, gboolean escape_first_space
)
261 /* len + 1 is enough, because unescaping never makes the
263 res
= g_new (gchar
, strlen (str
)*2 + 1);
267 end
= str
+ strlen (str
);
273 if (escape_first_space
&& p
== str
)
312 open_section (AnjutaPluginDescriptionParser
*parser
,
317 if (parser
->n_allocated_sections
== parser
->df
->n_sections
)
318 grow_sections (parser
);
320 if (parser
->current_section
== 0 &&
321 parser
->df
->sections
[0].section_name
== 0 &&
322 parser
->df
->sections
[0].n_lines
== 0)
325 g_warning ("non-initial NULL section\n");
327 /* The initial section was empty. Piggyback on it. */
328 parser
->df
->sections
[0].section_name
= g_quark_from_string (name
);
333 n
= parser
->df
->n_sections
++;
336 parser
->df
->sections
[n
].section_name
= g_quark_from_string (name
);
338 parser
->df
->sections
[n
].section_name
= 0;
339 parser
->df
->sections
[n
].n_lines
= 0;
340 parser
->df
->sections
[n
].lines
= NULL
;
342 parser
->current_section
= n
;
343 parser
->n_allocated_lines
= 0;
347 static AnjutaPluginDescriptionLine
*
348 new_line (AnjutaPluginDescriptionParser
*parser
)
350 AnjutaPluginDescriptionSection
*section
;
351 AnjutaPluginDescriptionLine
*line
;
353 section
= &parser
->df
->sections
[parser
->current_section
];
355 if (parser
->n_allocated_lines
== section
->n_lines
)
358 line
= §ion
->lines
[section
->n_lines
++];
360 memset (line
, 0, sizeof (AnjutaPluginDescriptionLine
));
366 is_blank_line (AnjutaPluginDescriptionParser
*parser
)
372 while (*p
&& *p
!= '\n')
374 if (!g_ascii_isspace (*p
))
383 parse_comment_or_blank (AnjutaPluginDescriptionParser
*parser
)
385 AnjutaPluginDescriptionLine
*line
;
388 line_end
= strchr (parser
->line
, '\n');
389 if (line_end
== NULL
)
390 line_end
= parser
->line
+ strlen (parser
->line
);
392 line
= new_line (parser
);
394 line
->value
= g_strndup (parser
->line
, line_end
- parser
->line
);
396 parser
->line
= (line_end
) ? line_end
+ 1 : NULL
;
401 parse_section_start (AnjutaPluginDescriptionParser
*parser
, GError
**error
)
406 line_end
= strchr (parser
->line
, '\n');
407 if (line_end
== NULL
)
408 line_end
= parser
->line
+ strlen (parser
->line
);
410 if (line_end
- parser
->line
<= 2 ||
413 report_error (parser
, "Invalid syntax for section header", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX
, error
);
414 parser_free (parser
);
418 section_name
= unescape_string (parser
->line
+ 1, line_end
- parser
->line
- 2);
420 if (section_name
== NULL
)
422 report_error (parser
, "Invalid escaping in section name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES
, error
);
423 parser_free (parser
);
427 open_section (parser
, section_name
);
429 parser
->line
= (line_end
) ? line_end
+ 1 : NULL
;
432 g_free (section_name
);
438 parse_key_value (AnjutaPluginDescriptionParser
*parser
, GError
**error
)
440 AnjutaPluginDescriptionLine
*line
;
445 gchar
*locale_start
= NULL
;
446 gchar
*locale_end
= NULL
;
451 line_end
= strchr (parser
->line
, '\n');
452 if (line_end
== NULL
)
453 line_end
= parser
->line
+ strlen (parser
->line
);
457 while (p
< line_end
&&
458 (valid
[(guchar
)*p
] & VALID_KEY_CHAR
))
462 if (key_start
== key_end
)
464 report_error (parser
, "Empty key name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX
, error
);
465 parser_free (parser
);
469 if (p
< line_end
&& *p
== '[')
473 while (p
< line_end
&& *p
!= ']')
479 report_error (parser
, "Unterminated locale specification in key", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX
, error
);
480 parser_free (parser
);
487 /* Skip space before '=' */
488 while (p
< line_end
&& *p
== ' ')
491 if (p
< line_end
&& *p
!= '=')
493 report_error (parser
, "Invalid characters in key name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_CHARS
, error
);
494 parser_free (parser
);
500 report_error (parser
, "No '=' in key/value pair", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX
, error
);
501 parser_free (parser
);
508 /* Skip space after '=' */
509 while (p
< line_end
&& *p
== ' ')
514 value
= unescape_string (value_start
, line_end
- value_start
);
517 report_error (parser
, "Invalid escaping in value", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES
, error
);
518 parser_free (parser
);
522 line
= new_line (parser
);
523 key
= g_strndup (key_start
, key_end
- key_start
);
524 line
->key
= g_quark_from_string (key
);
527 line
->locale
= g_strndup (locale_start
, locale_end
- locale_start
);
530 parser
->line
= (line_end
) ? line_end
+ 1 : NULL
;
538 report_error (AnjutaPluginDescriptionParser
*parser
,
540 AnjutaPluginDescriptionParseError error_code
,
543 AnjutaPluginDescriptionSection
*section
;
544 const gchar
*section_name
= NULL
;
546 section
= &parser
->df
->sections
[parser
->current_section
];
548 if (section
->section_name
)
549 section_name
= g_quark_to_string (section
->section_name
);
554 *error
= g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR
,
556 "Error in section %s at line %d: %s", section_name
, parser
->line_nr
, message
);
558 *error
= g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR
,
560 "Error at line %d: %s", parser
->line_nr
, message
);
565 * anjuta_plugin_description_new_from_string:
566 * @data: The data to parse. The format of the data is .ini style.
568 * Parses the given plugin description data (usally read from the plugin
569 * description file and creates an instance of #AnjutaPluginDescription.
570 * The format of the content string is similar to .ini format.
572 * Return value: a new #AnjutaPluginDescription object
574 AnjutaPluginDescription
*
575 anjuta_plugin_description_new_from_string (char *data
, GError
**error
)
577 AnjutaPluginDescriptionParser parser
;
579 parser
.df
= g_new0 (AnjutaPluginDescription
, 1);
580 parser
.current_section
= -1;
582 parser
.n_allocated_lines
= 0;
583 parser
.n_allocated_sections
= 0;
588 /* Put any initial comments in a NULL segment */
589 open_section (&parser
, NULL
);
591 while (parser
.line
&& *parser
.line
)
593 if (*parser
.line
== '[') {
594 if (!parse_section_start (&parser
, error
))
596 } else if (is_blank_line (&parser
) ||
598 parse_comment_or_blank (&parser
);
601 if (!parse_key_value (&parser
, error
))
610 * anjuta_plugin_description_to_string:
611 * @df: an #AnjutaPluginDescription object.
613 * Converts the description detains into string format, usually for
614 * saving it in a file.
616 * Return value: The string representation of the description. The
617 * returned values must be freed after use.
620 anjuta_plugin_description_to_string (AnjutaPluginDescription
*df
)
622 AnjutaPluginDescriptionSection
*section
;
623 AnjutaPluginDescriptionLine
*line
;
628 str
= g_string_sized_new (800);
630 for (i
= 0; i
< df
->n_sections
; i
++)
632 section
= &df
->sections
[i
];
634 if (section
->section_name
)
636 g_string_append_c (str
, '[');
637 s
= escape_string (g_quark_to_string (section
->section_name
), FALSE
);
638 g_string_append (str
, s
);
640 g_string_append (str
, "]\n");
643 for (j
= 0; j
< section
->n_lines
; j
++)
645 line
= §ion
->lines
[j
];
649 g_string_append (str
, line
->value
);
650 g_string_append_c (str
, '\n');
654 g_string_append (str
, g_quark_to_string (line
->key
));
657 g_string_append_c (str
, '[');
658 g_string_append (str
, line
->locale
);
659 g_string_append_c (str
, ']');
661 g_string_append_c (str
, '=');
662 s
= escape_string (line
->value
, TRUE
);
663 g_string_append (str
, s
);
665 g_string_append_c (str
, '\n');
670 return g_string_free (str
, FALSE
);
673 static AnjutaPluginDescriptionSection
*
674 lookup_section (AnjutaPluginDescription
*df
,
675 const char *section_name
)
677 AnjutaPluginDescriptionSection
*section
;
678 GQuark section_quark
;
681 section_quark
= g_quark_try_string (section_name
);
682 if (section_quark
== 0)
685 for (i
= 0; i
< df
->n_sections
; i
++)
687 section
= &df
->sections
[i
];
689 if (section
->section_name
== section_quark
)
695 static AnjutaPluginDescriptionLine
*
696 lookup_line (AnjutaPluginDescription
*df
,
697 AnjutaPluginDescriptionSection
*section
,
701 AnjutaPluginDescriptionLine
*line
;
705 key_quark
= g_quark_try_string (keyname
);
709 for (i
= 0; i
< section
->n_lines
; i
++)
711 line
= §ion
->lines
[i
];
713 if (line
->key
== key_quark
&&
714 ((locale
== NULL
&& line
->locale
== NULL
) ||
715 (locale
!= NULL
&& line
->locale
!= NULL
&& strcmp (locale
, line
->locale
) == 0)))
723 * anjuta_plugin_description_get_raw:
724 * @df: an #AnjutaPluginDescription object.
725 * @section_name: Name of the section.
726 * @keyname: Name of the key.
727 * @locale: The locale for which the value is to be retrieved.
728 * @val: Pointer to the variable to store the string value.
730 * Retrieves the value of a key (in the given section) for the given locale.
731 * The value returned in @val must be freed after use.
733 * Return value: TRUE if sucessful, otherwise FALSE.
736 anjuta_plugin_description_get_raw (AnjutaPluginDescription
*df
,
737 const char *section_name
,
742 AnjutaPluginDescriptionSection
*section
;
743 AnjutaPluginDescriptionLine
*line
;
747 section
= lookup_section (df
, section_name
);
751 line
= lookup_line (df
,
759 *val
= g_strdup (line
->value
);
765 * anjuta_plugin_description_foreach_section:
766 * @df: an #AnjutaPluginDescription object.
767 * @func: Callback function.
768 * @user_data: User data to pass to @func.
770 * Calls @func for each of the sections in the description.
773 anjuta_plugin_description_foreach_section (AnjutaPluginDescription
*df
,
774 AnjutaPluginDescriptionSectionFunc func
,
777 AnjutaPluginDescriptionSection
*section
;
780 for (i
= 0; i
< df
->n_sections
; i
++)
782 section
= &df
->sections
[i
];
784 (*func
) (df
, g_quark_to_string (section
->section_name
), user_data
);
790 * anjuta_plugin_description_foreach_key:
791 * @df: an #AnjutaPluginDescription object.
792 * @section_name: Name of the section.
793 * @include_localized: Whether each localized key should be called separately.
794 * @func: The callback function.
795 * @user_data: User data to pass to @func.
797 * Calls @func for each of the keys in the given section. @include_localized,
798 * if set to TRUE will make it call @func for the localized keys also, otherwise
799 * only one call is made for the key in current locale.
802 anjuta_plugin_description_foreach_key (AnjutaPluginDescription
*df
,
803 const char *section_name
,
804 gboolean include_localized
,
805 AnjutaPluginDescriptionLineFunc func
,
808 AnjutaPluginDescriptionSection
*section
;
809 AnjutaPluginDescriptionLine
*line
;
812 section
= lookup_section (df
, section_name
);
816 for (i
= 0; i
< section
->n_lines
; i
++)
818 line
= §ion
->lines
[i
];
820 (*func
) (df
, g_quark_to_string (line
->key
), line
->locale
, line
->value
, user_data
);
828 calculate_locale (AnjutaPluginDescription
*df
)
832 lang
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
836 p
= strchr (lang
, '.');
839 p
= strchr (lang
, '@');
844 lang
= g_strdup ("C");
846 p
= strchr (lang
, '_');
849 df
->current_locale
[0] = g_strdup (lang
);
851 df
->current_locale
[1] = lang
;
855 df
->current_locale
[0] = lang
;
856 df
->current_locale
[1] = NULL
;
861 * anjuta_plugin_description_get_locale_string:
862 * @df: an #AnjutaPluginDescription object.
863 * @section: Section name.
864 * @keyname: Key name.
865 * @val: Pointer to value to store retured value.
867 * Returns the value of key in the given section in current locale.
869 * Return value: TRUE if sucessful, otherwise FALSE.
872 anjuta_plugin_description_get_locale_string (AnjutaPluginDescription
*df
,
879 if (df
->current_locale
[0] == NULL
)
880 calculate_locale (df
);
882 if (df
->current_locale
[0] != NULL
)
884 res
= anjuta_plugin_description_get_raw (df
,section
, keyname
,
885 df
->current_locale
[0], val
);
890 if (df
->current_locale
[1] != NULL
)
892 res
= anjuta_plugin_description_get_raw (df
,section
, keyname
,
893 df
->current_locale
[1], val
);
898 return anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, val
);
902 * anjuta_plugin_description_get_string:
903 * @df: an #AnjutaPluginDescription object.
904 * @section: Section name.
905 * @keyname: Key name.
906 * @val: Pointer to value to store retured value.
908 * Returns the value of key in the given section.
910 * Return value: TRUE if sucessful, otherwise FALSE.
913 anjuta_plugin_description_get_string (AnjutaPluginDescription
*df
,
918 return anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, val
);
922 * anjuta_plugin_description_get_integer:
923 * @df: an #AnjutaPluginDescription object.
924 * @section: Section name.
925 * @keyname: Key name.
926 * @val: Pointer to value to store retured value.
928 * Returns the value of key as integer in the given section in current locale.
930 * Return value: TRUE if sucessful, otherwise FALSE.
933 anjuta_plugin_description_get_integer (AnjutaPluginDescription
*df
,
943 res
= anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, &str
);