1*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 2*7c478bd9Sstevel@tonic-gate 3*7c478bd9Sstevel@tonic-gate /* 4*7c478bd9Sstevel@tonic-gate * This file is part of libdyn.a, the C Dynamic Object library. It 5*7c478bd9Sstevel@tonic-gate * contains the source code for the functions DynGet() and DynAdd(). 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * There are no restrictions on this code; however, if you make any 8*7c478bd9Sstevel@tonic-gate * changes, I request that you document them so that I do not get 9*7c478bd9Sstevel@tonic-gate * credit or blame for your modifications. 10*7c478bd9Sstevel@tonic-gate * 11*7c478bd9Sstevel@tonic-gate * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) 12*7c478bd9Sstevel@tonic-gate * and MIT-Project Athena, 1989. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #include <stdio.h> 16*7c478bd9Sstevel@tonic-gate #include <strings.h> 17*7c478bd9Sstevel@tonic-gate 18*7c478bd9Sstevel@tonic-gate #include "dynP.h" 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate DynPtr DynArray(obj) 21*7c478bd9Sstevel@tonic-gate DynObjectP obj; 22*7c478bd9Sstevel@tonic-gate { 23*7c478bd9Sstevel@tonic-gate if (obj->debug) 24*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: array: returning array pointer %d.\n", 25*7c478bd9Sstevel@tonic-gate obj->array); 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate return obj->array; 28*7c478bd9Sstevel@tonic-gate } 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate DynPtr DynGet(obj, num) 31*7c478bd9Sstevel@tonic-gate DynObjectP obj; 32*7c478bd9Sstevel@tonic-gate int num; 33*7c478bd9Sstevel@tonic-gate { 34*7c478bd9Sstevel@tonic-gate if (num < 0) { 35*7c478bd9Sstevel@tonic-gate if (obj->debug) 36*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: get: bad index %d\n", num); 37*7c478bd9Sstevel@tonic-gate return NULL; 38*7c478bd9Sstevel@tonic-gate } 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate if (num >= obj->num_el) { 41*7c478bd9Sstevel@tonic-gate if (obj->debug) 42*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: get: highest element is %d.\n", 43*7c478bd9Sstevel@tonic-gate obj->num_el); 44*7c478bd9Sstevel@tonic-gate return NULL; 45*7c478bd9Sstevel@tonic-gate } 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate if (obj->debug) 48*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: get: Returning address %d + %d.\n", 49*7c478bd9Sstevel@tonic-gate obj->array, obj->el_size*num); 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate return (DynPtr) obj->array + obj->el_size*num; 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate int DynAdd(obj, el) 55*7c478bd9Sstevel@tonic-gate DynObjectP obj; 56*7c478bd9Sstevel@tonic-gate void *el; 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate int ret; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate ret = DynPut(obj, el, obj->num_el); 61*7c478bd9Sstevel@tonic-gate if (ret != DYN_OK) 62*7c478bd9Sstevel@tonic-gate return ret; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate ++obj->num_el; 65*7c478bd9Sstevel@tonic-gate return ret; 66*7c478bd9Sstevel@tonic-gate } 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * WARNING! There is a reason this function is not documented in the 70*7c478bd9Sstevel@tonic-gate * man page. If DynPut used to mutate already existing elements, 71*7c478bd9Sstevel@tonic-gate * everything will go fine. If it is used to add new elements 72*7c478bd9Sstevel@tonic-gate * directly, however, the state within the object (such as 73*7c478bd9Sstevel@tonic-gate * obj->num_el) will not be updated properly and many other functions 74*7c478bd9Sstevel@tonic-gate * in the library will lose. Have a nice day. 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate int DynPut(obj, el_in, idx) 77*7c478bd9Sstevel@tonic-gate DynObjectP obj; 78*7c478bd9Sstevel@tonic-gate void *el_in; 79*7c478bd9Sstevel@tonic-gate int idx; 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate DynPtr el = (DynPtr) el_in; 82*7c478bd9Sstevel@tonic-gate int ret; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if (obj->debug) 85*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: put: Writing %d bytes from %d to %d + %d\n", 86*7c478bd9Sstevel@tonic-gate obj->el_size, el, obj->array, idx*obj->el_size); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate if ((ret = _DynResize(obj, idx)) != DYN_OK) 89*7c478bd9Sstevel@tonic-gate return ret; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMMOVE 92*7c478bd9Sstevel@tonic-gate memmove(obj->array + idx*obj->el_size, el, obj->el_size); 93*7c478bd9Sstevel@tonic-gate #else 94*7c478bd9Sstevel@tonic-gate bcopy(el, obj->array + idx*obj->el_size, obj->el_size); 95*7c478bd9Sstevel@tonic-gate #endif 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate if (obj->debug) 98*7c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: put: done.\n"); 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate return DYN_OK; 101*7c478bd9Sstevel@tonic-gate } 102