source: trunk/dll/makelist.c@ 773

Last change on this file since 773 was 773, checked in by Steven Levine, 18 years ago

Correct some compare directories collector nits
Use BldQuoted... functions
Move BldQuoted... functions near primary callers
Add RUNTYPE_MASK
Use Runtime_Error2 more

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