~ubuntu-branches/debian/sid/link-monitor-applet/sid

« back to all changes in this revision

Viewing changes to jbsrc/lib/jb-config.c

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2008-03-30 22:26:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080330222613-5aubcuo9mgg2n7st
Tags: upstream-3.0
ImportĀ upstreamĀ versionĀ 3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * JB, the Jean-Yves Lefort's Build System
 
3
 * Copyright (C) 2008 Jean-Yves Lefort <jylefort@brutele.be>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program 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
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include <stdlib.h>
 
21
#include "jb-config.h"
 
22
#include "jb-variable.h"
 
23
#include "jb-util.h"
 
24
 
 
25
void
 
26
jb_config_save (void)
 
27
{
 
28
  GString *config;
 
29
  GSList *l;
 
30
 
 
31
  config = g_string_new(NULL);
 
32
 
 
33
  JB_LIST_FOREACH(l, jb_variables)
 
34
    {
 
35
      JBVariable *variable = l->data;
 
36
      char *value;
 
37
      char *escaped;
 
38
 
 
39
      value = jb_variable_to_string(variable);
 
40
      escaped = g_strescape(value, NULL);
 
41
      g_free(value);
 
42
 
 
43
      g_string_append_printf(config,
 
44
                             "%s|%s|%i|%s\n",
 
45
                             variable->name,
 
46
                             jb_variable_get_type_name(variable),
 
47
                             variable->flags,
 
48
                             escaped);
 
49
      g_free(escaped);
 
50
    }
 
51
 
 
52
  jb_write_file_or_exit(JB_CONFIG_FILE, config->str);
 
53
  g_string_free(config, TRUE);
 
54
}
 
55
 
 
56
void
 
57
jb_config_load (void)
 
58
{
 
59
  char *config;
 
60
  char **lines;
 
61
  int i;
 
62
 
 
63
  config = jb_read_file_or_exit(JB_CONFIG_FILE);
 
64
  lines = g_strsplit(config, "\n", 0);
 
65
  g_free(config);
 
66
 
 
67
  for (i = 0; lines[i] != NULL; i++)
 
68
    {
 
69
      int lineno = i + 1;
 
70
      char **fields;
 
71
      JBVariable *variable;
 
72
      JBVariableType *type;
 
73
      char *unescaped;
 
74
      GError *err = NULL;
 
75
 
 
76
      if (*lines[i] == '\0')
 
77
        continue;
 
78
 
 
79
      fields = g_strsplit(lines[i], "|", 4);
 
80
      if (g_strv_length(fields) != 4)
 
81
        jb_error("%s:%i: cannot parse line", JB_CONFIG_FILE, lineno);
 
82
 
 
83
      type = jb_variable_get_type(fields[1]);
 
84
      if (type == NULL)
 
85
        jb_error("%s:%i: invalid type \"%s\"", JB_CONFIG_FILE, lineno, fields[1]);
 
86
 
 
87
      variable = jb_variable_get_variable(fields[0]);
 
88
      if (variable == NULL)
 
89
        variable = jb_variable_add(type, fields[0], NULL, NULL, atoi(fields[2]));
 
90
      else
 
91
        {
 
92
          if (type != variable->type)
 
93
            jb_error("%s:%i: inconsistent type \"%s\" (type \"%s\" expected)",
 
94
                     JB_CONFIG_FILE,
 
95
                     lineno,
 
96
                     type->name,
 
97
                     variable->type->name);
 
98
        }
 
99
 
 
100
      unescaped = g_strcompress(fields[3]);
 
101
 
 
102
      if (! jb_variable_set_from_string(variable, unescaped, &err))
 
103
        jb_error("%s:%i: invalid value for type \"%s\": %s",
 
104
                 JB_CONFIG_FILE,
 
105
                 lineno,
 
106
                 type->name,
 
107
                 err->message);
 
108
 
 
109
      g_free(unescaped);
 
110
    }
 
111
 
 
112
  g_strfreev(lines);
 
113
}