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 functions DynGet() and DynAdd().
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
18*0Sstevel@tonic-gate #include "dynP.h"
19*0Sstevel@tonic-gate
DynArray(obj)20*0Sstevel@tonic-gate DynPtr DynArray(obj)
21*0Sstevel@tonic-gate DynObjectP obj;
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate if (obj->debug)
24*0Sstevel@tonic-gate fprintf(stderr, "dyn: array: returning array pointer %d.\n",
25*0Sstevel@tonic-gate obj->array);
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate return obj->array;
28*0Sstevel@tonic-gate }
29*0Sstevel@tonic-gate
DynGet(obj,num)30*0Sstevel@tonic-gate DynPtr DynGet(obj, num)
31*0Sstevel@tonic-gate DynObjectP obj;
32*0Sstevel@tonic-gate int num;
33*0Sstevel@tonic-gate {
34*0Sstevel@tonic-gate if (num < 0) {
35*0Sstevel@tonic-gate if (obj->debug)
36*0Sstevel@tonic-gate fprintf(stderr, "dyn: get: bad index %d\n", num);
37*0Sstevel@tonic-gate return NULL;
38*0Sstevel@tonic-gate }
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate if (num >= obj->num_el) {
41*0Sstevel@tonic-gate if (obj->debug)
42*0Sstevel@tonic-gate fprintf(stderr, "dyn: get: highest element is %d.\n",
43*0Sstevel@tonic-gate obj->num_el);
44*0Sstevel@tonic-gate return NULL;
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate if (obj->debug)
48*0Sstevel@tonic-gate fprintf(stderr, "dyn: get: Returning address %d + %d.\n",
49*0Sstevel@tonic-gate obj->array, obj->el_size*num);
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate return (DynPtr) obj->array + obj->el_size*num;
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate
DynAdd(obj,el)54*0Sstevel@tonic-gate int DynAdd(obj, el)
55*0Sstevel@tonic-gate DynObjectP obj;
56*0Sstevel@tonic-gate void *el;
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate int ret;
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate ret = DynPut(obj, el, obj->num_el);
61*0Sstevel@tonic-gate if (ret != DYN_OK)
62*0Sstevel@tonic-gate return ret;
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate ++obj->num_el;
65*0Sstevel@tonic-gate return ret;
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate * WARNING! There is a reason this function is not documented in the
70*0Sstevel@tonic-gate * man page. If DynPut used to mutate already existing elements,
71*0Sstevel@tonic-gate * everything will go fine. If it is used to add new elements
72*0Sstevel@tonic-gate * directly, however, the state within the object (such as
73*0Sstevel@tonic-gate * obj->num_el) will not be updated properly and many other functions
74*0Sstevel@tonic-gate * in the library will lose. Have a nice day.
75*0Sstevel@tonic-gate */
DynPut(obj,el_in,idx)76*0Sstevel@tonic-gate int DynPut(obj, el_in, idx)
77*0Sstevel@tonic-gate DynObjectP obj;
78*0Sstevel@tonic-gate void *el_in;
79*0Sstevel@tonic-gate int idx;
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate DynPtr el = (DynPtr) el_in;
82*0Sstevel@tonic-gate int ret;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate if (obj->debug)
85*0Sstevel@tonic-gate fprintf(stderr, "dyn: put: Writing %d bytes from %d to %d + %d\n",
86*0Sstevel@tonic-gate obj->el_size, el, obj->array, idx*obj->el_size);
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate if ((ret = _DynResize(obj, idx)) != DYN_OK)
89*0Sstevel@tonic-gate return ret;
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
92*0Sstevel@tonic-gate memmove(obj->array + idx*obj->el_size, el, obj->el_size);
93*0Sstevel@tonic-gate #else
94*0Sstevel@tonic-gate bcopy(el, obj->array + idx*obj->el_size, obj->el_size);
95*0Sstevel@tonic-gate #endif
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate if (obj->debug)
98*0Sstevel@tonic-gate fprintf(stderr, "dyn: put: done.\n");
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate return DYN_OK;
101*0Sstevel@tonic-gate }
102