17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * This file is part of libdyn.a, the C Dynamic Object library. It
37c478bd9Sstevel@tonic-gate * contains the source code for the function DynInsert().
4*1da57d55SToomas Soome *
57c478bd9Sstevel@tonic-gate * There are no restrictions on this code; however, if you make any
67c478bd9Sstevel@tonic-gate * changes, I request that you document them so that I do not get
77c478bd9Sstevel@tonic-gate * credit or blame for your modifications.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
107c478bd9Sstevel@tonic-gate * and MIT-Project Athena, 1989.
117c478bd9Sstevel@tonic-gate */
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate #include <stdio.h>
147c478bd9Sstevel@tonic-gate #include <strings.h>
157c478bd9Sstevel@tonic-gate #include "dynP.h"
167c478bd9Sstevel@tonic-gate
DynInsert(obj,idx,els_in,num)177c478bd9Sstevel@tonic-gate int DynInsert(obj, idx, els_in, num)
187c478bd9Sstevel@tonic-gate DynObjectP obj;
197c478bd9Sstevel@tonic-gate void *els_in;
207c478bd9Sstevel@tonic-gate int idx, num;
217c478bd9Sstevel@tonic-gate {
227c478bd9Sstevel@tonic-gate DynPtr els = (DynPtr) els_in;
237c478bd9Sstevel@tonic-gate int ret;
24*1da57d55SToomas Soome
257c478bd9Sstevel@tonic-gate if (idx < 0 || idx > obj->num_el) {
267c478bd9Sstevel@tonic-gate if (obj->debug)
277c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: insert: index %d is not in [0,%d]\n",
287c478bd9Sstevel@tonic-gate idx, obj->num_el);
297c478bd9Sstevel@tonic-gate return DYN_BADINDEX;
307c478bd9Sstevel@tonic-gate }
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate if (num < 1) {
337c478bd9Sstevel@tonic-gate if (obj->debug)
347c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: insert: cannot insert %d elements\n",
357c478bd9Sstevel@tonic-gate num);
367c478bd9Sstevel@tonic-gate return DYN_BADVALUE;
377c478bd9Sstevel@tonic-gate }
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate if (obj->debug)
407c478bd9Sstevel@tonic-gate fprintf(stderr,"dyn: insert: Moving %d bytes from %d + %d to + %d\n",
417c478bd9Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size, obj->array,
427c478bd9Sstevel@tonic-gate obj->el_size*idx, obj->el_size*(idx+num));
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate if ((ret = _DynResize(obj, obj->num_el + num)) != DYN_OK)
457c478bd9Sstevel@tonic-gate return ret;
467c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
477c478bd9Sstevel@tonic-gate memmove(obj->array + obj->el_size*(idx + num),
487c478bd9Sstevel@tonic-gate obj->array + obj->el_size*idx,
497c478bd9Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size);
507c478bd9Sstevel@tonic-gate #else
517c478bd9Sstevel@tonic-gate bcopy(obj->array + obj->el_size*idx,
52*1da57d55SToomas Soome obj->array + obj->el_size*(idx + num),
537c478bd9Sstevel@tonic-gate (obj->num_el-idx)*obj->el_size);
54*1da57d55SToomas Soome #endif
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate if (obj->debug)
577c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: insert: Copying %d bytes from %d to %d + %d\n",
587c478bd9Sstevel@tonic-gate obj->el_size*num, els, obj->array, obj->el_size*idx);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
617c478bd9Sstevel@tonic-gate memmove(obj->array + obj->el_size*idx, els, obj->el_size*num);
627c478bd9Sstevel@tonic-gate #else
637c478bd9Sstevel@tonic-gate bcopy(els, obj->array + obj->el_size*idx, obj->el_size*num);
64*1da57d55SToomas Soome #endif
657c478bd9Sstevel@tonic-gate obj->num_el += num;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if (obj->debug)
687c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: insert: done.\n");
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate return DYN_OK;
717c478bd9Sstevel@tonic-gate }
72