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
);
590 while (parser
.line
!= NULL
&& strlen(parser
.line
))
592 if (*parser
.line
== '[') {
593 if (!parse_section_start (&parser
, error
))
595 } else if (is_blank_line (&parser
) ||
597 parse_comment_or_blank (&parser
);
600 if (!parse_key_value (&parser
, error
))
609 * anjuta_plugin_description_to_string:
610 * @df: an #AnjutaPluginDescription object.
612 * Converts the description detains into string format, usually for
613 * saving it in a file.
615 * Return value: The string representation of the description. The
616 * returned values must be freed after use.
619 anjuta_plugin_description_to_string (AnjutaPluginDescription
*df
)
621 AnjutaPluginDescriptionSection
*section
;
622 AnjutaPluginDescriptionLine
*line
;
627 str
= g_string_sized_new (800);
629 for (i
= 0; i
< df
->n_sections
; i
++)
631 section
= &df
->sections
[i
];
633 if (section
->section_name
)
635 g_string_append_c (str
, '[');
636 s
= escape_string (g_quark_to_string (section
->section_name
), FALSE
);
637 g_string_append (str
, s
);
639 g_string_append (str
, "]\n");
642 for (j
= 0; j
< section
->n_lines
; j
++)
644 line
= §ion
->lines
[j
];
648 g_string_append (str
, line
->value
);
649 g_string_append_c (str
, '\n');
653 g_string_append (str
, g_quark_to_string (line
->key
));
656 g_string_append_c (str
, '[');
657 g_string_append (str
, line
->locale
);
658 g_string_append_c (str
, ']');
660 g_string_append_c (str
, '=');
661 s
= escape_string (line
->value
, TRUE
);
662 g_string_append (str
, s
);
664 g_string_append_c (str
, '\n');
669 return g_string_free (str
, FALSE
);
672 static AnjutaPluginDescriptionSection
*
673 lookup_section (AnjutaPluginDescription
*df
,
674 const char *section_name
)
676 AnjutaPluginDescriptionSection
*section
;
677 GQuark section_quark
;
680 section_quark
= g_quark_try_string (section_name
);
681 if (section_quark
== 0)
684 for (i
= 0; i
< df
->n_sections
; i
++)
686 section
= &df
->sections
[i
];
688 if (section
->section_name
== section_quark
)
694 static AnjutaPluginDescriptionLine
*
695 lookup_line (AnjutaPluginDescription
*df
,
696 AnjutaPluginDescriptionSection
*section
,
700 AnjutaPluginDescriptionLine
*line
;
704 key_quark
= g_quark_try_string (keyname
);
708 for (i
= 0; i
< section
->n_lines
; i
++)
710 line
= §ion
->lines
[i
];
712 if (line
->key
== key_quark
&&
713 ((locale
== NULL
&& line
->locale
== NULL
) ||
714 (locale
!= NULL
&& line
->locale
!= NULL
&& strcmp (locale
, line
->locale
) == 0)))
722 * anjuta_plugin_description_get_raw:
723 * @df: an #AnjutaPluginDescription object.
724 * @section_name: Name of the section.
725 * @keyname: Name of the key.
726 * @locale: The locale for which the value is to be retrieved.
727 * @val: Pointer to the variable to store the string value.
729 * Retrieves the value of a key (in the given section) for the given locale.
730 * The value returned in @val must be freed after use.
732 * Return value: TRUE if sucessful, otherwise FALSE.
735 anjuta_plugin_description_get_raw (AnjutaPluginDescription
*df
,
736 const char *section_name
,
741 AnjutaPluginDescriptionSection
*section
;
742 AnjutaPluginDescriptionLine
*line
;
746 section
= lookup_section (df
, section_name
);
750 line
= lookup_line (df
,
758 *val
= g_strdup (line
->value
);
764 * anjuta_plugin_description_foreach_section:
765 * @df: an #AnjutaPluginDescription object.
766 * @func: Callback function.
767 * @user_data: User data to pass to @func.
769 * Calls @func for each of the sections in the description.
772 anjuta_plugin_description_foreach_section (AnjutaPluginDescription
*df
,
773 AnjutaPluginDescriptionSectionFunc func
,
776 AnjutaPluginDescriptionSection
*section
;
779 for (i
= 0; i
< df
->n_sections
; i
++)
781 section
= &df
->sections
[i
];
783 (*func
) (df
, g_quark_to_string (section
->section_name
), user_data
);
789 * anjuta_plugin_description_foreach_key:
790 * @df: an #AnjutaPluginDescription object.
791 * @section_name: Name of the section.
792 * @include_localized: Whether each localized key should be called separately.
793 * @func: The callback function.
794 * @user_data: User data to pass to @func.
796 * Calls @func for each of the keys in the given section. @include_localized,
797 * if set to TRUE will make it call @func for the localized keys also, otherwise
798 * only one call is made for the key in current locale.
801 anjuta_plugin_description_foreach_key (AnjutaPluginDescription
*df
,
802 const char *section_name
,
803 gboolean include_localized
,
804 AnjutaPluginDescriptionLineFunc func
,
807 AnjutaPluginDescriptionSection
*section
;
808 AnjutaPluginDescriptionLine
*line
;
811 section
= lookup_section (df
, section_name
);
815 for (i
= 0; i
< section
->n_lines
; i
++)
817 line
= §ion
->lines
[i
];
819 (*func
) (df
, g_quark_to_string (line
->key
), line
->locale
, line
->value
, user_data
);
827 calculate_locale (AnjutaPluginDescription
*df
)
831 lang
= g_strdup (setlocale (LC_MESSAGES
, NULL
));
835 p
= strchr (lang
, '.');
838 p
= strchr (lang
, '@');
843 lang
= g_strdup ("C");
845 p
= strchr (lang
, '_');
848 df
->current_locale
[0] = g_strdup (lang
);
850 df
->current_locale
[1] = lang
;
854 df
->current_locale
[0] = lang
;
855 df
->current_locale
[1] = NULL
;
860 * anjuta_plugin_description_get_locale_string:
861 * @df: an #AnjutaPluginDescription object.
862 * @section: Section name.
863 * @keyname: Key name.
864 * @val: Pointer to value to store retured value.
866 * Returns the value of key in the given section in current locale.
868 * Return value: TRUE if sucessful, otherwise FALSE.
871 anjuta_plugin_description_get_locale_string (AnjutaPluginDescription
*df
,
878 if (df
->current_locale
[0] == NULL
)
879 calculate_locale (df
);
881 if (df
->current_locale
[0] != NULL
)
883 res
= anjuta_plugin_description_get_raw (df
,section
, keyname
,
884 df
->current_locale
[0], val
);
889 if (df
->current_locale
[1] != NULL
)
891 res
= anjuta_plugin_description_get_raw (df
,section
, keyname
,
892 df
->current_locale
[1], val
);
897 return anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, val
);
901 * anjuta_plugin_description_get_string:
902 * @df: an #AnjutaPluginDescription object.
903 * @section: Section name.
904 * @keyname: Key name.
905 * @val: Pointer to value to store retured value.
907 * Returns the value of key in the given section.
909 * Return value: TRUE if sucessful, otherwise FALSE.
912 anjuta_plugin_description_get_string (AnjutaPluginDescription
*df
,
917 return anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, val
);
921 * anjuta_plugin_description_get_integer:
922 * @df: an #AnjutaPluginDescription object.
923 * @section: Section name.
924 * @keyname: Key name.
925 * @val: Pointer to value to store retured value.
927 * Returns the value of key as integer in the given section.
929 * Return value: TRUE if sucessful, otherwise FALSE.
932 anjuta_plugin_description_get_integer (AnjutaPluginDescription
*df
,
942 res
= anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, &str
);
955 * anjuta_plugin_description_get_boolean:
956 * @df: an #AnjutaPluginDescription object.
957 * @section: Section name.
958 * @keyname: Key name.
959 * @val: Pointer to value to store retured value.
961 * Returns the value of key as boolean in the given section.
963 * Return value: TRUE if sucessful, otherwise FALSE.
966 anjuta_plugin_description_get_boolean (AnjutaPluginDescription
*df
,
976 res
= anjuta_plugin_description_get_raw (df
, section
, keyname
, NULL
, &str
);
980 if ((g_ascii_strcasecmp (str
, "yes") == 0) ||
981 (g_ascii_strcasecmp (str
, "true") == 0))
985 else if ((g_ascii_strcasecmp (str
, "no") == 0) ||
986 (g_ascii_strcasecmp (str
, "false") == 0))