Add GNU LGPL headers to all .c .C and .h files
[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 librararies 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 "ilint.h"
44 #include "ilcontext.h"
45 #include "ilerrors.h"
46
47         /*  Functions in /ilc/ilobject.c :
48         */
49         /*  Called by ilCreateContext() to create object data in the given context.
50                 Returns: true if add was ok, else error: caller should free all.
51         */
52 IL_EXTERN ilBool _ilObjectInitContext (
53     ilContextPtr        pContext
54     );
55
56         /*  Called by ilDestroyContext() to destroy object data in the given context.
57         */
58 IL_EXTERN void _ilObjectDestroyContext (
59     ilContextPtr        pContext
60     );
61
62
63         /*  ------------------------ ilInternalCreateContext ------------------------- */
64         /*  Called by the macro IL_CREATE_CONTEXT() which passes in the version 
65             check number.  Only check the low-order 16 bits of the versionCheck;
66             that way backwards-compatible versions can be differentiated by the
67             upper 16 bits (which might signal the library to do something different).
68         */
69
70 ilError ilInternalCreateContext (
71     int                 versionCheck,
72     ilContext          *pContextReturn,         /* RETURNED */
73     unsigned long       mustBeZero
74     )
75 {
76 register ilContextPtr   pContext;
77
78         /*  If internal version # > the version # the library was built with, then
79             error; if <, old IL program using new library: supported.
80         */
81     if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
82         return IL_ERROR_VERSION_MISMATCH;
83     if (mustBeZero != 0)
84         return IL_ERROR_PAR_NOT_ZERO;
85
86     pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
87     if (!pContext) 
88         return IL_ERROR_MALLOC;
89
90     if (!_ilObjectInitContext (pContext)) {
91         IL_FREE (pContext);
92         return IL_ERROR_MALLOC;
93         }
94
95     pContext->p.error = 0;
96     pContext->p.errorInfo = 0;
97
98         /*  Init private type code to start at standard image types.
99         */
100     pContext->privateType = IL_MAX_TYPE + 1;
101     *pContextReturn = (ilContext)pContext;
102     return 0;
103 }
104
105
106 #ifdef IL_GARBAGE_MALLOC
107
108         /*  --------------------- ilMallocAndInitWithGarbage ------------------------ */
109         /*  Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
110             defined only during test/debug - not in production product.)
111             malloc the given nBytes, fill it with garbage and return ptr to it.
112         */
113 IL_PRIVATE void *_ilMallocAndInitWithGarbage (
114     unsigned long           nBytes
115     )
116 {
117 register ilPtr              p, pMalloc;
118
119     pMalloc = (ilPtr)malloc (nBytes);
120     if (p = pMalloc) {
121       while (nBytes-- > 0)
122         *p++ = 0xFD;
123       }
124     return (void *)pMalloc;
125 }
126
127 #endif
128
129         /*  ----------------------- ilGetPrivateType ----------------------- */
130         /*  Public function; see spec.
131         */
132
133 unsigned int ilGetPrivateType (
134     ilContext           context
135     )
136 {
137 register ilContextPtr   pContext;
138
139         /*  Increment code (but not if it has wrapped to zero! and return it.
140         */
141     pContext = IL_CONTEXT_PTR (context);
142     if (pContext->privateType != 0)
143         pContext->privateType++;
144     return pContext->privateType;
145 }
146
147
148         /*  ------------------------ ilDestroyContext ---------------------------- */
149         /*  Public function; see spec.
150         */
151
152 ilBool ilDestroyContext (
153     ilContext           context
154     )
155 {
156 register ilContextPtr   pContext;
157 int                     i;
158
159         /*  Destroy all objects associated with this context, then free
160             any data pointed to in pAlloc array.
161         */
162     pContext = IL_CONTEXT_PTR (context);
163     _ilObjectDestroyContext (pContext);
164
165     for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
166         if (pContext->pAlloc[i])
167             IL_FREE (pContext->pAlloc[i]);
168         }
169
170     IL_FREE (pContext);
171     return TRUE;
172 }
173
174         /*  ------------------------ ilIntersectRect ------------------------------ */
175         /*  Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
176             storing the result in "*pDstRect".
177                 Not in this file for any particular reason; no other logical place for it.
178         */
179 IL_PRIVATE void _ilIntersectRect (
180     register ilRect     *pSrcRect,
181     register ilRect     *pDstRect
182     )
183 {
184 long                    left, top, right, bottom, i;
185
186         /*  Change to non-inclusive coords for easier compare.
187         */
188     left = pSrcRect->x;
189     right = left + pSrcRect->width;
190     top = pSrcRect->y;
191     bottom = top + pSrcRect->height;
192
193     if (pDstRect->x > left)
194         left = pDstRect->x;
195     if (pDstRect->y > top)
196         top = pDstRect->y;
197     i = pDstRect->x + pDstRect->width;          /* pDstRect right */
198     if (i < right)
199         right = i;
200     i = pDstRect->y + pDstRect->height;         /* pDstRect bottom */
201     if (i < bottom)
202         bottom = i;
203
204         /*  Store result into pDstRect, with width/height always >= 0.
205         */
206     pDstRect->x = left;
207     pDstRect->y = top;
208     pDstRect->width = (right <= left) ? 0 : right - left;
209     pDstRect->height = (bottom <= top) ? 0 : bottom - top;
210 }
211