source: trunk/dll/makelist.c@ 1009

Last change on this file since 1009 was 1009, checked in by Steven Levine, 17 years ago

Add xfree xstrdup Fortify support
Add MT capable Fortify scope logic

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 1009 2008-05-10 07:51:58Z stevenhl $
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 }
79}
80
81VOID FreeList(CHAR **list)
82{
83 UINT x;
84
85 if (list) {
86 for (x = 0; list[x]; x++) {
87#ifdef __DEBUG_ALLOC__
88 _heap_check();
89#endif
90 free(list[x]);
91 }
92#ifdef __DEBUG_ALLOC__
93 _heap_check();
94#endif
95 xfree(list, pszSrcFile, __LINE__);
96 }
97 DosPostEventSem(CompactSem);
98}
99
100INT AddToFileList(CHAR *string, FILEFINDBUF4L *ffb4, FILELIST ***list,
101 UINT *pnumfiles, UINT *pnumalloced)
102{
103 FILELIST *pfl;
104
105 if (string && ffb4) {
106 // Ensure room for NULL entry
107 if (((*pnumfiles) + 3) > *pnumalloced) {
108 FILELIST **pflArray;
109
110 // Use plain realloc for speed
111 // 06 Aug 07 SHL fixme to know why + 6
112 pflArray = realloc(*list, (*pnumalloced + 6) * sizeof(FILELIST *));
113 if (!pflArray) {
114 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
115 return 1;
116 }
117 (*pnumalloced) += 6;
118 *list = pflArray;
119 }
120 // Use plain malloc for speed
121 pfl = malloc(sizeof(FILELIST) + strlen(string));
122 if (!pfl) {
123 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
124 return 2;
125 }
126 pfl->attrFile = ffb4->attrFile;
127 pfl->date = ffb4->fdateLastWrite;
128 pfl->time = ffb4->ftimeLastWrite;
129 pfl->ladate = ffb4->fdateLastAccess;
130 pfl->latime = ffb4->ftimeLastAccess;
131 pfl->crdate = ffb4->fdateCreation;
132 pfl->crtime = ffb4->ftimeCreation;
133 pfl->cbFile = ffb4->cbFile;
134 pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
135 strcpy(pfl->fname, string);
136 (*list)[*pnumfiles] = pfl;
137 (*pnumfiles)++;
138 // Ensure list always ends with two NULL entries
139 // 06 Aug 07 SHL fixme to know why
140 (*list)[*pnumfiles] = NULL;
141 (*list)[(*pnumfiles) + 1] = NULL;
142#ifdef __DEBUG_ALLOC__
143 _heap_check();
144#endif
145 }
146 return 0;
147}
148
149/**
150 * Add string to string list
151 * Enlarges as needed
152 * Ensures 2 NULL end markers exist
153 */
154
155INT AddToList(CHAR *string, CHAR ***list, UINT *pnumfiles, UINT *pnumalloced)
156{
157 CHAR **ppsz;
158 PSZ psz;
159
160 if (string) {
161 if (((*pnumfiles) + 3) > *pnumalloced) {
162 // Use plain realloc for speed
163 ppsz = realloc(*list, (*pnumalloced + 6) * sizeof(CHAR *));
164 if (!ppsz) {
165 Runtime_Error(pszSrcFile, __LINE__, "realloc");
166 return 1;
167 }
168 (*pnumalloced) += 6;
169 *list = ppsz;
170 }
171 // Use plain malloc for speed
172 psz = malloc(strlen(string) + 1);
173 if (!psz) {
174 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
175 return 2;
176 }
177 (*list)[*pnumfiles] = psz;
178 strcpy((*list)[*pnumfiles], string); // Add entry
179 (*pnumfiles)++;
180 (*list)[*pnumfiles] = NULL; // Add end marker
181 (*list)[(*pnumfiles) + 1] = NULL; // Add 2nd end marker - fixme to know why?
182#ifdef __DEBUG_ALLOC__
183 _heap_check();
184#endif
185 }
186 return 0;
187}
188
189CHAR **BuildList(HWND hwndCnr)
190{
191 PCNRITEM pci;
192 CHAR **list = NULL, **test;
193 UINT numfiles = 0, numalloc = 0;
194 INT error = 0, attribute = CRA_CURSORED;
195
196 pci = (PCNRITEM) CurrentRecord(hwndCnr);
197 if (pci && (INT) pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
198 if (pci->rc.flRecordAttr & CRA_SELECTED) {
199 attribute = CRA_SELECTED;
200 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
201 MPFROMSHORT(attribute));
202 }
203 }
204 while (pci && (INT) pci != -1 && !error) {
205 if (!(pci->rc.flRecordAttr & CRA_FILTERED))
206 error = AddToList(pci->pszFileName, &list, &numfiles, &numalloc);
207 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pci),
208 MPFROMSHORT(attribute));
209 }
210 if (numalloc > numfiles + 1) {
211 // Use plain realloc for speed
212 test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
213 if (!test)
214 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
215 else
216 list = test;
217 } // while
218 return list;
219}
220
221CHAR **BuildArcList(HWND hwndCnr)
222{
223 PARCITEM pai;
224 CHAR **list = NULL;
225 UINT numfiles = 0, numalloc = 0;
226 INT error = 0, attribute = CRA_CURSORED;
227
228 pai = (PARCITEM) CurrentRecord(hwndCnr);
229 if (pai && (INT) pai != -1) {
230 if (pai->rc.flRecordAttr & CRA_SELECTED) {
231 attribute = CRA_SELECTED;
232 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
233 MPFROMSHORT(attribute));
234 }
235 }
236 while (pai && (INT) pai != -1 && !error) {
237 if (!(pai->rc.flRecordAttr & CRA_FILTERED))
238 error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
239 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
240 MPFROMSHORT(attribute));
241 }
242 return list;
243}
244
245CHAR **RemoveFromList(CHAR **list, CHAR *item)
246{
247 UINT x, y;
248
249 if (list && list[0] && item) {
250 for (x = 0; list[x]; x++) {
251 if (item == list[x]) {
252 free(list[x]);
253 list[x] = NULL;
254 for (y = x;; y++) {
255 if (y != x && !list[y])
256 break;
257 list[y] = list[y + 1];
258 }
259 if (!list[0]) {
260 FreeList(list);
261 list = NULL;
262 }
263 break;
264 }
265 }
266 }
267 return list;
268}
269
270CHAR **CombineLists(CHAR **prime, CHAR **add)
271{
272 UINT x;
273 UINT numalloc, numfiles = 0;
274
275 if (add && add[0]) {
276 if (prime) {
277 for (x = 0; prime[x]; x++)
278 numfiles++;
279 }
280 numalloc = numfiles;
281 for (x = 0; add[x]; x++) {
282 if (*add[x])
283 AddToList(add[x], &prime, &numfiles, &numalloc);
284 }
285 FreeList(add);
286 }
287 return prime;
288}
289
290#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
291#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
Note: See TracBrowser for help on using the repository browser.