Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / libanjuta / anjuta-plugin-description.c
blobede8c8977977f93e850812d3a6fe14a9b32d6f96
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);
591 while (parser.line && *parser.line)
593 if (*parser.line == '[') {
594 if (!parse_section_start (&parser, error))
595 return NULL;
596 } else if (is_blank_line (&parser) ||
597 *parser.line == '#')
598 parse_comment_or_blank (&parser);
599 else
601 if (!parse_key_value (&parser, error))
602 return NULL;
606 return parser.df;
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.
619 char *
620 anjuta_plugin_description_to_string (AnjutaPluginDescription *df)
622 AnjutaPluginDescriptionSection *section;
623 AnjutaPluginDescriptionLine *line;
624 GString *str;
625 char *s;
626 int i, j;
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);
639 g_free (s);
640 g_string_append (str, "]\n");
643 for (j = 0; j < section->n_lines; j++)
645 line = &section->lines[j];
647 if (line->key == 0)
649 g_string_append (str, line->value);
650 g_string_append_c (str, '\n');
652 else
654 g_string_append (str, g_quark_to_string (line->key));
655 if (line->locale)
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);
664 g_free (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;
679 int i;
681 section_quark = g_quark_try_string (section_name);
682 if (section_quark == 0)
683 return NULL;
685 for (i = 0; i < df->n_sections; i ++)
687 section = &df->sections[i];
689 if (section->section_name == section_quark)
690 return section;
692 return NULL;
695 static AnjutaPluginDescriptionLine *
696 lookup_line (AnjutaPluginDescription *df,
697 AnjutaPluginDescriptionSection *section,
698 const char *keyname,
699 const char *locale)
701 AnjutaPluginDescriptionLine *line;
702 GQuark key_quark;
703 int i;
705 key_quark = g_quark_try_string (keyname);
706 if (key_quark == 0)
707 return NULL;
709 for (i = 0; i < section->n_lines; i++)
711 line = &section->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)))
716 return line;
719 return NULL;
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.
735 gboolean
736 anjuta_plugin_description_get_raw (AnjutaPluginDescription *df,
737 const char *section_name,
738 const char *keyname,
739 const char *locale,
740 char **val)
742 AnjutaPluginDescriptionSection *section;
743 AnjutaPluginDescriptionLine *line;
745 *val = NULL;
747 section = lookup_section (df, section_name);
748 if (!section)
749 return FALSE;
751 line = lookup_line (df,
752 section,
753 keyname,
754 locale);
756 if (!line)
757 return FALSE;
759 *val = g_strdup (line->value);
761 return TRUE;
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.
772 void
773 anjuta_plugin_description_foreach_section (AnjutaPluginDescription *df,
774 AnjutaPluginDescriptionSectionFunc func,
775 gpointer user_data)
777 AnjutaPluginDescriptionSection *section;
778 int i;
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);
786 return;
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.
801 void
802 anjuta_plugin_description_foreach_key (AnjutaPluginDescription *df,
803 const char *section_name,
804 gboolean include_localized,
805 AnjutaPluginDescriptionLineFunc func,
806 gpointer user_data)
808 AnjutaPluginDescriptionSection *section;
809 AnjutaPluginDescriptionLine *line;
810 int i;
812 section = lookup_section (df, section_name);
813 if (!section)
814 return;
816 for (i = 0; i < section->n_lines; i++)
818 line = &section->lines[i];
820 (*func) (df, g_quark_to_string (line->key), line->locale, line->value, user_data);
823 return;
827 static void
828 calculate_locale (AnjutaPluginDescription *df)
830 char *p, *lang;
832 lang = g_strdup (setlocale (LC_MESSAGES, NULL));
834 if (lang)
836 p = strchr (lang, '.');
837 if (p)
838 *p = '\0';
839 p = strchr (lang, '@');
840 if (p)
841 *p = '\0';
843 else
844 lang = g_strdup ("C");
846 p = strchr (lang, '_');
847 if (p)
849 df->current_locale[0] = g_strdup (lang);
850 *p = '\0';
851 df->current_locale[1] = lang;
853 else
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.
871 gboolean
872 anjuta_plugin_description_get_locale_string (AnjutaPluginDescription *df,
873 const char *section,
874 const char *keyname,
875 char **val)
877 gboolean res;
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);
886 if (res)
887 return TRUE;
890 if (df->current_locale[1] != NULL)
892 res = anjuta_plugin_description_get_raw (df,section, keyname,
893 df->current_locale[1], val);
894 if (res)
895 return TRUE;
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.
912 gboolean
913 anjuta_plugin_description_get_string (AnjutaPluginDescription *df,
914 const char *section,
915 const char *keyname,
916 char **val)
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.
932 gboolean
933 anjuta_plugin_description_get_integer (AnjutaPluginDescription *df,
934 const char *section,
935 const char *keyname,
936 int *val)
938 gboolean res;
939 char *str;
941 *val = 0;
943 res = anjuta_plugin_description_get_raw (df, section, keyname, NULL, &str);
944 if (!res)
945 return FALSE;
948 *val = atoi (str);
949 g_free (str);
951 return TRUE;