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