geom_xml2tree.c (126748) | geom_xml2tree.c (126786) |
---|---|
1/*- 2 * Copyright (c) 2003 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * | 1/*- 2 * Copyright (c) 2003 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * |
29 * $FreeBSD: head/lib/libgeom/geom_xml2tree.c 126748 2004-03-08 16:37:08Z phk $ | 29 * $FreeBSD: head/lib/libgeom/geom_xml2tree.c 126786 2004-03-09 21:14:18Z jhb $ |
30 */ 31 32#include <stdio.h> 33#include <inttypes.h> 34#include <stdlib.h> 35#include <string.h> 36#include <unistd.h> 37#include <errno.h> --- 27 unchanged lines hidden (view full) --- 65 void *id; 66 void *ref; 67 int i; 68 69 mt = userData; 70 mt->level++; 71 mt->sbuf[mt->level] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 72 id = NULL; | 30 */ 31 32#include <stdio.h> 33#include <inttypes.h> 34#include <stdlib.h> 35#include <string.h> 36#include <unistd.h> 37#include <errno.h> --- 27 unchanged lines hidden (view full) --- 65 void *id; 66 void *ref; 67 int i; 68 69 mt = userData; 70 mt->level++; 71 mt->sbuf[mt->level] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 72 id = NULL; |
73 ref = NULL; |
|
73 for (i = 0; attr[i] != NULL; i += 2) { 74 if (!strcmp(attr[i], "id")) { 75 id = (void *)strtoul(attr[i + 1], NULL, 0); 76 mt->nident++; 77 } else if (!strcmp(attr[i], "ref")) { 78 ref = (void *)strtoul(attr[i + 1], NULL, 0); 79 } else 80 printf("%*.*s[%s = %s]\n", 81 mt->level + 1, mt->level + 1, "", 82 attr[i], attr[i + 1]); 83 } 84 if (!strcmp(name, "class") && mt->class == NULL) { 85 mt->class = calloc(1, sizeof *mt->class); | 74 for (i = 0; attr[i] != NULL; i += 2) { 75 if (!strcmp(attr[i], "id")) { 76 id = (void *)strtoul(attr[i + 1], NULL, 0); 77 mt->nident++; 78 } else if (!strcmp(attr[i], "ref")) { 79 ref = (void *)strtoul(attr[i + 1], NULL, 0); 80 } else 81 printf("%*.*s[%s = %s]\n", 82 mt->level + 1, mt->level + 1, "", 83 attr[i], attr[i + 1]); 84 } 85 if (!strcmp(name, "class") && mt->class == NULL) { 86 mt->class = calloc(1, sizeof *mt->class); |
86 mt->class->id = id; 87 LIST_INSERT_HEAD(&mt->mesh->class, mt->class, class); 88 LIST_INIT(&mt->class->geom); 89 LIST_INIT(&mt->class->config); | 87 mt->class->lg_id = id; 88 LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class); 89 LIST_INIT(&mt->class->lg_geom); 90 LIST_INIT(&mt->class->lg_config); |
90 return; 91 } 92 if (!strcmp(name, "geom") && mt->geom == NULL) { 93 mt->geom = calloc(1, sizeof *mt->geom); | 91 return; 92 } 93 if (!strcmp(name, "geom") && mt->geom == NULL) { 94 mt->geom = calloc(1, sizeof *mt->geom); |
94 mt->geom->id = id; 95 LIST_INSERT_HEAD(&mt->class->geom, mt->geom, geom); 96 LIST_INIT(&mt->geom->provider); 97 LIST_INIT(&mt->geom->consumer); 98 LIST_INIT(&mt->geom->config); | 95 mt->geom->lg_id = id; 96 LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom); 97 LIST_INIT(&mt->geom->lg_provider); 98 LIST_INIT(&mt->geom->lg_consumer); 99 LIST_INIT(&mt->geom->lg_config); |
99 return; 100 } 101 if (!strcmp(name, "class") && mt->geom != NULL) { | 100 return; 101 } 102 if (!strcmp(name, "class") && mt->geom != NULL) { |
102 mt->geom->class = ref; | 103 mt->geom->lg_class = ref; |
103 return; 104 } 105 if (!strcmp(name, "consumer") && mt->consumer == NULL) { 106 mt->consumer = calloc(1, sizeof *mt->consumer); | 104 return; 105 } 106 if (!strcmp(name, "consumer") && mt->consumer == NULL) { 107 mt->consumer = calloc(1, sizeof *mt->consumer); |
107 mt->consumer->id = id; 108 LIST_INSERT_HEAD(&mt->geom->consumer, mt->consumer, consumer); 109 LIST_INIT(&mt->consumer->config); | 108 mt->consumer->lg_id = id; 109 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer, 110 lg_consumer); 111 LIST_INIT(&mt->consumer->lg_config); |
110 return; 111 } 112 if (!strcmp(name, "geom") && mt->consumer != NULL) { | 112 return; 113 } 114 if (!strcmp(name, "geom") && mt->consumer != NULL) { |
113 mt->consumer->geom = ref; | 115 mt->consumer->lg_geom = ref; |
114 return; 115 } 116 if (!strcmp(name, "provider") && mt->consumer != NULL) { | 116 return; 117 } 118 if (!strcmp(name, "provider") && mt->consumer != NULL) { |
117 mt->consumer->provider = ref; | 119 mt->consumer->lg_provider = ref; |
118 return; 119 } 120 if (!strcmp(name, "provider") && mt->provider == NULL) { 121 mt->provider = calloc(1, sizeof *mt->provider); | 120 return; 121 } 122 if (!strcmp(name, "provider") && mt->provider == NULL) { 123 mt->provider = calloc(1, sizeof *mt->provider); |
122 mt->provider->id = id; 123 LIST_INSERT_HEAD(&mt->geom->provider, mt->provider, provider); 124 LIST_INIT(&mt->provider->consumers); 125 LIST_INIT(&mt->provider->config); | 124 mt->provider->lg_id = id; 125 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider, 126 lg_provider); 127 LIST_INIT(&mt->provider->lg_consumers); 128 LIST_INIT(&mt->provider->lg_config); |
126 return; 127 } 128 if (!strcmp(name, "geom") && mt->provider != NULL) { | 129 return; 130 } 131 if (!strcmp(name, "geom") && mt->provider != NULL) { |
129 mt->provider->geom = ref; | 132 mt->provider->lg_geom = ref; |
130 return; 131 } 132 if (!strcmp(name, "config")) { 133 if (mt->provider != NULL) { | 133 return; 134 } 135 if (!strcmp(name, "config")) { 136 if (mt->provider != NULL) { |
134 mt->config = &mt->provider->config; | 137 mt->config = &mt->provider->lg_config; |
135 return; 136 } 137 if (mt->consumer != NULL) { | 138 return; 139 } 140 if (mt->consumer != NULL) { |
138 mt->config = &mt->consumer->config; | 141 mt->config = &mt->consumer->lg_config; |
139 return; 140 } 141 if (mt->geom != NULL) { | 142 return; 143 } 144 if (mt->geom != NULL) { |
142 mt->config = &mt->geom->config; | 145 mt->config = &mt->geom->lg_config; |
143 return; 144 } 145 if (mt->class != NULL) { | 146 return; 147 } 148 if (mt->class != NULL) { |
146 mt->config = &mt->class->config; | 149 mt->config = &mt->class->lg_config; |
147 return; 148 } 149 } 150} 151 152static void 153EndElement(void *userData, const char *name) 154{ --- 9 unchanged lines hidden (view full) --- 164 mt->level--; 165 if (strlen(p) == 0) { 166 free(p); 167 p = NULL; 168 } 169 170 if (!strcmp(name, "name")) { 171 if (mt->provider != NULL) { | 150 return; 151 } 152 } 153} 154 155static void 156EndElement(void *userData, const char *name) 157{ --- 9 unchanged lines hidden (view full) --- 167 mt->level--; 168 if (strlen(p) == 0) { 169 free(p); 170 p = NULL; 171 } 172 173 if (!strcmp(name, "name")) { 174 if (mt->provider != NULL) { |
172 mt->provider->name = p; | 175 mt->provider->lg_name = p; |
173 return; 174 } else if (mt->geom != NULL) { | 176 return; 177 } else if (mt->geom != NULL) { |
175 mt->geom->name = p; | 178 mt->geom->lg_name = p; |
176 return; 177 } else if (mt->class != NULL) { | 179 return; 180 } else if (mt->class != NULL) { |
178 mt->class->name = p; | 181 mt->class->lg_name = p; |
179 return; 180 } 181 } 182 if (!strcmp(name, "rank") && mt->geom != NULL) { | 182 return; 183 } 184 } 185 if (!strcmp(name, "rank") && mt->geom != NULL) { |
183 mt->geom->rank = strtoul(p, NULL, 0); | 186 mt->geom->lg_rank = strtoul(p, NULL, 0); |
184 free(p); 185 return; 186 } 187 if (!strcmp(name, "mode") && mt->provider != NULL) { | 187 free(p); 188 return; 189 } 190 if (!strcmp(name, "mode") && mt->provider != NULL) { |
188 mt->provider->mode = p; | 191 mt->provider->lg_mode = p; |
189 return; 190 } 191 if (!strcmp(name, "mode") && mt->consumer != NULL) { | 192 return; 193 } 194 if (!strcmp(name, "mode") && mt->consumer != NULL) { |
192 mt->consumer->mode = p; | 195 mt->consumer->lg_mode = p; |
193 return; 194 } 195 if (!strcmp(name, "mediasize") && mt->provider != NULL) { | 196 return; 197 } 198 if (!strcmp(name, "mediasize") && mt->provider != NULL) { |
196 mt->provider->mediasize = strtoumax(p, NULL, 0); | 199 mt->provider->lg_mediasize = strtoumax(p, NULL, 0); |
197 free(p); 198 return; 199 } 200 if (!strcmp(name, "sectorsize") && mt->provider != NULL) { | 200 free(p); 201 return; 202 } 203 if (!strcmp(name, "sectorsize") && mt->provider != NULL) { |
201 mt->provider->sectorsize = strtoul(p, NULL, 0); | 204 mt->provider->lg_sectorsize = strtoul(p, NULL, 0); |
202 free(p); 203 return; 204 } 205 206 if (!strcmp(name, "config")) { 207 mt->config = NULL; 208 return; 209 } 210 211 if (mt->config != NULL) { 212 gc = calloc(sizeof *gc, 1); | 205 free(p); 206 return; 207 } 208 209 if (!strcmp(name, "config")) { 210 mt->config = NULL; 211 return; 212 } 213 214 if (mt->config != NULL) { 215 gc = calloc(sizeof *gc, 1); |
213 gc->name = strdup(name); 214 gc->val = p; 215 LIST_INSERT_HEAD(mt->config, gc, config); | 216 gc->lg_name = strdup(name); 217 gc->lg_val = p; 218 LIST_INSERT_HEAD(mt->config, gc, lg_config); |
216 return; 217 } 218 219 if (p != NULL) { 220 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p); 221 free(p); 222 } 223 --- 42 unchanged lines hidden (view full) --- 266 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1); 267} 268 269struct gident * 270geom_lookupid(struct gmesh *gmp, const void *id) 271{ 272 struct gident *gip; 273 | 219 return; 220 } 221 222 if (p != NULL) { 223 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p); 224 free(p); 225 } 226 --- 42 unchanged lines hidden (view full) --- 269 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1); 270} 271 272struct gident * 273geom_lookupid(struct gmesh *gmp, const void *id) 274{ 275 struct gident *gip; 276 |
274 for (gip = gmp->ident; gip->id != NULL; gip++) 275 if (gip->id == id) | 277 for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++) 278 if (gip->lg_id == id) |
276 return (gip); 277 return (NULL); 278} 279 280int 281geom_xml2tree(struct gmesh *gmp, char *p) 282{ 283 XML_Parser parser; 284 struct mystate *mt; 285 struct gclass *cl; 286 struct ggeom *ge; 287 struct gprovider *pr; 288 struct gconsumer *co; 289 int i; 290 291 memset(gmp, 0, sizeof *gmp); | 279 return (gip); 280 return (NULL); 281} 282 283int 284geom_xml2tree(struct gmesh *gmp, char *p) 285{ 286 XML_Parser parser; 287 struct mystate *mt; 288 struct gclass *cl; 289 struct ggeom *ge; 290 struct gprovider *pr; 291 struct gconsumer *co; 292 int i; 293 294 memset(gmp, 0, sizeof *gmp); |
292 LIST_INIT(&gmp->class); | 295 LIST_INIT(&gmp->lg_class); |
293 parser = XML_ParserCreate(NULL); 294 mt = calloc(1, sizeof *mt); 295 if (mt == NULL) 296 return (ENOMEM); 297 mt->mesh = gmp; 298 XML_SetUserData(parser, mt); 299 XML_SetElementHandler(parser, StartElement, EndElement); 300 XML_SetCharacterDataHandler(parser, CharData); 301 i = XML_Parse(parser, p, strlen(p), 1); 302 if (i != 1) 303 return (-1); 304 XML_ParserFree(parser); | 296 parser = XML_ParserCreate(NULL); 297 mt = calloc(1, sizeof *mt); 298 if (mt == NULL) 299 return (ENOMEM); 300 mt->mesh = gmp; 301 XML_SetUserData(parser, mt); 302 XML_SetElementHandler(parser, StartElement, EndElement); 303 XML_SetCharacterDataHandler(parser, CharData); 304 i = XML_Parse(parser, p, strlen(p), 1); 305 if (i != 1) 306 return (-1); 307 XML_ParserFree(parser); |
305 gmp->ident = calloc(sizeof *gmp->ident, mt->nident + 1); 306 if (gmp->ident == NULL) | 308 gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1); 309 if (gmp->lg_ident == NULL) |
307 return (ENOMEM); 308 free(mt); 309 i = 0; 310 /* Collect all identifiers */ | 310 return (ENOMEM); 311 free(mt); 312 i = 0; 313 /* Collect all identifiers */ |
311 LIST_FOREACH(cl, &gmp->class, class) { 312 gmp->ident[i].id = cl->id; 313 gmp->ident[i].ptr = cl; 314 gmp->ident[i].what = ISCLASS; | 314 LIST_FOREACH(cl, &gmp->lg_class, lg_class) { 315 gmp->lg_ident[i].lg_id = cl->lg_id; 316 gmp->lg_ident[i].lg_ptr = cl; 317 gmp->lg_ident[i].lg_what = ISCLASS; |
315 i++; | 318 i++; |
316 LIST_FOREACH(ge, &cl->geom, geom) { 317 gmp->ident[i].id = ge->id; 318 gmp->ident[i].ptr = ge; 319 gmp->ident[i].what = ISGEOM; | 319 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) { 320 gmp->lg_ident[i].lg_id = ge->lg_id; 321 gmp->lg_ident[i].lg_ptr = ge; 322 gmp->lg_ident[i].lg_what = ISGEOM; |
320 i++; | 323 i++; |
321 LIST_FOREACH(pr, &ge->provider, provider) { 322 gmp->ident[i].id = pr->id; 323 gmp->ident[i].ptr = pr; 324 gmp->ident[i].what = ISPROVIDER; | 324 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) { 325 gmp->lg_ident[i].lg_id = pr->lg_id; 326 gmp->lg_ident[i].lg_ptr = pr; 327 gmp->lg_ident[i].lg_what = ISPROVIDER; |
325 i++; 326 } | 328 i++; 329 } |
327 LIST_FOREACH(co, &ge->consumer, consumer) { 328 gmp->ident[i].id = co->id; 329 gmp->ident[i].ptr = co; 330 gmp->ident[i].what = ISCONSUMER; | 330 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) { 331 gmp->lg_ident[i].lg_id = co->lg_id; 332 gmp->lg_ident[i].lg_ptr = co; 333 gmp->lg_ident[i].lg_what = ISCONSUMER; |
331 i++; 332 } 333 } 334 } 335 /* Substitute all identifiers */ | 334 i++; 335 } 336 } 337 } 338 /* Substitute all identifiers */ |
336 LIST_FOREACH(cl, &gmp->class, class) { 337 LIST_FOREACH(ge, &cl->geom, geom) { 338 ge->class = geom_lookupid(gmp, ge->class)->ptr; 339 LIST_FOREACH(pr, &ge->provider, provider) { 340 pr->geom = geom_lookupid(gmp, pr->geom)->ptr; | 339 LIST_FOREACH(cl, &gmp->lg_class, lg_class) { 340 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) { 341 ge->lg_class = 342 geom_lookupid(gmp, ge->lg_class)->lg_ptr; 343 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) { 344 pr->lg_geom = 345 geom_lookupid(gmp, pr->lg_geom)->lg_ptr; |
341 } | 346 } |
342 LIST_FOREACH(co, &ge->consumer, consumer) { 343 co->geom = geom_lookupid(gmp, co->geom)->ptr; 344 if (co->provider != NULL) { 345 co->provider = 346 geom_lookupid(gmp, co->provider)->ptr; | 347 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) { 348 co->lg_geom = 349 geom_lookupid(gmp, co->lg_geom)->lg_ptr; 350 if (co->lg_provider != NULL) { 351 co->lg_provider = 352 geom_lookupid(gmp, 353 co->lg_provider)->lg_ptr; |
347 LIST_INSERT_HEAD( | 354 LIST_INSERT_HEAD( |
348 &co->provider->consumers, 349 co, consumers); | 355 &co->lg_provider->lg_consumers, 356 co, lg_consumers); |
350 } 351 } 352 } 353 } 354 return (0); 355} 356 357int --- 12 unchanged lines hidden (view full) --- 370delete_config(struct gconf *gp) 371{ 372 struct gconfig *cf; 373 374 for (;;) { 375 cf = LIST_FIRST(gp); 376 if (cf == NULL) 377 return; | 357 } 358 } 359 } 360 } 361 return (0); 362} 363 364int --- 12 unchanged lines hidden (view full) --- 377delete_config(struct gconf *gp) 378{ 379 struct gconfig *cf; 380 381 for (;;) { 382 cf = LIST_FIRST(gp); 383 if (cf == NULL) 384 return; |
378 LIST_REMOVE(cf, config); 379 free(cf->name); 380 free(cf->val); | 385 LIST_REMOVE(cf, lg_config); 386 free(cf->lg_name); 387 free(cf->lg_val); |
381 free(cf); 382 } 383} 384 385void 386geom_deletetree(struct gmesh *gmp) 387{ 388 struct gclass *cl; 389 struct ggeom *ge; 390 struct gprovider *pr; 391 struct gconsumer *co; 392 | 388 free(cf); 389 } 390} 391 392void 393geom_deletetree(struct gmesh *gmp) 394{ 395 struct gclass *cl; 396 struct ggeom *ge; 397 struct gprovider *pr; 398 struct gconsumer *co; 399 |
393 free(gmp->ident); 394 gmp->ident = NULL; | 400 free(gmp->lg_ident); 401 gmp->lg_ident = NULL; |
395 for (;;) { | 402 for (;;) { |
396 cl = LIST_FIRST(&gmp->class); | 403 cl = LIST_FIRST(&gmp->lg_class); |
397 if (cl == NULL) 398 break; | 404 if (cl == NULL) 405 break; |
399 LIST_REMOVE(cl, class); 400 delete_config(&cl->config); 401 if (cl->name) free(cl->name); | 406 LIST_REMOVE(cl, lg_class); 407 delete_config(&cl->lg_config); 408 if (cl->lg_name) free(cl->lg_name); |
402 for (;;) { | 409 for (;;) { |
403 ge = LIST_FIRST(&cl->geom); | 410 ge = LIST_FIRST(&cl->lg_geom); |
404 if (ge == NULL) 405 break; | 411 if (ge == NULL) 412 break; |
406 LIST_REMOVE(ge, geom); 407 delete_config(&ge->config); 408 if (ge->name) free(ge->name); | 413 LIST_REMOVE(ge, lg_geom); 414 delete_config(&ge->lg_config); 415 if (ge->lg_name) free(ge->lg_name); |
409 for (;;) { | 416 for (;;) { |
410 pr = LIST_FIRST(&ge->provider); | 417 pr = LIST_FIRST(&ge->lg_provider); |
411 if (pr == NULL) 412 break; | 418 if (pr == NULL) 419 break; |
413 LIST_REMOVE(pr, provider); 414 delete_config(&pr->config); 415 if (pr->name) free(pr->name); 416 if (pr->mode) free(pr->mode); | 420 LIST_REMOVE(pr, lg_provider); 421 delete_config(&pr->lg_config); 422 if (pr->lg_name) free(pr->lg_name); 423 if (pr->lg_mode) free(pr->lg_mode); |
417 free(pr); 418 } 419 for (;;) { | 424 free(pr); 425 } 426 for (;;) { |
420 co = LIST_FIRST(&ge->consumer); | 427 co = LIST_FIRST(&ge->lg_consumer); |
421 if (co == NULL) 422 break; | 428 if (co == NULL) 429 break; |
423 LIST_REMOVE(co, consumer); 424 delete_config(&co->config); 425 if (co->mode) free(co->mode); | 430 LIST_REMOVE(co, lg_consumer); 431 delete_config(&co->lg_config); 432 if (co->lg_mode) free(co->lg_mode); |
426 free(co); 427 } 428 free(ge); 429 } 430 free(cl); 431 } 432} | 433 free(co); 434 } 435 free(ge); 436 } 437 free(cl); 438 } 439} |