]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/libdyn/dyn_delete.c
r4260@bucket (orig r250): kcr | 2008-01-20 15:59:22 -0500
[1ts-debian.git] / zephyr / libdyn / dyn_delete.c
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
15 #include "dynP.h"
16
17 /*
18  * Checkers!  Get away from that "hard disk erase" button!
19  *    (Stupid dog.  He almost did it to me again ...)
20  */                                 
21 int
22 DynDelete(DynObject obj,
23           int idx)
24 {
25      if (idx < 0) {
26           if (obj->debug)
27                fprintf(stderr, "dyn: delete: bad index %d\n", idx);
28           return DYN_BADINDEX;
29      }
30      
31      if (idx >= obj->num_el) {
32           if (obj->debug)
33                fprintf(stderr, "dyn: delete: Highest index is %d.\n",
34                        obj->num_el);
35           return DYN_BADINDEX;
36      }
37
38      if (idx == obj->num_el-1) {
39           if (obj->paranoid) {
40                if (obj->debug)
41                     fprintf(stderr, "dyn: delete: last element, zeroing.\n");
42                (void) memset(obj->array + idx*obj->el_size, 0, obj->el_size);
43           }
44           else {
45                if (obj->debug)
46                     fprintf(stderr, "dyn: delete: last element, punting.\n");
47           }
48      }    
49      else {
50           if (obj->debug)
51                fprintf(stderr,
52                        "dyn: delete: copying %d bytes from %p + %d to + %d.\n",
53                        obj->el_size*(obj->num_el - idx), obj->array,
54                        (idx+1)*obj->el_size, idx*obj->el_size);
55           
56           (void) memmove(obj->array + idx*obj->el_size,
57                          obj->array + (idx+1)*obj->el_size,
58                          obj->el_size*(obj->num_el - idx));
59
60           if (obj->paranoid) {
61                if (obj->debug)
62                     fprintf(stderr,
63                             "dyn: delete: zeroing %d bytes from %p + %d\n",
64                             obj->el_size, obj->array,
65                             obj->el_size*(obj->num_el - 1));
66                (void) memset(obj->array + obj->el_size*(obj->num_el - 1),
67                              0, obj->el_size);
68           }
69      }
70      
71      --obj->num_el;
72      
73      if (obj->debug)
74           fprintf(stderr, "dyn: delete: done.\n");
75
76      return DYN_OK;
77 }