| 523 | */ |
| 524 | |
| 525 | int msInsertLayer(mapObj *map, layerObj *layer, int nIndex) |
| 526 | { |
| 527 | if (!layer) |
| 528 | { |
| 529 | msSetError(MS_CHILDERR, "Can't insert a NULL Layer", "msInsertLayer()"); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | /* Ensure there is room for a new layer */ |
| 534 | if (map->numlayers == map->maxlayers) |
| 535 | { |
| 536 | if (msGrowMapLayers(map) == NULL) |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | /* msGrowMapLayers allocates the new layer which we don't need to do since we have 1 that we are inserting |
| 541 | not sure if it is possible for this to be non null otherwise, but better to check since this function |
| 542 | replaces the value */ |
| 543 | if (map->layers[map->numlayers]!=NULL) |
| 544 | free(map->layers[map->numlayers]); |
| 545 | |
| 546 | /* Catch attempt to insert past end of layers array */ |
| 547 | if (nIndex >= map->numlayers) { |
| 548 | msSetError(MS_CHILDERR, "Cannot insert layer beyond index %d", |
| 549 | "msInsertLayer()", map->numlayers-1); |
| 550 | return -1; |
| 551 | } |
| 552 | else if (nIndex < 0) { /* Insert at the end by default */ |
| 553 | map->layerorder[map->numlayers] = map->numlayers; |
| 554 | GET_LAYER(map, map->numlayers) = layer; |
| 555 | GET_LAYER(map, map->numlayers)->index = map->numlayers; |
| 556 | GET_LAYER(map, map->numlayers)->map = map; |
| 557 | MS_REFCNT_INCR(layer); |
| 558 | map->numlayers++; |
| 559 | return map->numlayers-1; |
| 560 | } |
| 561 | else if (nIndex >= 0 && nIndex < map->numlayers) { |
| 562 | /* Move existing layers at the specified nIndex or greater */ |
| 563 | /* to an index one higher */ |
| 564 | int i; |
| 565 | for (i=map->numlayers; i>nIndex; i--) { |
| 566 | GET_LAYER(map, i)=GET_LAYER(map, i-1); |
| 567 | GET_LAYER(map, i)->index = i; |
| 568 | } |
| 569 | |
| 570 | /* assign new layer to specified index */ |
| 571 | GET_LAYER(map, nIndex)=layer; |
| 572 | GET_LAYER(map, nIndex)->index = nIndex; |
| 573 | GET_LAYER(map, nIndex)->map = map; |
| 574 | |
| 575 | /* adjust layers drawing order */ |
| 576 | for (i=map->numlayers; i>nIndex; i--) { |
| 577 | map->layerorder[i] = map->layerorder[i-1]; |
| 578 | if (map->layerorder[i] >= nIndex) map->layerorder[i]++; |
| 579 | } |
| 580 | for (i=0; i<nIndex; i++) { |
| 581 | if (map->layerorder[i] >= nIndex) map->layerorder[i]++; |
| 582 | } |
no test coverage detected