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