source: trunk/dll/makelist.c@ 1018

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

Additional fortify scopes mostly for "lists"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 1018 2008-05-26 19:34:56Z 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 pflArray = realloc(*list, (*pnumalloced + 6) * sizeof(FILELIST *));
119 if (!pflArray) {
120 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
121 return 1;
122 }
123 (*pnumalloced) += 6;
124 *list = pflArray;
125 }
126 // Use plain malloc for speed
127 pfl = malloc(sizeof(FILELIST) + strlen(string));
128 if (!pfl) {
129 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
130 return 2;
131 }
132 pfl->attrFile = ffb4->attrFile;
133 pfl->date = ffb4->fdateLastWrite;
134 pfl->time = ffb4->ftimeLastWrite;
135 pfl->ladate = ffb4->fdateLastAccess;
136 pfl->latime = ffb4->ftimeLastAccess;
137 pfl->crdate = ffb4->fdateCreation;
138 pfl->crtime = ffb4->ftimeCreation;
139 pfl->cbFile = ffb4->cbFile;
140 pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
141 strcpy(pfl->fname, string);
142 (*list)[*pnumfiles] = pfl;
143 (*pnumfiles)++;
144 // Ensure list always ends with two NULL entries
145 // 06 Aug 07 SHL fixme to know why
146 (*list)[*pnumfiles] = NULL;
147 (*list)[(*pnumfiles) + 1] = NULL;
148#ifdef __DEBUG_ALLOC__
149 _heap_check();
150#endif
151 }
152 return 0;
153}
154
155/**
156 * Add string to string list
157 * Enlarges as needed
158 * Ensures 2 NULL end markers exist
159 */
160
161INT AddToList(CHAR *string, CHAR ***list, UINT *pnumfiles, UINT *pnumalloced)
162{
163 CHAR **ppsz;
164 PSZ psz;
165
166 if (string) {
167 if (((*pnumfiles) + 3) > *pnumalloced) {
168 // Use plain realloc for speed
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 test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
219 if (!test)
220 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
221 else
222 list = test;
223 } // while
224 return list;
225}
226
227CHAR **BuildArcList(HWND hwndCnr)
228{
229 PARCITEM pai;
230 CHAR **list = NULL;
231 UINT numfiles = 0, numalloc = 0;
232 INT error = 0, attribute = CRA_CURSORED;
233
234 pai = (PARCITEM) CurrentRecord(hwndCnr);
235 if (pai && (INT) pai != -1) {
236 if (pai->rc.flRecordAttr & CRA_SELECTED) {
237 attribute = CRA_SELECTED;
238 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
239 MPFROMSHORT(attribute));
240 }
241 }
242 while (pai && (INT) pai != -1 && !error) {
243 if (!(pai->rc.flRecordAttr & CRA_FILTERED))
244 error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
245 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
246 MPFROMSHORT(attribute));
247 }
248 return list;
249}
250
251CHAR **RemoveFromList(CHAR **list, CHAR *item)
252{
253 UINT x, y;
254
255 if (list && list[0] && item) {
256 for (x = 0; list[x]; x++) {
257 if (item == list[x]) {
258 free(list[x]);
259 list[x] = NULL;
260 for (y = x;; y++) {
261 if (y != x && !list[y])
262 break;
263 list[y] = list[y + 1];
264 }
265 if (!list[0]) {
266 FreeList(list);
267 list = NULL;
268 }
269 break;
270 }
271 }
272 }
273 return list;
274}
275
276CHAR **CombineLists(CHAR **prime, CHAR **add)
277{
278 UINT x;
279 UINT numalloc, numfiles = 0;
280
281 if (add && add[0]) {
282 if (prime) {
283 for (x = 0; prime[x]; x++)
284 numfiles++;
285 }
286 numalloc = numfiles;
287 for (x = 0; add[x]; x++) {
288 if (*add[x])
289 AddToList(add[x], &prime, &numfiles, &numalloc);
290 }
291 FreeList(add);
292 }
293 return prime;
294}
295
296#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
297#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
Note: See TracBrowser for help on using the repository browser.