source: trunk/dll/makelist.c@ 1029

Last change on this file since 1029 was 1029, checked in by Gregg Young, 17 years ago

Fixed early memory free; Added free_... functions to make fortify checking easier; Added fortify scopes; Delete now moves to trash can on systems with the xworkplace trash can installed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 1029 2008-06-23 01:30:16Z gyoung $
5
6 Make file lists
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2003, 2008 Steven H.Levine
10
11 12 Feb 03 SHL AddToFileList: standardize EA math
12 22 Jul 06 SHL Use Runtime_Error
13 22 Jul 06 SHL AddToList optimize
14 06 Apr 07 GKY Work around PM DragInfo and DrgFreeDISH limits
15 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
16 29 Feb 08 GKY Use xfree where appropriate
17
18***********************************************************************/
19
20#include <stdlib.h>
21#include <string.h>
22
23#define INCL_DOS
24#define INCL_WIN
25#define INCL_LONGLONG
26
27#include "fm3str.h"
28#include "makelist.h"
29#include "errutil.h" // Dos_Error...
30#include "strutil.h" // GetPString
31#include "dircnrs.h"
32#include "fm3dll.h" // 05 Jan 08 SHL fixme to be gone
33
34#include "fortify.h" // 06 May 08 SHL
35
36static PSZ pszSrcFile = __FILE__;
37
38VOID SortList(LISTINFO *li)
39{
40 /* bubble-sort entries by size, descending */
41
42 UINT x;
43 CHAR *s;
44 ULONG l;
45 BOOL swapped;
46
47 if (li && li->list && li->list[0] && li->cbFile) {
48 do {
49 swapped = FALSE;
50 for (x = 0; li->list[x] && li->list[x + 1]; x++) {
51 if (li->cbFile[x] < li->cbFile[x + 1]) {
52 s = li->list[x];
53 li->list[x] = li->list[x + 1];
54 li->list[x + 1] = s;
55 l = li->cbFile[x];
56 li->cbFile[x] = li->cbFile[x + 1];
57 li->cbFile[x + 1] = l;
58 if (li->ulitemID) {
59 l = li->ulitemID[x];
60 li->ulitemID[x] = li->ulitemID[x + 1];
61 li->ulitemID[x + 1] = l;
62 }
63 swapped = TRUE;
64 }
65 }
66 } while (swapped);
67 }
68}
69
70VOID FreeListInfo(LISTINFO *li)
71{
72 if (li) {
73 xfree(li->ulitemID, pszSrcFile, __LINE__);
74 xfree(li->cbFile, pszSrcFile, __LINE__);
75 if (li->list)
76 FreeList(li->list);
77 xfree(li, pszSrcFile, __LINE__);
78# ifdef FORTIFY
79 Fortify_LeaveScope();
80# endif
81 }
82}
83
84VOID FreeList(CHAR **list)
85{
86 UINT x;
87
88 if (list) {
89 for (x = 0; list[x]; x++) {
90#ifdef __DEBUG_ALLOC__
91 _heap_check();
92#endif
93 free(list[x]);
94 }
95#ifdef __DEBUG_ALLOC__
96 _heap_check();
97#endif
98 xfree(list, pszSrcFile, __LINE__);
99# ifdef FORTIFY
100 Fortify_LeaveScope();
101# endif
102 }
103 DosPostEventSem(CompactSem);
104}
105
106INT AddToFileList(CHAR *string, FILEFINDBUF4L *ffb4, FILELIST ***list,
107 UINT *pnumfiles, UINT *pnumalloced)
108{
109 FILELIST *pfl;
110
111 if (string && ffb4) {
112 // Ensure room for NULL entry
113 if (((*pnumfiles) + 3) > *pnumalloced) {
114 FILELIST **pflArray;
115
116 // Use plain realloc for speed
117 // 06 Aug 07 SHL fixme to know why + 6
118# ifdef FORTIFY
119 Fortify_EnterScope();
120# endif
121 pflArray = realloc(*list, (*pnumalloced + 6) * sizeof(FILELIST *));
122 if (!pflArray) {
123 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
124 return 1;
125 }
126 (*pnumalloced) += 6;
127 *list = pflArray;
128 }
129 // Use plain malloc for speed
130 pfl = malloc(sizeof(FILELIST) + strlen(string));
131 if (!pfl) {
132 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
133 return 2;
134 }
135 pfl->attrFile = ffb4->attrFile;
136 pfl->date = ffb4->fdateLastWrite;
137 pfl->time = ffb4->ftimeLastWrite;
138 pfl->ladate = ffb4->fdateLastAccess;
139 pfl->latime = ffb4->ftimeLastAccess;
140 pfl->crdate = ffb4->fdateCreation;
141 pfl->crtime = ffb4->ftimeCreation;
142 pfl->cbFile = ffb4->cbFile;
143 pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
144 strcpy(pfl->fname, string);
145 (*list)[*pnumfiles] = pfl;
146 (*pnumfiles)++;
147 // Ensure list always ends with two NULL entries
148 // 06 Aug 07 SHL fixme to know why
149 (*list)[*pnumfiles] = NULL;
150 (*list)[(*pnumfiles) + 1] = NULL;
151#ifdef __DEBUG_ALLOC__
152 _heap_check();
153#endif
154 }
155 return 0;
156}
157
158/**
159 * Add string to string list
160 * Enlarges as needed
161 * Ensures 2 NULL end markers exist
162 */
163
164INT AddToList(CHAR *string, CHAR ***list, UINT *pnumfiles, UINT *pnumalloced)
165{
166 CHAR **ppsz;
167 PSZ psz;
168
169 if (string) {
170 if (((*pnumfiles) + 3) > *pnumalloced) {
171 // Use plain realloc for speed
172# ifdef FORTIFY
173 Fortify_EnterScope();
174# endif
175 ppsz = realloc(*list, (*pnumalloced + 6) * sizeof(CHAR *));
176 if (!ppsz) {
177 Runtime_Error(pszSrcFile, __LINE__, "realloc");
178 return 1;
179 }
180 (*pnumalloced) += 6;
181 *list = ppsz;
182 }
183 // Use plain malloc for speed
184 psz = malloc(strlen(string) + 1);
185 if (!psz) {
186 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
187 return 2;
188 }
189 (*list)[*pnumfiles] = psz;
190 strcpy((*list)[*pnumfiles], string); // Add entry
191 (*pnumfiles)++;
192 (*list)[*pnumfiles] = NULL; // Add end marker
193 (*list)[(*pnumfiles) + 1] = NULL; // Add 2nd end marker - fixme to know why?
194#ifdef __DEBUG_ALLOC__
195 _heap_check();
196#endif
197 }
198 return 0;
199}
200
201CHAR **BuildList(HWND hwndCnr)
202{
203 PCNRITEM pci;
204 CHAR **list = NULL, **test;
205 UINT numfiles = 0, numalloc = 0;
206 INT error = 0, attribute = CRA_CURSORED;
207
208 pci = (PCNRITEM) CurrentRecord(hwndCnr);
209 if (pci && (INT) pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
210 if (pci->rc.flRecordAttr & CRA_SELECTED) {
211 attribute = CRA_SELECTED;
212 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
213 MPFROMSHORT(attribute));
214 }
215 }
216 while (pci && (INT) pci != -1 && !error) {
217 if (!(pci->rc.flRecordAttr & CRA_FILTERED))
218 error = AddToList(pci->pszFileName, &list, &numfiles, &numalloc);
219 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pci),
220 MPFROMSHORT(attribute));
221 }
222 if (numalloc > numfiles + 1) {
223 // Use plain realloc for speed
224
225# ifdef FORTIFY
226 Fortify_EnterScope();
227# endif
228 test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
229 if (!test)
230 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
231 else
232 list = test;
233 } // while
234 return list;
235}
236
237CHAR **BuildArcList(HWND hwndCnr)
238{
239 PARCITEM pai;
240 CHAR **list = NULL;
241 UINT numfiles = 0, numalloc = 0;
242 INT error = 0, attribute = CRA_CURSORED;
243
244 pai = (PARCITEM) CurrentRecord(hwndCnr);
245 if (pai && (INT) pai != -1) {
246 if (pai->rc.flRecordAttr & CRA_SELECTED) {
247 attribute = CRA_SELECTED;
248 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
249 MPFROMSHORT(attribute));
250 }
251 }
252 while (pai && (INT) pai != -1 && !error) {
253 if (!(pai->rc.flRecordAttr & CRA_FILTERED))
254 error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
255 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
256 MPFROMSHORT(attribute));
257 }
258 return list;
259}
260
261CHAR **RemoveFromList(CHAR **list, CHAR *item)
262{
263 UINT x, y;
264
265 if (list && list[0] && item) {
266 for (x = 0; list[x]; x++) {
267 if (item == list[x]) {
268 free(list[x]);
269 list[x] = NULL;
270 for (y = x;; y++) {
271 if (y != x && !list[y])
272 break;
273 list[y] = list[y + 1];
274 }
275 if (!list[0]) {
276 FreeList(list);
277 list = NULL;
278 }
279# ifdef FORTIFY
280 Fortify_LeaveScope();
281# endif
282 break;
283 }
284 }
285 }
286 return list;
287}
288
289CHAR **CombineLists(CHAR **prime, CHAR **add)
290{
291 UINT x;
292 UINT numalloc, numfiles = 0;
293
294 if (add && add[0]) {
295 if (prime) {
296 for (x = 0; prime[x]; x++)
297 numfiles++;
298 }
299 numalloc = numfiles;
300 for (x = 0; add[x]; x++) {
301 if (*add[x])
302 AddToList(add[x], &prime, &numfiles, &numalloc);
303 }
304 FreeList(add);
305 }
306 return prime;
307}
308
309#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
310#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
Note: See TracBrowser for help on using the repository browser.