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