[go: up one dir, main page]

File: path.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (358 lines) | stat: -rw-r--r-- 8,693 bytes parent folder | download | duplicates (2)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**************************************************************************\
 *
 *  This file is part of the Coin 3D visualization library.
 *  Copyright (C) by Kongsberg Oil & Gas Technologies.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  ("GPL") version 2 as published by the Free Software Foundation.
 *  See the file LICENSE.GPL at the root directory of this source
 *  distribution for additional information about the GNU GPL.
 *
 *  For using Coin with software that can not be combined with the GNU
 *  GPL, and for taking advantage of the additional benefits of our
 *  support services, please contact Kongsberg Oil & Gas Technologies
 *  about acquiring a Coin Professional Edition License.
 *
 *  See http://www.coin3d.org/ for more information.
 *
 *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
 *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
 *
\**************************************************************************/

#include <Inventor/C/XML/path.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <Inventor/C/base/string.h>
#include <Inventor/C/XML/element.h>
#include "utils.h"

/* ********************************************************************** */

struct cc_xml_path {
  struct path_node * head;
};

/* ********************************************************************** */

struct path_node {
  char * element;
  int idx;
  struct path_node * next;
  // make list doubly-linked for match_p use?
};

static
path_node *
path_node_new(const char * element, int idx)
{
  path_node * node = new path_node;
  node->element = cc_xml_strdup(element);
  node->idx = idx;
  node->next = NULL;
  return node;
}

static
path_node *
path_node_clone(path_node * node)
{
  return path_node_new(node->element, node->idx);
}

static
path_node *
path_node_delete(path_node * node)
{
  path_node * next;
  next = node->next;
  delete[] node->element;
  delete node;
  return next;
}

static
void
path_node_delete_chain(path_node * head)
{
  while ( head != NULL ) {
    path_node * prev = head;
    head = head->next;
    delete[] prev->element;
    delete prev;
  }
}

static
int
path_node_match_p(path_node * node, const cc_xml_elt * elt)
{
  if ( strcmp(node->element, cc_xml_elt_get_type(elt)) != 0 ) return FALSE;
  if ( node->idx != -1 ) {
    cc_xml_elt * parent = cc_xml_elt_get_parent(elt);
    if ( parent == NULL ) return (node->idx == 0) ? TRUE : FALSE;
    int i = cc_xml_elt_get_child_type_index(parent, elt);
    return (i == node->idx) ? TRUE : FALSE;
  }
  return TRUE;
}

/* ********************************************************************** */

cc_xml_path *
cc_xml_path_new(void)
{
  cc_xml_path * path = new cc_xml_path;
  path->head = NULL;
  assert(path);
  return path;
} // cc_xml_path_new()

void
cc_xml_path_delete_x(cc_xml_path * path)
{
  assert(path);
  path_node_delete_chain(path->head);
  delete path;
} // cc_xml_path_delete_x()

void
cc_xml_path_clear_x(cc_xml_path * path)
{
  assert(path);
  path_node_delete_chain(path->head);
  path->head = NULL;
} // cc_xml_path_clear_x()

static
void
cc_xml_path_set_x_va(cc_xml_path * path, va_list args)
{
  path_node_delete_chain(path->head);
  path->head = NULL;
  char * arg;
  while ( (arg = va_arg(args, char *)) != NULL ) {
    char * ptr;
    if ( (ptr = strchr(arg, '[')) == NULL ) {
      cc_xml_path_prepend_x(path, arg, -1);
    } else {
      int idx = atoi(ptr + 1);
      assert(idx >= -1);
      char * str = cc_xml_strndup(arg, static_cast<int>(ptr - arg));
      cc_xml_path_prepend_x(path, str, idx);
      delete [] str;
    }
  }
  cc_xml_path_reverse_x(path);
} // cc_xml_path_set_x_va()

void
cc_xml_path_copy_x(cc_xml_path * path, cc_xml_path * path2)
{
  assert(path && path2);
  cc_xml_path_clear_x(path);
  path_node * p1node;
  path_node * p1prev = NULL;
  path_node * p2node = path2->head;
  while ( p2node != NULL ) {
    p1node = path_node_clone(p2node);
    if ( p1prev == NULL ) path->head = p1node;
    else p1prev->next = p1node;
    p1prev = p1node;
  }
} // cc_xml_path_copy_x()

void
cc_xml_path_set_x(cc_xml_path * path, ...)
{
  assert(path);
  va_list args;
  va_start(args, path);
  cc_xml_path_set_x_va(path, args);
  va_end(args);
} // cc_xml_path_set_x()

void
cc_xml_path_reverse_x(cc_xml_path * path)
{
  assert(path);
  struct path_node * prev = NULL;
  struct path_node * node = path->head;
  while ( node != NULL ) {
    struct path_node * next = node->next;
    node->next = prev;
    prev = node;
    node = next;
  }
  path->head = prev;
} // cc_xml_path_reverse_x()

int
cc_xml_path_get_length(const cc_xml_path * path)
{
  assert(path);
  int len = 0;
  struct path_node * node = path->head;
  while ( node != NULL ) {
    assert(len < 100);
    len++;
    node = node->next;
  }
  return len;
} // cc_xml_path_get_length()

const char *
cc_xml_path_get_type(const cc_xml_path * path, int idx)
{
  assert(path && path->head && idx >= 0);
  struct path_node * node = path->head;
  int i;
  for ( i = 0; i < idx; i++ ) {
    assert(node->next != NULL);
    node = node->next;
  }
  return node->element;
} // cc_xml_path_get_type()

int
cc_xml_path_get_index(const cc_xml_path * path, int idx)
{
  assert(path && path->head && idx >= 0);
  struct path_node * node = path->head;
  int i;
  for ( i = 0; i < idx; i++ ) {
    assert(node->next != NULL);
    node = node->next;
  }
  return node->idx;
} // cc_xml_path_get_index()

int
cc_xml_path_match_p(const cc_xml_path * path, const cc_xml_elt * elt)
{
  // consider using a doubly-linked list instead of continous head->tail traversal
  int length = cc_xml_path_get_length(path);
  struct path_node * head = path->head;
  int i, j;
  for ( i = length - 1; i >= 0; i-- ) {
    struct path_node * node = head;
    for ( j = 0; j < i; j++ ) {
      node = node->next;
      assert(node);
    }
    if ( !path_node_match_p(node, elt) ) return FALSE;
    elt = cc_xml_elt_get_parent(elt);
    assert(elt);
  }
  return TRUE;
} // cc_xml_path_match_p()

void
cc_xml_path_append_x(cc_xml_path * path, const char * elt, int idx)
{
  assert(path);
  struct path_node * node = path->head;
  if ( node == NULL ) {
    path->head = path_node_new(elt, idx);
  } else {
    while ( node->next != NULL )
      node = node->next;
    node->next = path_node_new(elt, idx);
  }
} // cc_xml_path_append_x()

void
cc_xml_path_append_path_x(cc_xml_path * path, cc_xml_path * path2)
{
  assert(path && path2);
  struct path_node * p1node = path->head;
  struct path_node * p1prev = NULL;
  if ( p1node != NULL ) {
    while ( p1node->next != NULL ) {
      p1prev = p1node;
      p1node = p1node->next;
    }
  }
  struct path_node * p2node = path2->head;
  while ( p2node != NULL ) {
    p1node = path_node_clone(p2node);
    if ( p1prev == NULL ) path->head = p1node;
    else p1prev->next = p1node;
    p1prev = p1node;
  }
} // cc_xml_path_append_path_x()

void
cc_xml_path_prepend_x(cc_xml_path * path, const char * elt, int idx)
{
  assert(path);
  struct path_node * node = path->head;
  path->head = path_node_new(elt, idx);
  path->head->next = node;
} // cc_xml_path_prepend_x()

void
cc_xml_path_prepend_path_x(cc_xml_path * path, cc_xml_path * path2)
{
  assert(path && path2);
  struct path_node * p1node = NULL;
  struct path_node * p1head = NULL;
  struct path_node * p1tail = NULL;
  struct path_node * p2node = path2->head;
  while ( p2node != NULL ) {
    p1tail = path_node_clone(p2node);
    if ( p1head == NULL ) p1head = p1tail;
    else p1node->next = p1tail;
    p1node = p1tail;
  }
  if ( p1tail != NULL ) {
    p1tail->next = path->head;
    path->head = p1head;
  }
} // cc_xml_path_prepend_path_x()

void
cc_xml_path_truncate_x(cc_xml_path * path, int length)
{
  assert(path);
  int len = 0;
  struct path_node * node = path->head;
  while ( (node != NULL) && (len < length) ) {
    len++;
    node = node->next;
  }
  if ( node ) {
    path_node_delete_chain(node->next);
    node->next = NULL;
  }
} // cc_xml_path_truncate_x()

/* ********************************************************************** */

void
cc_xml_path_dump(cc_xml_path * path)
{
  assert(path);
  struct path_node * node = path->head;
  while ( node != NULL ) {
    if ( node != path->head )
      fprintf(stderr, ".");
    fprintf(stderr, "%s", node->element);
    if ( node->idx != -1 ) {
      fprintf(stderr, "[%d]", node->idx);
    }
    node = node->next;
  }
  fprintf(stderr, "\n");
}

/* ********************************************************************** */