source: trunk/dll/makelist.c@ 551

Last change on this file since 551 was 551, checked in by Gregg Young, 19 years ago

Indentation cleanup

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