Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtHelp / CleanUp.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: CleanUp.c /main/10 1996/01/29 12:19:40 cde-hp $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  **
27  **   File:        CleanUp.c
28  **
29  **   Project:     Cde DtHelp
30  **
31  **   Description:
32  **
33  **  (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
34  **
35  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
36  **  (c) Copyright 1993, 1994 International Business Machines Corp.
37  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
38  **  (c) Copyright 1993, 1994 Novell, Inc.
39  **
40  **
41  ****************************************************************************
42  ************************************<+>*************************************/
43
44 /*
45  * system includes
46  */
47 #include <stdlib.h>
48
49 /*
50  * Canvas Engine includes
51  */
52 #include "CanvasP.h"
53 #include "CanvasSegP.h"
54 #include "LinkMgrP.h"
55
56 /*
57  * private includes
58  */
59 #include "bufioI.h"
60 #include "CleanUpI.h"
61 #include "FontAttrI.h"
62 #include "FormatUtilI.h"
63 #include "RegionI.h"
64 #include "XInterfaceI.h"
65
66 #ifdef NLS16
67 #endif
68
69 /********    Private Function Declarations    ********/
70 /********    End Private Function Declarations    ********/
71
72 /*****************************************************************************
73  *              Private Defines
74  *****************************************************************************/
75 /*****************************************************************************
76  *              Private Macros
77  *****************************************************************************/
78 #define AbbrevStr(x)            (FrmtPrivInfoPtr(x)->abbrev)
79 #define DupFlag(x)              (FrmtPrivInfoPtr(x)->dup_flag)
80 #define MatchInfo(x)            (FrmtPrivInfoPtr(x)->match_info)
81 #define TopFlag(x)              (FrmtPrivInfoPtr(x)->top_block)
82 /*****************************************************************************
83  *              Private Functions
84  *****************************************************************************/
85 static void
86 CheckFreePrivInfo(_DtCvSegment *seg)
87 {
88     /*
89      * free the match information
90      */
91     if (NULL != MatchInfo(seg))
92       {
93         SdlMatchData *m = (SdlMatchData *)(MatchInfo(seg));
94
95         if (NULL != m->ssi)
96             free(m->ssi);
97         free(m);
98       }
99
100     /*
101      * free any abbreviation
102      */
103     if (NULL != AbbrevStr(seg))
104         free(AbbrevStr(seg));
105 }
106
107 static void
108 FreePrivateAndSeg(_DtCvSegment *seg)
109 {
110     if (NULL != seg)
111       {
112         /*
113          * free the private information block
114          */
115         if (NULL != FrmtPrivInfoPtr(seg))
116             free(FrmtPrivInfoPtr(seg));
117
118         /*
119          * free the segment block
120          */
121         free(seg);
122       }
123 }
124
125 /*****************************************************************************
126  *              Semi-Private Variables
127  *****************************************************************************/
128 /*****************************************************************************
129  *              Semi-Public Functions
130  *****************************************************************************/
131 /*****************************************************************************
132  * Function:    void _DtHelpFreeSegments (_DtCvSegment *seg_list)
133  *
134  * Parameters:
135  *              seg_list        Specifies the Canvas Engine segment list.
136  *
137  * Returns:     Nothing
138  *
139  * Purpose:     Free all memory associated with an SDL list.
140  *****************************************************************************/
141 void
142 _DtHelpFreeSegments (
143     _DtCvSegment        *seg_list,
144     _DtCvStatus          unresolved,
145     void                (*destroy_region)(),
146     _DtCvPointer         client_data)
147 {
148     int                    i;
149     char                   dupFlag;
150     char                 **strs;
151     _DtCvSegment         *nextSeg;
152     _DtCvSegment         *topSeg = NULL;
153     _DtCvSegment         *topTab = NULL;
154     _DtCvSegment        **tableSeg;
155     _DtHelpDARegion      *pReg;
156
157     while (seg_list != NULL)
158       {
159         dupFlag = DupFlag(seg_list);
160         nextSeg = seg_list->next_seg;
161         switch (_DtCvPrimaryTypeOfSeg(seg_list))
162           {
163             case _DtCvCONTAINER:
164                         /*
165                          * free the id block.
166                          */
167                         if (False == dupFlag &&
168                                       NULL != _DtCvContainerIdOfSeg(seg_list))
169                             free(_DtCvContainerIdOfSeg(seg_list));
170
171                         /*
172                          * free the contents of the block
173                          */
174                         _DtHelpFreeSegments(_DtCvContainerListOfSeg(seg_list),
175                                                 unresolved,
176                                                 destroy_region,
177                                                 client_data);
178                         break;
179
180             case _DtCvMARKER:
181                         if (False == dupFlag)
182                             free(_DtCvIdOfMarkerSeg(seg_list));
183                         break;
184
185             case _DtCvREGION:
186                         if (False == dupFlag && NULL != destroy_region)
187                             (destroy_region)(client_data, 
188                                                 _DtCvInfoOfRegionSeg(seg_list));
189                         break;
190
191             case _DtCvSTRING:
192                         if (False == dupFlag)
193                           {
194                             free(_DtCvStringOfStringSeg(seg_list));
195                             if (True == unresolved &&
196                                         NULL != _DtCvFontOfStringSeg(seg_list))
197                                 free(_DtCvFontOfStringSeg(seg_list));
198                           }
199                         break;
200
201             case _DtCvTABLE:
202                         if (True == dupFlag)
203                             break;
204
205                         /*
206                          * free the blocks
207                          */
208                         tableSeg = _DtCvCellsOfTableSeg(seg_list);
209                         while (NULL != tableSeg && NULL != *tableSeg)
210                           {
211                             /*
212                              * free the id block.
213                              */
214                             if (False == DupFlag(*tableSeg)
215                                    && NULL != _DtCvContainerIdOfSeg(*tableSeg))
216                                 free(_DtCvContainerIdOfSeg(*tableSeg));
217
218                             /*
219                              * free the contents of the block
220                              */
221                             _DtHelpFreeSegments(
222                                         _DtCvContainerListOfSeg(*tableSeg),
223                                                                 unresolved,
224                                                                 destroy_region,
225                                                                 client_data);
226                             tableSeg++;
227                           }
228
229                         /*
230                          * free each of the table cells
231                          */
232                         tableSeg = _DtCvCellsOfTableSeg(seg_list);
233                         while (NULL != tableSeg && NULL != *tableSeg)
234                           {
235                             if (True == TopFlag(*tableSeg))
236                               {
237                                 FreePrivateAndSeg(topTab);
238                                 topTab = *tableSeg;
239                               }
240                             CheckFreePrivInfo(*tableSeg);
241                             tableSeg++;
242                           }
243
244                         FreePrivateAndSeg(topTab);
245                         topTab = NULL;
246
247                         /*
248                          * free the list of cells
249                          */
250                         tableSeg = _DtCvCellsOfTableSeg(seg_list);
251                         free (tableSeg);
252
253                         /*
254                          * free the row ids.
255                          */
256                         _DtHelpCeFreeStringArray(
257                                         _DtCvCellIdsOfTableSeg(seg_list));
258
259                         /*
260                          * free the column justification and width.
261                          */
262                         free((void *) _DtCvColJustifyOfTableSeg(seg_list));
263                         strs = _DtCvColWOfTableSeg(seg_list);
264                         for (i = 0;
265                            NULL != strs && i < _DtCvNumColsOfTableSeg(seg_list);
266                                                                 i++, strs++)
267                             free((void *) *strs);
268                         if (NULL != _DtCvColWOfTableSeg(seg_list))
269                             free((void *) _DtCvColWOfTableSeg(seg_list));
270
271                         break;
272           }
273
274         CheckFreePrivInfo(seg_list);
275         if (True == TopFlag(seg_list))
276           {
277             FreePrivateAndSeg(topSeg);
278             topSeg = seg_list;
279           }
280         seg_list = nextSeg;
281       }
282
283     FreePrivateAndSeg(topSeg);
284
285 } /* End _DtHelpFreeSegments */
286
287 /*****************************************************************************
288  * Function:    void _DtHelpDestroyTopicData (
289  *                                      _DtCvSegment *seg_list)
290  *
291  * Parameters:
292  *              seg_list        Specifies the SDL segment list.
293  *
294  * Returns:     Nothing
295  *
296  * Purpose:     Free all memory associated with an SDL list.
297  *****************************************************************************/
298 void
299 _DtHelpDestroyTopicData (
300     _DtCvTopicInfo      *topic,
301     void                (*destroy_region)(),
302     _DtCvPointer         client_data)
303 {
304     if (NULL != topic)
305       {
306         /*
307          * free the segment list
308          */
309         _DtHelpFreeSegments(topic->seg_list, _DtCvFALSE,
310                                                 destroy_region, client_data);
311
312         /*
313          * free the id string
314          */
315         if (NULL != topic->id_str)
316             free(topic->id_str);
317
318         /*
319          * free the database
320          */
321         _DtLinkDbDestroy(topic->link_data);
322
323         /*
324          * free the structure
325          */
326         free(topic);
327       }
328
329 } /* End _DtHelpDestroyTopicData */