source: trunk/dll/makelist.c@ 1185

Last change on this file since 1185 was 1185, checked in by John Small, 17 years ago

Ticket 187: Draft 2: Move remaining function declarations

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