libanjuta: bgo #696984 - Fix function argument name typos in documentation comments
[anjuta.git] / libanjuta / anjuta-plugin-description.c
blob14e48d222e655b9fe12b4acb81bcce150c23e7c3
1 /*
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.
21 /**
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
31 #include <string.h>
32 #include <locale.h>
33 #include <stdlib.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) */
43 gint n_lines;
44 AnjutaPluginDescriptionLine *lines;
47 struct _AnjutaPluginDescriptionLine {
48 GQuark key; /* 0 means comment or blank line in value */
49 char *locale;
50 gchar *value;
53 struct _AnjutaPluginDescription {
54 gint n_sections;
55 AnjutaPluginDescriptionSection *sections;
56 char *current_locale[2];
59 struct _AnjutaPluginDescriptionParser {
60 AnjutaPluginDescription *df;
61 gint current_section;
62 gint n_allocated_lines;
63 gint n_allocated_sections;
64 gint line_nr;
65 char *line;
68 #define VALID_KEY_CHAR 1
69 #define VALID_LOCALE_CHAR 2
70 guchar valid[256] = {
71 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
72 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
73 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 ,
74 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
75 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
76 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x2 ,
77 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 ,
78 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
79 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
80 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
81 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ,
82 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 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,
90 char *message,
91 AnjutaPluginDescriptionParseError error_code,
92 GError **error);
94 static AnjutaPluginDescriptionSection *lookup_section (AnjutaPluginDescription *df,
95 const char *section);
97 static AnjutaPluginDescriptionLine *lookup_line (AnjutaPluginDescription *df,
98 AnjutaPluginDescriptionSection *section,
99 const char *keyname,
100 const char *locale);
102 GQuark
103 anjuta_plugin_description_parse_error_quark (void)
105 static GQuark quark;
106 if (!quark)
107 quark = g_quark_from_static_string ("g_desktop_parse_error");
109 return quark;
112 static void
113 parser_free (AnjutaPluginDescriptionParser *parser)
115 anjuta_plugin_description_free (parser->df);
118 static void
119 anjuta_plugin_description_line_free (AnjutaPluginDescriptionLine *line)
121 g_free (line->locale);
122 g_free (line->value);
125 static void
126 anjuta_plugin_description_section_free (AnjutaPluginDescriptionSection *section)
128 int i;
130 for (i = 0; i < section->n_lines; i++)
131 anjuta_plugin_description_line_free (&section->lines[i]);
133 g_free (section->lines);
137 * anjuta_plugin_description_free:
138 * @df: an #AnjutaPluginDescription object
140 * Frees the #AnjutaPluginDescription instance.
142 void
143 anjuta_plugin_description_free (AnjutaPluginDescription *df)
145 int i;
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]);
153 g_free (df);
156 static void
157 grow_lines (AnjutaPluginDescriptionParser *parser)
159 int new_n_lines;
160 AnjutaPluginDescriptionSection *section;
162 if (parser->n_allocated_lines == 0)
163 new_n_lines = 1;
164 else
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;
174 static void
175 grow_sections (AnjutaPluginDescriptionParser *parser)
177 int new_n_sections;
179 if (parser->n_allocated_sections == 0)
180 new_n_sections = 1;
181 else
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;
189 static gchar *
190 unescape_string (gchar *str, gint len)
192 gchar *res;
193 gchar *p, *q;
194 gchar *end;
196 /* len + 1 is enough, because unescaping never makes the
197 * string longer */
198 res = g_new (gchar, len + 1);
199 p = str;
200 q = res;
201 end = str + len;
203 while (p < end)
205 if (*p == 0)
207 /* Found an embedded null */
208 g_free (res);
209 return NULL;
211 if (*p == '\\')
213 p++;
214 if (p >= end)
216 /* Escape at end of string */
217 g_free (res);
218 return NULL;
221 switch (*p)
223 case 's':
224 *q++ = ' ';
225 break;
226 case 't':
227 *q++ = '\t';
228 break;
229 case 'n':
230 *q++ = '\n';
231 break;
232 case 'r':
233 *q++ = '\r';
234 break;
235 case '\\':
236 *q++ = '\\';
237 break;
238 default:
239 /* Invalid escape code */
240 g_free (res);
241 return NULL;
243 p++;
245 else
246 *q++ = *p++;
248 *q = 0;
250 return res;
253 static gchar *
254 escape_string (const gchar *str, gboolean escape_first_space)
256 gchar *res;
257 char *q;
258 const gchar *p;
260 /* len + 1 is enough, because unescaping never makes the
261 * string longer */
262 res = g_new (gchar, strlen (str)*2 + 1);
264 p = str;
265 q = res;
267 while (*p)
269 if (*p == ' ')
271 if (escape_first_space && p == str)
273 *q++ = '\\';
274 *q++ = 's';
276 else
277 *q++ = ' ';
279 else if (*p == '\\')
281 *q++ = '\\';
282 *q++ = '\\';
284 else if (*p == '\t')
286 *q++ = '\\';
287 *q++ = 't';
289 else if (*p == '\n')
291 *q++ = '\\';
292 *q++ = 'n';
294 else if (*p == '\r')
296 *q++ = '\\';
297 *q++ = 'r';
299 else
300 *q++ = *p;
301 p++;
303 *q = 0;
305 return res;
309 static void
310 open_section (AnjutaPluginDescriptionParser *parser,
311 const char *name)
313 int n;
315 if (parser->n_allocated_sections == parser->df->n_sections)
316 grow_sections (parser);
318 if (parser->current_section == 0 &&
319 parser->df->sections[0].section_name == 0 &&
320 parser->df->sections[0].n_lines == 0)
322 if (!name)
323 g_warning ("non-initial NULL section\n");
325 /* The initial section was empty. Piggyback on it. */
326 parser->df->sections[0].section_name = g_quark_from_string (name);
328 return;
331 n = parser->df->n_sections++;
333 if (name)
334 parser->df->sections[n].section_name = g_quark_from_string (name);
335 else
336 parser->df->sections[n].section_name = 0;
337 parser->df->sections[n].n_lines = 0;
338 parser->df->sections[n].lines = NULL;
340 parser->current_section = n;
341 parser->n_allocated_lines = 0;
342 grow_lines (parser);
345 static AnjutaPluginDescriptionLine *
346 new_line (AnjutaPluginDescriptionParser *parser)
348 AnjutaPluginDescriptionSection *section;
349 AnjutaPluginDescriptionLine *line;
351 section = &parser->df->sections[parser->current_section];
353 if (parser->n_allocated_lines == section->n_lines)
354 grow_lines (parser);
356 line = &section->lines[section->n_lines++];
358 memset (line, 0, sizeof (AnjutaPluginDescriptionLine));
360 return line;
363 static gboolean
364 is_blank_line (AnjutaPluginDescriptionParser *parser)
366 gchar *p;
368 p = parser->line;
370 while (*p && *p != '\n')
372 if (!g_ascii_isspace (*p))
373 return FALSE;
375 p++;
377 return TRUE;
380 static void
381 parse_comment_or_blank (AnjutaPluginDescriptionParser *parser)
383 AnjutaPluginDescriptionLine *line;
384 gchar *line_end;
386 line_end = strchr (parser->line, '\n');
387 if (line_end == NULL)
388 line_end = parser->line + strlen (parser->line);
390 line = new_line (parser);
392 line->value = g_strndup (parser->line, line_end - parser->line);
394 parser->line = (line_end) ? line_end + 1 : NULL;
395 parser->line_nr++;
398 static gboolean
399 parse_section_start (AnjutaPluginDescriptionParser *parser, GError **error)
401 gchar *line_end;
402 gchar *section_name;
404 line_end = strchr (parser->line, '\n');
405 if (line_end == NULL)
406 line_end = parser->line + strlen (parser->line);
408 if (line_end - parser->line <= 2 ||
409 line_end[-1] != ']')
411 report_error (parser, "Invalid syntax for section header", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
412 parser_free (parser);
413 return FALSE;
416 section_name = unescape_string (parser->line + 1, line_end - parser->line - 2);
418 if (section_name == NULL)
420 report_error (parser, "Invalid escaping in section name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES, error);
421 parser_free (parser);
422 return FALSE;
425 open_section (parser, section_name);
427 parser->line = (line_end) ? line_end + 1 : NULL;
428 parser->line_nr++;
430 g_free (section_name);
432 return TRUE;
435 static gboolean
436 parse_key_value (AnjutaPluginDescriptionParser *parser, GError **error)
438 AnjutaPluginDescriptionLine *line;
439 gchar *line_end;
440 gchar *key_start;
441 gchar *key_end;
442 gchar *key;
443 gchar *locale_start = NULL;
444 gchar *locale_end = NULL;
445 gchar *value_start;
446 gchar *value;
447 gchar *p;
449 line_end = strchr (parser->line, '\n');
450 if (line_end == NULL)
451 line_end = parser->line + strlen (parser->line);
453 p = parser->line;
454 key_start = p;
455 while (p < line_end &&
456 (valid[(guchar)*p] & VALID_KEY_CHAR))
457 p++;
458 key_end = p;
460 if (key_start == key_end)
462 report_error (parser, "Empty key name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
463 parser_free (parser);
464 return FALSE;
467 if (p < line_end && *p == '[')
469 p++;
470 locale_start = p;
471 while (p < line_end && *p != ']')
472 p++;
473 locale_end = p;
475 if (p == line_end)
477 report_error (parser, "Unterminated locale specification in key", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
478 parser_free (parser);
479 return FALSE;
482 p++;
485 /* Skip space before '=' */
486 while (p < line_end && *p == ' ')
487 p++;
489 if (p < line_end && *p != '=')
491 report_error (parser, "Invalid characters in key name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_CHARS, error);
492 parser_free (parser);
493 return FALSE;
496 if (p == line_end)
498 report_error (parser, "No '=' in key/value pair", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
499 parser_free (parser);
500 return FALSE;
503 /* Skip the '=' */
504 p++;
506 /* Skip space after '=' */
507 while (p < line_end && *p == ' ')
508 p++;
510 value_start = p;
512 value = unescape_string (value_start, line_end - value_start);
513 if (value == NULL)
515 report_error (parser, "Invalid escaping in value", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES, error);
516 parser_free (parser);
517 return FALSE;
520 line = new_line (parser);
521 key = g_strndup (key_start, key_end - key_start);
522 line->key = g_quark_from_string (key);
523 g_free (key);
524 if (locale_start)
525 line->locale = g_strndup (locale_start, locale_end - locale_start);
526 line->value = value;
528 parser->line = (*line_end) ? line_end + 1 : NULL;
529 parser->line_nr++;
531 return TRUE;
535 static void
536 report_error (AnjutaPluginDescriptionParser *parser,
537 char *message,
538 AnjutaPluginDescriptionParseError error_code,
539 GError **error)
541 AnjutaPluginDescriptionSection *section;
542 const gchar *section_name = NULL;
544 section = &parser->df->sections[parser->current_section];
546 if (section->section_name)
547 section_name = g_quark_to_string (section->section_name);
549 if (error)
551 if (section_name)
552 *error = g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR,
553 error_code,
554 "Error in section %s at line %d: %s", section_name, parser->line_nr, message);
555 else
556 *error = g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR,
557 error_code,
558 "Error at line %d: %s", parser->line_nr, message);
562 AnjutaPluginDescription*
563 anjuta_plugin_description_copy (AnjutaPluginDescription *df)
565 return anjuta_plugin_description_new_from_string (anjuta_plugin_description_to_string (df),
566 NULL);
569 GType
570 anjuta_plugin_description_get_type (void)
572 static GType type_id = 0;
574 if (!type_id)
575 type_id = g_boxed_type_register_static ("AnjutaPluginDescription",
576 (GBoxedCopyFunc) anjuta_plugin_description_copy,
577 (GBoxedFreeFunc) anjuta_plugin_description_free);
579 return type_id;
584 * anjuta_plugin_description_new_from_string:
585 * @data: The data to parse. The format of the data is .ini style.
587 * Parses the given plugin description data (usally read from the plugin
588 * description file and creates an instance of #AnjutaPluginDescription.
589 * The format of the content string is similar to .ini format.
591 * Return value: a new #AnjutaPluginDescription object
593 AnjutaPluginDescription *
594 anjuta_plugin_description_new_from_string (char *data, GError **error)
596 AnjutaPluginDescriptionParser parser;
598 parser.df = g_new0 (AnjutaPluginDescription, 1);
599 parser.current_section = -1;
601 parser.n_allocated_lines = 0;
602 parser.n_allocated_sections = 0;
603 parser.line_nr = 1;
605 parser.line = data;
607 /* Put any initial comments in a NULL segment */
608 open_section (&parser, NULL);
609 while (parser.line != NULL && strlen(parser.line))
611 if (*parser.line == '[') {
612 if (!parse_section_start (&parser, error))
613 return NULL;
614 } else if (is_blank_line (&parser) ||
615 *parser.line == '#')
616 parse_comment_or_blank (&parser);
617 else
619 if (!parse_key_value (&parser, error))
620 return NULL;
624 return parser.df;
628 * anjuta_plugin_description_to_string:
629 * @df: an #AnjutaPluginDescription object.
631 * Converts the description detains into string format, usually for
632 * saving it in a file.
634 * Return value: The string representation of the description. The
635 * returned values must be freed after use.
637 char *
638 anjuta_plugin_description_to_string (AnjutaPluginDescription *df)
640 AnjutaPluginDescriptionSection *section;
641 AnjutaPluginDescriptionLine *line;
642 GString *str;
643 char *s;
644 int i, j;
646 str = g_string_sized_new (800);
648 for (i = 0; i < df->n_sections; i ++)
650 section = &df->sections[i];
652 if (section->section_name)
654 g_string_append_c (str, '[');
655 s = escape_string (g_quark_to_string (section->section_name), FALSE);
656 g_string_append (str, s);
657 g_free (s);
658 g_string_append (str, "]\n");
661 for (j = 0; j < section->n_lines; j++)
663 line = &section->lines[j];
665 if (line->key == 0)
667 g_string_append (str, line->value);
668 g_string_append_c (str, '\n');
670 else
672 g_string_append (str, g_quark_to_string (line->key));
673 if (line->locale)
675 g_string_append_c (str, '[');
676 g_string_append (str, line->locale);
677 g_string_append_c (str, ']');
679 g_string_append_c (str, '=');
680 s = escape_string (line->value, TRUE);
681 g_string_append (str, s);
682 g_free (s);
683 g_string_append_c (str, '\n');
688 return g_string_free (str, FALSE);
691 static AnjutaPluginDescriptionSection *
692 lookup_section (AnjutaPluginDescription *df,
693 const char *section_name)
695 AnjutaPluginDescriptionSection *section;
696 GQuark section_quark;
697 int i;
699 section_quark = g_quark_try_string (section_name);
700 if (section_quark == 0)
701 return NULL;
703 for (i = 0; i < df->n_sections; i ++)
705 section = &df->sections[i];
707 if (section->section_name == section_quark)
708 return section;
710 return NULL;
713 static AnjutaPluginDescriptionLine *
714 lookup_line (AnjutaPluginDescription *df,
715 AnjutaPluginDescriptionSection *section,
716 const char *keyname,
717 const char *locale)
719 AnjutaPluginDescriptionLine *line;
720 GQuark key_quark;
721 int i;
723 key_quark = g_quark_try_string (keyname);
724 if (key_quark == 0)
725 return NULL;
727 for (i = 0; i < section->n_lines; i++)
729 line = &section->lines[i];
731 if (line->key == key_quark &&
732 ((locale == NULL && line->locale == NULL) ||
733 (locale != NULL && line->locale != NULL && strcmp (locale, line->locale) == 0)))
734 return line;
737 return NULL;
741 * anjuta_plugin_description_get_raw:
742 * @df: an #AnjutaPluginDescription object.
743 * @section_name: Name of the section.
744 * @keyname: Name of the key.
745 * @locale: The locale for which the value is to be retrieved.
746 * @val: Pointer to the variable to store the string value.
748 * Retrieves the value of a key (in the given section) for the given locale.
749 * The value returned in @val must be freed after use.
751 * Return value: TRUE if sucessful, otherwise FALSE.
753 gboolean
754 anjuta_plugin_description_get_raw (AnjutaPluginDescription *df,
755 const char *section_name,
756 const char *keyname,
757 const char *locale,
758 char **val)
760 AnjutaPluginDescriptionSection *section;
761 AnjutaPluginDescriptionLine *line;
763 *val = NULL;
765 section = lookup_section (df, section_name);
766 if (!section)
767 return FALSE;
769 line = lookup_line (df,
770 section,
771 keyname,
772 locale);
774 if (!line)
775 return FALSE;
777 *val = g_strdup (line->value);
779 return TRUE;
783 * anjuta_plugin_description_foreach_section:
784 * @df: an #AnjutaPluginDescription object.
785 * @func: Callback function.
786 * @user_data: User data to pass to @func.
788 * Calls @func for each of the sections in the description.
790 void
791 anjuta_plugin_description_foreach_section (AnjutaPluginDescription *df,
792 AnjutaPluginDescriptionSectionFunc func,
793 gpointer user_data)
795 AnjutaPluginDescriptionSection *section;
796 int i;
798 for (i = 0; i < df->n_sections; i ++)
800 section = &df->sections[i];
802 (*func) (df, g_quark_to_string (section->section_name), user_data);
804 return;
808 * anjuta_plugin_description_foreach_key:
809 * @df: an #AnjutaPluginDescription object.
810 * @section_name: Name of the section.
811 * @include_localized: Whether each localized key should be called separately.
812 * @func: The callback function.
813 * @user_data: User data to pass to @func.
815 * Calls @func for each of the keys in the given section. @include_localized,
816 * if set to TRUE will make it call @func for the localized keys also, otherwise
817 * only one call is made for the key in current locale.
819 void
820 anjuta_plugin_description_foreach_key (AnjutaPluginDescription *df,
821 const char *section_name,
822 gboolean include_localized,
823 AnjutaPluginDescriptionLineFunc func,
824 gpointer user_data)
826 AnjutaPluginDescriptionSection *section;
827 AnjutaPluginDescriptionLine *line;
828 int i;
830 section = lookup_section (df, section_name);
831 if (!section)
832 return;
834 for (i = 0; i < section->n_lines; i++)
836 line = &section->lines[i];
838 (*func) (df, g_quark_to_string (line->key), line->locale, line->value, user_data);
841 return;
845 static void
846 calculate_locale (AnjutaPluginDescription *df)
848 char *p, *lang;
850 lang = g_strdup (setlocale (LC_MESSAGES, NULL));
852 if (lang)
854 p = strchr (lang, '.');
855 if (p)
856 *p = '\0';
857 p = strchr (lang, '@');
858 if (p)
859 *p = '\0';
861 else
862 lang = g_strdup ("C");
864 p = strchr (lang, '_');
865 if (p)
867 df->current_locale[0] = g_strdup (lang);
868 *p = '\0';
869 df->current_locale[1] = lang;
871 else
873 df->current_locale[0] = lang;
874 df->current_locale[1] = NULL;
879 * anjuta_plugin_description_get_locale_string:
880 * @df: an #AnjutaPluginDescription object.
881 * @section: Section name.
882 * @keyname: Key name.
883 * @val: Pointer to value to store retured value.
885 * Returns the value of key in the given section in current locale.
887 * Return value: TRUE if sucessful, otherwise FALSE.
889 gboolean
890 anjuta_plugin_description_get_locale_string (AnjutaPluginDescription *df,
891 const char *section,
892 const char *keyname,
893 char **val)
895 gboolean res;
897 if (df->current_locale[0] == NULL)
898 calculate_locale (df);
900 if (df->current_locale[0] != NULL)
902 res = anjuta_plugin_description_get_raw (df,section, keyname,
903 df->current_locale[0], val);
904 if (res)
905 return TRUE;
908 if (df->current_locale[1] != NULL)
910 res = anjuta_plugin_description_get_raw (df,section, keyname,
911 df->current_locale[1], val);
912 if (res)
913 return TRUE;
916 return anjuta_plugin_description_get_raw (df, section, keyname, NULL, val);
920 * anjuta_plugin_description_get_string:
921 * @df: an #AnjutaPluginDescription object.
922 * @section: Section name.
923 * @keyname: Key name.
924 * @val: Pointer to value to store retured value.
926 * Returns the value of key in the given section.
928 * Return value: TRUE if sucessful, otherwise FALSE.
930 gboolean
931 anjuta_plugin_description_get_string (AnjutaPluginDescription *df,
932 const char *section,
933 const char *keyname,
934 char **val)
936 return anjuta_plugin_description_get_raw (df, section, keyname, NULL, val);
940 * anjuta_plugin_description_get_integer:
941 * @df: an #AnjutaPluginDescription object.
942 * @section: Section name.
943 * @keyname: Key name.
944 * @val: Pointer to value to store retured value.
946 * Returns the value of key as integer in the given section.
948 * Return value: TRUE if sucessful, otherwise FALSE.
950 gboolean
951 anjuta_plugin_description_get_integer (AnjutaPluginDescription *df,
952 const char *section,
953 const char *keyname,
954 int *val)
956 gboolean res;
957 char *str;
959 *val = 0;
961 res = anjuta_plugin_description_get_raw (df, section, keyname, NULL, &str);
962 if (!res)
963 return FALSE;
966 *val = atoi (str);
967 g_free (str);
969 return TRUE;
974 * anjuta_plugin_description_get_boolean:
975 * @df: an #AnjutaPluginDescription object.
976 * @section: Section name.
977 * @keyname: Key name.
978 * @val: Pointer to value to store retured value.
980 * Returns the value of key as boolean in the given section.
982 * Return value: TRUE if sucessful, otherwise FALSE.
984 gboolean
985 anjuta_plugin_description_get_boolean (AnjutaPluginDescription *df,
986 const char *section,
987 const char *keyname,
988 gboolean *val)
990 gboolean res;
991 char *str;
993 *val = 0;
995 res = anjuta_plugin_description_get_raw (df, section, keyname, NULL, &str);
996 if (!res)
997 return FALSE;
999 if ((g_ascii_strcasecmp (str, "yes") == 0) ||
1000 (g_ascii_strcasecmp (str, "true") == 0))
1002 *val = TRUE;
1004 else if ((g_ascii_strcasecmp (str, "no") == 0) ||
1005 (g_ascii_strcasecmp (str, "false") == 0))
1008 *val = FALSE;
1010 else
1012 res = FALSE;
1015 g_free (str);
1017 return res;