source: trunk/dll/makelist.c@ 1063

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

Fortify ifdef reformat

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