1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * This file is part of libdyn.a, the C Dynamic Object library. It
5*0Sstevel@tonic-gate * contains the source code for the function DynInsert().
6*0Sstevel@tonic-gate *
7*0Sstevel@tonic-gate * There are no restrictions on this code; however, if you make any
8*0Sstevel@tonic-gate * changes, I request that you document them so that I do not get
9*0Sstevel@tonic-gate * credit or blame for your modifications.
10*0Sstevel@tonic-gate *
11*0Sstevel@tonic-gate * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
12*0Sstevel@tonic-gate * and MIT-Project Athena, 1989.
13*0Sstevel@tonic-gate */
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate #include <stdio.h>
16*0Sstevel@tonic-gate #include <strings.h>
17*0Sstevel@tonic-gate #include "dynP.h"
18*0Sstevel@tonic-gate
DynInsert(obj,idx,els_in,num)19*0Sstevel@tonic-gate int DynInsert(obj, idx, els_in, num)
20*0Sstevel@tonic-gate DynObjectP obj;
21*0Sstevel@tonic-gate void *els_in;
22*0Sstevel@tonic-gate int idx, num;
23*0Sstevel@tonic-gate {
24*0Sstevel@tonic-gate DynPtr els = (DynPtr) els_in;
25*0Sstevel@tonic-gate int ret;
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate if (idx < 0 || idx > obj->num_el) {
28*0Sstevel@tonic-gate if (obj->debug)
29*0Sstevel@tonic-gate fprintf(stderr, "dyn: insert: index %d is not in [0,%d]\n",
30*0Sstevel@tonic-gate idx, obj->num_el);
31*0Sstevel@tonic-gate return DYN_BADINDEX;
32*0Sstevel@tonic-gate }
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate if (num < 1) {
35*0Sstevel@tonic-gate if (obj->debug)
36*0Sstevel@tonic-gate fprintf(stderr, "dyn: insert: cannot insert %d elements\n",
37*0Sstevel@tonic-gate num);
38*0Sstevel@tonic-gate return DYN_BADVALUE;
39*0Sstevel@tonic-gate }
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate if (obj->debug)
42*0Sstevel@tonic-gate fprintf(stderr,"dyn: insert: Moving %d bytes from %d + %d to + %d\n",
43*0Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size, obj->array,
44*0Sstevel@tonic-gate obj->el_size*idx, obj->el_size*(idx+num));
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate if ((ret = _DynResize(obj, obj->num_el + num)) != DYN_OK)
47*0Sstevel@tonic-gate return ret;
48*0Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
49*0Sstevel@tonic-gate memmove(obj->array + obj->el_size*(idx + num),
50*0Sstevel@tonic-gate obj->array + obj->el_size*idx,
51*0Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size);
52*0Sstevel@tonic-gate #else
53*0Sstevel@tonic-gate bcopy(obj->array + obj->el_size*idx,
54*0Sstevel@tonic-gate obj->array + obj->el_size*(idx + num),
55*0Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size);
56*0Sstevel@tonic-gate #endif
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate if (obj->debug)
59*0Sstevel@tonic-gate fprintf(stderr, "dyn: insert: Copying %d bytes from %d to %d + %d\n",
60*0Sstevel@tonic-gate obj->el_size*num, els, obj->array, obj->el_size*idx);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
63*0Sstevel@tonic-gate memmove(obj->array + obj->el_size*idx, els, obj->el_size*num);
64*0Sstevel@tonic-gate #else
65*0Sstevel@tonic-gate bcopy(els, obj->array + obj->el_size*idx, obj->el_size*num);
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate obj->num_el += num;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate if (obj->debug)
70*0Sstevel@tonic-gate fprintf(stderr, "dyn: insert: done.\n");
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate return DYN_OK;
73*0Sstevel@tonic-gate }
74