1 /*
2  * This file is part of libdyn.a, the C Dynamic Object library.  It
3  * contains the source code for the function DynDelete().
4  *
5  * There are no restrictions on this code; however, if you make any
6  * changes, I request that you document them so that I do not get
7  * credit or blame for your modifications.
8  *
9  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
10  * and MIT-Project Athena, 1989.
11  */
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include "dynP.h"
17 
DynDelete(obj,index)18 int DynDelete(obj, index)
19    DynObjectP obj;
20    int index;
21 {
22      if (index < 0) {
23 	  if (obj->debug)
24 	       fprintf(stderr, "dyn: delete: bad index %d\n", index);
25 	  return DYN_BADINDEX;
26      }
27 
28      if (index >= obj->num_el) {
29 	  if (obj->debug)
30 	       fprintf(stderr, "dyn: delete: Highest index is %d.\n",
31 		       obj->num_el);
32 	  return DYN_BADINDEX;
33      }
34 
35      if (index == obj->num_el-1) {
36 	  if (obj->paranoid) {
37 	       if (obj->debug)
38 		    fprintf(stderr, "dyn: delete: last element, zeroing.\n");
39 	       bzero(obj->array + index*obj->el_size, obj->el_size);
40 	  }
41 	  else {
42 	       if (obj->debug)
43 		    fprintf(stderr, "dyn: delete: last element, punting.\n");
44 	  }
45      }
46      else {
47 	  if (obj->debug)
48 	       fprintf(stderr,
49 		       "dyn: delete: copying %d bytes from %p + %d to + %d.\n",
50 		       obj->el_size*(obj->num_el - index), obj->array,
51 		       (index+1)*obj->el_size, index*obj->el_size);
52 
53 	  bcopy(obj->array + (index+1)*obj->el_size,
54 		obj->array + index*obj->el_size,
55 		obj->el_size*(obj->num_el - index));
56 
57 	  if (obj->paranoid) {
58 	       if (obj->debug)
59 		    fprintf(stderr,
60 			    "dyn: delete: zeroing %d bytes from %p + %d\n",
61 			    obj->el_size, obj->array,
62 			    obj->el_size*(obj->num_el - 1));
63 	       bzero(obj->array + obj->el_size*(obj->num_el - 1),
64 		     obj->el_size);
65 	  }
66      }
67 
68      --obj->num_el;
69 
70      if (obj->debug)
71 	  fprintf(stderr, "dyn: delete: done.\n");
72 
73      return DYN_OK;
74 }
75