]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - libdyn/dyn_insert.c
undo merge disaster
[1ts-debian.git] / libdyn / dyn_insert.c
1 /*
2  * This file is part of libdyn.a, the C Dynamic Object library.  It
3  * contains the source code for the function DynInsert().
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 "dynP.h"
14
15 int DynInsert(obj, idx, els, num)
16    DynObject obj;
17    DynPtr els;
18    int idx, num;
19 {
20      int ret;
21      
22      if (idx < 0 || idx > obj->num_el) {
23           if (obj->debug)
24                fprintf(stderr, "dyn: insert: index %d is not in [0,%d]\n",
25                        idx, obj->num_el);
26           return DYN_BADINDEX;
27      }
28
29      if (num < 1) {
30           if (obj->debug)
31                fprintf(stderr, "dyn: insert: cannot insert %d elements\n",
32                        num);
33           return DYN_BADVALUE;
34      }
35
36      if (obj->debug)
37           fprintf(stderr,"dyn: insert: Moving %d bytes from %p + %d to + %d\n",
38                   (obj->num_el-idx)*obj->el_size, obj->array,
39                   obj->el_size*idx, obj->el_size*(idx+num));
40
41      if ((ret = _DynResize(obj, obj->num_el + num)) != DYN_OK)
42           return ret;
43
44      (void) memmove(obj->array + (idx + num), obj->array + idx, 
45                     (obj->num_el-idx)*obj->el_size);
46
47      if (obj->debug)
48           fprintf(stderr, "dyn: insert: Copying %d bytes from %p to %p + %d\n",
49                   obj->el_size*num, els, obj->array, obj->el_size*idx);
50
51      (void) memmove(obj->array + obj->el_size*idx, els, obj->el_size*num);
52
53      obj->num_el += num;
54
55      if (obj->debug)
56           fprintf(stderr, "dyn: insert: done.\n");
57
58      return DYN_OK;
59 }