Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / DtHelp / il / ilcontext.c
1 /* $XConsortium: ilcontext.c /main/3 1995/10/23 15:43:53 rswiston $ */
2 /**---------------------------------------------------------------------
3 ***     
4 ***    (c)Copyright 1991 Hewlett-Packard Co.
5 ***    
6 ***                             RESTRICTED RIGHTS LEGEND
7 ***    Use, duplication, or disclosure by the U.S. Government is subject to
8 ***    restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
9 ***    Technical Data and Computer Software clause in DFARS 252.227-7013.
10 ***                             Hewlett-Packard Company
11 ***                             3000 Hanover Street
12 ***                             Palo Alto, CA 94304 U.S.A.
13 ***    Rights for non-DOD U.S. Government Departments and Agencies are as set
14 ***    forth in FAR 52.227-19(c)(1,2).
15 ***
16 ***-------------------------------------------------------------------*/
17
18         /*  ilcontext.c - Contains ilCreate/DestroyContext() and related code.
19         */
20
21 #include "ilint.h"
22 #include "ilcontext.h"
23 #include "ilerrors.h"
24
25         /*  Functions in /ilc/ilobject.c :
26         */
27         /*  Called by ilCreateContext() to create object data in the given context.
28                 Returns: true if add was ok, else error: caller should free all.
29         */
30 IL_EXTERN ilBool _ilObjectInitContext (
31     ilContextPtr        pContext
32     );
33
34         /*  Called by ilDestroyContext() to destroy object data in the given context.
35         */
36 IL_EXTERN void _ilObjectDestroyContext (
37     ilContextPtr        pContext
38     );
39
40
41         /*  ------------------------ ilInternalCreateContext ------------------------- */
42         /*  Called by the macro IL_CREATE_CONTEXT() which passes in the version 
43             check number.  Only check the low-order 16 bits of the versionCheck;
44             that way backwards-compatible versions can be differentiated by the
45             upper 16 bits (which might signal the library to do something different).
46         */
47
48 ilError ilInternalCreateContext (
49     int                 versionCheck,
50     ilContext          *pContextReturn,         /* RETURNED */
51     unsigned long       mustBeZero
52     )
53 {
54 register ilContextPtr   pContext;
55
56         /*  If internal version # > the version # the library was built with, then
57             error; if <, old IL program using new library: supported.
58         */
59     if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
60         return IL_ERROR_VERSION_MISMATCH;
61     if (mustBeZero != 0)
62         return IL_ERROR_PAR_NOT_ZERO;
63
64     pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
65     if (!pContext) 
66         return IL_ERROR_MALLOC;
67
68     if (!_ilObjectInitContext (pContext)) {
69         IL_FREE (pContext);
70         return IL_ERROR_MALLOC;
71         }
72
73     pContext->p.error = 0;
74     pContext->p.errorInfo = 0;
75
76         /*  Init private type code to start at standard image types.
77         */
78     pContext->privateType = IL_MAX_TYPE + 1;
79     *pContextReturn = (ilContext)pContext;
80     return 0;
81 }
82
83
84 #ifdef IL_GARBAGE_MALLOC
85
86         /*  --------------------- ilMallocAndInitWithGarbage ------------------------ */
87         /*  Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
88             defined only during test/debug - not in production product.)
89             malloc the given nBytes, fill it with garbage and return ptr to it.
90         */
91 IL_PRIVATE void *_ilMallocAndInitWithGarbage (
92     unsigned long           nBytes
93     )
94 {
95 register ilPtr              p, pMalloc;
96
97     pMalloc = (ilPtr)malloc (nBytes);
98     if (p = pMalloc) {
99       while (nBytes-- > 0)
100         *p++ = 0xFD;
101       }
102     return (void *)pMalloc;
103 }
104
105 #endif
106
107         /*  ----------------------- ilGetPrivateType ----------------------- */
108         /*  Public function; see spec.
109         */
110
111 unsigned int ilGetPrivateType (
112     ilContext           context
113     )
114 {
115 register ilContextPtr   pContext;
116
117         /*  Increment code (but not if it has wrapped to zero! and return it.
118         */
119     pContext = IL_CONTEXT_PTR (context);
120     if (pContext->privateType != 0)
121         pContext->privateType++;
122     return pContext->privateType;
123 }
124
125
126         /*  ------------------------ ilDestroyContext ---------------------------- */
127         /*  Public function; see spec.
128         */
129
130 ilBool ilDestroyContext (
131     ilContext           context
132     )
133 {
134 register ilContextPtr   pContext;
135 int                     i;
136
137         /*  Destroy all objects associated with this context, then free
138             any data pointed to in pAlloc array.
139         */
140     pContext = IL_CONTEXT_PTR (context);
141     _ilObjectDestroyContext (pContext);
142
143     for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
144         if (pContext->pAlloc[i])
145             IL_FREE (pContext->pAlloc[i]);
146         }
147
148     IL_FREE (pContext);
149     return TRUE;
150 }
151
152         /*  ------------------------ ilIntersectRect ------------------------------ */
153         /*  Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
154             storing the result in "*pDstRect".
155                 Not in this file for any particular reason; no other logical place for it.
156         */
157 IL_PRIVATE void _ilIntersectRect (
158     register ilRect     *pSrcRect,
159     register ilRect     *pDstRect
160     )
161 {
162 long                    left, top, right, bottom, i;
163
164         /*  Change to non-inclusive coords for easier compare.
165         */
166     left = pSrcRect->x;
167     right = left + pSrcRect->width;
168     top = pSrcRect->y;
169     bottom = top + pSrcRect->height;
170
171     if (pDstRect->x > left)
172         left = pDstRect->x;
173     if (pDstRect->y > top)
174         top = pDstRect->y;
175     i = pDstRect->x + pDstRect->width;          /* pDstRect right */
176     if (i < right)
177         right = i;
178     i = pDstRect->y + pDstRect->height;         /* pDstRect bottom */
179     if (i < bottom)
180         bottom = i;
181
182         /*  Store result into pDstRect, with width/height always >= 0.
183         */
184     pDstRect->x = left;
185     pDstRect->y = top;
186     pDstRect->width = (right <= left) ? 0 : right - left;
187     pDstRect->height = (bottom <= top) ? 0 : bottom - top;
188 }
189