python-loader: Fix build with python3 (bgo#633786)
[anjuta.git] / libanjuta / anjuta-plugin-description.c
blob49c666fd5466c5346dde845bbb64e35c4e4168f7
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;
259 const gchar *end;
261 /* len + 1 is enough, because unescaping never makes the
262 * string longer */
263 res = g_new (gchar, strlen (str)*2 + 1);
265 p = str;
266 q = res;
267 end = str + strlen (str);
269 while (*p)
271 if (*p == ' ')
273 if (escape_first_space && p == str)
275 *q++ = '\\';
276 *q++ = 's';
278 else
279 *q++ = ' ';
281 else if (*p == '\\')
283 *q++ = '\\';
284 *q++ = '\\';
286 else if (*p == '\t')
288 *q++ = '\\';
289 *q++ = 't';
291 else if (*p == '\n')
293 *q++ = '\\';
294 *q++ = 'n';
296 else if (*p == '\r')
298 *q++ = '\\';
299 *q++ = 'r';
301 else
302 *q++ = *p;
303 p++;
305 *q = 0;
307 return res;
311 static void
312 open_section (AnjutaPluginDescriptionParser *parser,
313 const char *name)
315 int n;
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)
324 if (!name)
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);
330 return;
333 n = parser->df->n_sections++;
335 if (name)
336 parser->df->sections[n].section_name = g_quark_from_string (name);
337 else
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;
344 grow_lines (parser);
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)
356 grow_lines (parser);
358 line = &section->lines[section->n_lines++];
360 memset (line, 0, sizeof (AnjutaPluginDescriptionLine));
362 return line;
365 static gboolean
366 is_blank_line (AnjutaPluginDescriptionParser *parser)
368 gchar *p;
370 p = parser->line;
372 while (*p && *p != '\n')
374 if (!g_ascii_isspace (*p))
375 return FALSE;
377 p++;
379 return TRUE;
382 static void
383 parse_comment_or_blank (AnjutaPluginDescriptionParser *parser)
385 AnjutaPluginDescriptionLine *line;
386 gchar *line_end;
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;
397 parser->line_nr++;
400 static gboolean
401 parse_section_start (AnjutaPluginDescriptionParser *parser, GError **error)
403 gchar *line_end;
404 gchar *section_name;
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 ||
411 line_end[-1] != ']')
413 report_error (parser, "Invalid syntax for section header", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
414 parser_free (parser);
415 return FALSE;
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);
424 return FALSE;
427 open_section (parser, section_name);
429 parser->line = (line_end) ? line_end + 1 : NULL;
430 parser->line_nr++;
432 g_free (section_name);
434 return TRUE;
437 static gboolean
438 parse_key_value (AnjutaPluginDescriptionParser *parser, GError **error)
440 AnjutaPluginDescriptionLine *line;
441 gchar *line_end;
442 gchar *key_start;
443 gchar *key_end;
444 gchar *key;
445 gchar *locale_start = NULL;
446 gchar *locale_end = NULL;
447 gchar *value_start;
448 gchar *value;
449 gchar *p;
451 line_end = strchr (parser->line, '\n');
452 if (line_end == NULL)
453 line_end = parser->line + strlen (parser->line);
455 p = parser->line;
456 key_start = p;
457 while (p < line_end &&
458 (valid[(guchar)*p] & VALID_KEY_CHAR))
459 p++;
460 key_end = p;
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);
466 return FALSE;
469 if (p < line_end && *p == '[')
471 p++;
472 locale_start = p;
473 while (p < line_end && *p != ']')
474 p++;
475 locale_end = p;
477 if (p == line_end)
479 report_error (parser, "Unterminated locale specification in key", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
480 parser_free (parser);
481 return FALSE;
484 p++;
487 /* Skip space before '=' */
488 while (p < line_end && *p == ' ')
489 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);
495 return FALSE;
498 if (p == line_end)
500 report_error (parser, "No '=' in key/value pair", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
501 parser_free (parser);
502 return FALSE;
505 /* Skip the '=' */
506 p++;
508 /* Skip space after '=' */
509 while (p < line_end && *p == ' ')
510 p++;
512 value_start = p;
514 value = unescape_string (value_start, line_end - value_start);
515 if (value == NULL)
517 report_error (parser, "Invalid escaping in value", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES, error);
518 parser_free (parser);
519 return FALSE;
522 line = new_line (parser);
523 key = g_strndup (key_start, key_end - key_start);
524 line->key = g_quark_from_string (key);
525 g_free (key);
526 if (locale_start)
527 line->locale = g_strndup (locale_start, locale_end - locale_start);
528 line->value = value;
530 parser->line = (*line_end) ? line_end + 1 : NULL;
531 parser->line_nr++;
533 return TRUE;
537 static void
538 report_error (AnjutaPluginDescriptionParser *parser,
539 char *message,
540 AnjutaPluginDescriptionParseError error_code,
541 GError **error)
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);
551 if (error)
553 if (section_name)
554 *error = g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR,
555 error_code,
556 "Error in section %s at line %d: %s", section_name, parser->line_nr, message);
557 else
558 *error = g_error_new (ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR,
559 error_code,
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;
584 parser.line_nr = 1;
586 parser.line = data;
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))
594 return NULL;
595 } else if (is_blank_line (&parser) ||
596 *parser.line == '#')
597 parse_comment_or_blank (&parser);
598 else
600 if (!parse_key_value (&parser, error))
601 return NULL;
605 return parser.df;
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.
618 char *
619 anjuta_plugin_description_to_string (AnjutaPluginDescription *df)
621 AnjutaPluginDescriptionSection *section;
622 AnjutaPluginDescriptionLine *line;
623 GString *str;
624 char *s;
625 int i, j;
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);
638 g_free (s);
639 g_string_append (str, "]\n");
642 for (j = 0; j < section->n_lines; j++)
644 line = &section->lines[j];
646 if (line->key == 0)
648 g_string_append (str, line->value);
649 g_string_append_c (str, '\n');
651 else
653 g_string_append (str, g_quark_to_string (line->key));
654 if (line->locale)
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);
663 g_free (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;
678 int i;
680 section_quark = g_quark_try_string (section_name);
681 if (section_quark == 0)
682 return NULL;
684 for (i = 0; i < df->n_sections; i ++)
686 section = &df->sections[i];
688 if (section->section_name == section_quark)
689 return section;
691 return NULL;
694 static AnjutaPluginDescriptionLine *
695 lookup_line (AnjutaPluginDescription *df,
696 AnjutaPluginDescriptionSection *section,
697 const char *keyname,
698 const char *locale)
700 AnjutaPluginDescriptionLine *line;
701 GQuark key_quark;
702 int i;
704 key_quark = g_quark_try_string (keyname);
705 if (key_quark == 0)
706 return NULL;
708 for (i = 0; i < section->n_lines; i++)
710 line = &section->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)))
715 return line;
718 return NULL;
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.
734 gboolean
735 anjuta_plugin_description_get_raw (AnjutaPluginDescription *df,
736 const char *section_name,
737 const char *keyname,
738 const char *locale,
739 char **val)
741 AnjutaPluginDescriptionSection *section;
742 AnjutaPluginDescriptionLine *line;
744 *val = NULL;
746 section = lookup_section (df, section_name);
747 if (!section)
748 return FALSE;
750 line = lookup_line (df,
751 section,
752 keyname,
753 locale);
755 if (!line)
756 return FALSE;
758 *val = g_strdup (line->value);
760 return TRUE;
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.
771 void
772 anjuta_plugin_description_foreach_section (AnjutaPluginDescription *df,
773 AnjutaPluginDescriptionSectionFunc func,
774 gpointer user_data)
776 AnjutaPluginDescriptionSection *section;
777 int i;
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);
785 return;
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.
800 void
801 anjuta_plugin_description_foreach_key (AnjutaPluginDescription *df,
802 const char *section_name,
803 gboolean include_localized,
804 AnjutaPluginDescriptionLineFunc func,
805 gpointer user_data)
807 AnjutaPluginDescriptionSection *section;
808 AnjutaPluginDescriptionLine *line;
809 int i;
811 section = lookup_section (df, section_name);
812 if (!section)
813 return;
815 for (i = 0; i < section->n_lines; i++)
817 line = &section->lines[i];
819 (*func) (df, g_quark_to_string (line->key), line->locale, line->value, user_data);
822 return;
826 static void
827 calculate_locale (AnjutaPluginDescription *df)
829 char *p, *lang;
831 lang = g_strdup (setlocale (LC_MESSAGES, NULL));
833 if (lang)
835 p = strchr (lang, '.');
836 if (p)
837 *p = '\0';
838 p = strchr (lang, '@');
839 if (p)
840 *p = '\0';
842 else
843 lang = g_strdup ("C");
845 p = strchr (lang, '_');
846 if (p)
848 df->current_locale[0] = g_strdup (lang);
849 *p = '\0';
850 df->current_locale[1] = lang;
852 else
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.
870 gboolean
871 anjuta_plugin_description_get_locale_string (AnjutaPluginDescription *df,
872 const char *section,
873 const char *keyname,
874 char **val)
876 gboolean res;
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);
885 if (res)
886 return TRUE;
889 if (df->current_locale[1] != NULL)
891 res = anjuta_plugin_description_get_raw (df,section, keyname,
892 df->current_locale[1], val);
893 if (res)
894 return TRUE;
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.
911 gboolean
912 anjuta_plugin_description_get_string (AnjutaPluginDescription *df,
913 const char *section,
914 const char *keyname,
915 char **val)
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.
931 gboolean
932 anjuta_plugin_description_get_integer (AnjutaPluginDescription *df,
933 const char *section,
934 const char *keyname,
935 int *val)
937 gboolean res;
938 char *str;
940 *val = 0;
942 res = anjuta_plugin_description_get_raw (df, section, keyname, NULL, &str);
943 if (!res)
944 return FALSE;
947 *val = atoi (str);
948 g_free (str);
950 return TRUE;
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.
965 gboolean
966 anjuta_plugin_description_get_boolean (AnjutaPluginDescription *df,
967 const char *section,
968 const char *keyname,
969 gboolean *val)
971 gboolean res;
972 char *str;
974 *val = 0;
976 res = anjuta_plugin_description_get_raw (df, section, keyname, NULL, &str);
977 if (!res)
978 return FALSE;
980 if ((g_ascii_strcasecmp (str, "yes") == 0) ||
981 (g_ascii_strcasecmp (str, "true") == 0))
983 *val = TRUE;
985 else if ((g_ascii_strcasecmp (str, "no") == 0) ||
986 (g_ascii_strcasecmp (str, "false") == 0))
989 *val = FALSE;
991 else
993 res = FALSE;
996 g_free (str);
998 return res;