File: sequence_load.c

package info (click to toggle)
lebiniou 3.18-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,396 kB
  • sloc: ansic: 20,369; sh: 4,169; makefile: 834; awk: 194
file content (263 lines) | stat: -rw-r--r-- 6,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  Copyright 1994-2012 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "globals.h"
#include "xmlutils.h"
#include "pictures.h"
#include "colormaps.h"


/*
 * Left as an exercise to the reader: this code is not robust at all
 * we expect to read files that have been writen by Sequence_write.
 * But we are talibans and are not kind with wrong inputs.
 */
Sequence_t *
Sequence_load(const char *file)
{
  Sequence_t *s = NULL;
  xmlDocPtr doc = NULL; /* XmlTree */
  xmlNodePtr sequence_node = NULL, plugins_node = NULL;
  int res;
  long tmp;
  char *dot = NULL;
  gchar *blah = NULL;
  xmlChar *youhou;
  const gchar *home_dir = NULL;

  if (file == NULL) {
    printf("[!] Attempt to load a sequence with a NULL filename\n");
    return NULL;
  }

  dot = strrchr(file, '.');
  if ((dot == NULL) || strcasecmp(dot, ".xml")) {
#ifdef XDEBUG
    printf("[!] Not a sequence filename: '%s'\n", file);
#endif
    return NULL;
  }    

#ifdef XDEBUG
  printf("[i] Loading sequence from file '%s'\n", file);
#endif

  /* BLA ! */
  xmlKeepBlanksDefault(0);
  xmlSubstituteEntitiesDefault(1);

  /*
   * build an XML tree from the file
   */
  home_dir = g_get_home_dir();
  blah = g_strdup_printf("%s/%s/%s", home_dir, SAVEDIR, file);

  doc = xmlParseFile(blah);
  g_free(blah);
  if (doc == NULL) {
    printf("[!] xmlParseFile error\n");
    return NULL;
  }
  
  sequence_node = xmlDocGetRootElement(doc);
  if (sequence_node == NULL) {
    printf("[!] Sequence %s: xmlDocGetRootElement error\n", file);
    goto error;
  }

  sequence_node = xmlFindElement("sequence", sequence_node);
  if (sequence_node == NULL) {
    printf("[!] Sequence %s: no <sequence> found\n", file);
    goto error;
  }

  youhou = xmlGetProp(sequence_node, (const xmlChar *)"id");
  tmp = getintfield(youhou);
  xmlFree(youhou);
  assert(tmp > 0);

  s = Sequence_new(tmp);

  /* first, get <auto_colormaps> */
  sequence_node = sequence_node->xmlChildrenNode;

  sequence_node = xmlFindElement("auto_colormaps", sequence_node);
  if (sequence_node == NULL) {
    printf("[!] Sequence %s: no <auto_colormaps> found\n", file);
    goto error;
  }
  res = xmlGetOptionalLong(doc, sequence_node, &tmp);
  if (res == -1)
    s->auto_colormaps = 0;
  else
    s->auto_colormaps = (u_char)tmp;
  assert((s->auto_colormaps == 0) || (s->auto_colormaps == 1));
#ifdef XDEBUG
  printf("[i] Random colormaps: %s\n", (s->auto_colormaps ? "on" : "off"));
#endif

  if (!s->auto_colormaps) {
    /* not auto*, get colormap name */
    char *cmap = xmlGetMandatoryString(doc, "colormap", sequence_node);
#ifdef XDEBUG
    printf("[i] Need colormap: '%s'\n", cmap);
#endif
    s->cmap_id = Colormaps_find(cmap);
    xfree(cmap);
  } else 
    s->cmap_id = Colormaps_random_id();
    
  /* then, get <auto_pictures> */
  sequence_node = sequence_node->next;
  sequence_node = xmlFindElement("auto_pictures", sequence_node);
  if (sequence_node == NULL) {
    printf("[!] Sequence %s: no <auto_pictures> found\n", file);
    goto error;
  }
  res = xmlGetOptionalLong(doc, sequence_node, &tmp);
  if (res == -1)
    s->auto_pictures = 0;
  else
    s->auto_pictures = (u_char)tmp;
  assert((s->auto_pictures == 0) || (s->auto_pictures == 1));
#ifdef XDEBUG
  printf("[i] Random pictures: %s\n", (s->auto_pictures ? "on" : "off"));
#endif

  if (!s->auto_pictures) {
    /* not auto*, get picture name */
    char *picture = xmlGetMandatoryString(doc, "picture", sequence_node);
#ifdef XDEBUG
    printf("[i] Need picture: '%s'\n", picture);
#endif
    if (pictures == NULL) {
      if (libbiniou_verbose)
	printf("[!] No pictures are loaded, won't find '%s'\n", picture);
      xfree(picture);
      goto error;
    } else {
      s->picture_id = Pictures_find(picture);
      if (s->picture_id == 0) {
	if (libbiniou_verbose)
	  printf("[!] Picture '%s' not found, using default\n", picture);
      }
      xfree(picture);
    }
  } else {
    if (pictures == NULL) {
      s->broken = 1;
      s->picture_id = -1;
      s->auto_pictures = 0;
    } else
      s->picture_id = Pictures_random_id(); //pictures);
  }

  /* now, get plugins */
  plugins_node = xmlFindElement("plugins", sequence_node);
  if (plugins_node == NULL) {
    printf("[!] Sequence %s: no <plugins> found\n", file);
    goto error;
  }
  plugins_node = plugins_node->xmlChildrenNode;
  if (plugins_node == NULL) {
    printf("[!] Sequence %s: no elements in <plugins>\n", file);
    goto error;
  }

  while (plugins_node != NULL) {
    u_char lens;
    Layer_t *layer;
    Plugin_t *p;

    assert(plugins_node->name != NULL);
    lens = !xmlStrcmp(plugins_node->name, (const xmlChar *)"lens");

#ifdef XDEBUG
    if (lens)
      printf("[i] Lens:");
    else
      printf("[i] Plugin:");
#endif

    youhou = xmlGetProp(plugins_node, (const xmlChar *)"id");
    tmp = getintfield(youhou);
    xmlFree(youhou);
    assert(tmp > 0);
    if (!tmp) {
#ifdef XDEBUG
      printf("\n");
#endif
      goto error;
    }
#ifdef XDEBUG
    printf(" %li\n", tmp);
#endif
    
    p = Plugins_find((u_long)tmp);
    if (p == NULL)
      goto error;

    if (!(*p->options & BEQ_DISABLED)) {
      enum LayerMode mode;

      layer = Layer_new(p);
      
      youhou = xmlGetProp(plugins_node, (const xmlChar *)"mode");
      if (youhou != NULL) {
	mode = LayerMode_from_string((const char *)youhou);
	xmlFree(youhou);
      } else
	mode = NORMAL;
      layer->mode = mode;
      
      s->layers = g_list_append(s->layers, (gpointer)layer);
      
      if (lens)
	s->lens = (Plugin_t *)p;
    }

    plugins_node = plugins_node->next;
  }

  /* Check sequence length */
  assert(Sequence_size(s) <= MAX_SEQ_LEN);
  
  /* Clean up */
  xmlFreeDoc(doc);
  xmlCleanupParser(); /* FIXME is this ok ? */

  *dot = '\0'; /* spr0tch */
  s->name = strdup(file);
#ifdef XDEBUG
  printf("[s] Sequence loaded as '%s'\n", s->name);
#endif

  return s;

 error:
  printf("[!] Failed to load sequence from file '%s'\n", file);

  /* Clean up */
  xmlFreeDoc(doc);
  xmlCleanupParser(); /* FIXME is this ok ? */

  Sequence_delete(s);

  return NULL;
}